Skip to content

Commit

Permalink
Allow creating Kura component configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
reda-alaoui committed Jan 4, 2025
1 parent d795152 commit 2a1023f
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.api.shared.model;

import java.io.Serializable;

public class GwtConfigComponentCreator extends KapuaBaseModel implements Serializable {

private static final long serialVersionUID = -6388356998309026758L;

private static final String COMPONENT_FACTORY_ID = "componentFactoryId";
private static final String COMPONENT_ID = "componentId";

public String getComponentFactoryId() {
return get(COMPONENT_FACTORY_ID);
}

public String getUnescapedComponentFactoryId() {
return getUnescaped(COMPONENT_FACTORY_ID);
}

public void setComponentFactoryId(String componentFactoryId) {
set(COMPONENT_FACTORY_ID, componentFactoryId);
}

public String getComponentId() {
return get(COMPONENT_ID);
}

public String getUnescapedComponentId() {
return getUnescaped(COMPONENT_ID);
}

public void setComponentId(String componentId) {
set(COMPONENT_ID, componentId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*******************************************************************************
* Copyright (c) 2025 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.device.client.device.configuration;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.entity.EntityAddEditDialog;
import org.eclipse.kapua.app.console.module.api.client.ui.panel.FormPanel;
import org.eclipse.kapua.app.console.module.api.client.ui.widget.KapuaTextField;
import org.eclipse.kapua.app.console.module.api.client.util.ConsoleInfo;
import org.eclipse.kapua.app.console.module.api.client.util.DialogUtils;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigComponentCreator;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.device.client.messages.ConsoleDeviceMessages;
import org.eclipse.kapua.app.console.module.device.shared.model.GwtDevice;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceManagementService;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceManagementServiceAsync;

public class DeviceConfigAddDialog extends EntityAddEditDialog {

private static final ConsoleDeviceMessages MSGS = GWT.create(ConsoleDeviceMessages.class);
private static final ConsoleMessages CONSOLE_MSGS = GWT.create(ConsoleMessages.class);

private static final GwtDeviceManagementServiceAsync GWT_DEVICE_MANAGEMENT_SERVICE = GWT.create(GwtDeviceManagementService.class);

private final GwtDevice device;

private KapuaTextField<String> componentFactoryIdField;
private KapuaTextField<String> componentIdField;

public DeviceConfigAddDialog(GwtSession currentSession, GwtDevice device) {
super(currentSession);
this.device = device;
DialogUtils.resizeDialog(this, 400, 200);
}

@Override
public void createBody() {
submitButton.disable();
FormPanel roleFormPanel = new FormPanel(FORM_LABEL_WIDTH);
// Name
componentFactoryIdField = new KapuaTextField<String>();
componentFactoryIdField.setAllowBlank(false);
componentFactoryIdField.setMaxLength(255);
componentFactoryIdField.setFieldLabel("* " + MSGS.dialogDeviceConfigComponentAddComponentFactoryIdFieldName());
roleFormPanel.add(componentFactoryIdField);

componentIdField = new KapuaTextField<String>();
componentIdField.setAllowBlank(false);
componentIdField.setMaxLength(255);
componentIdField.setFieldLabel("* " + MSGS.dialogDeviceConfigComponentAddComponentIdFieldName());
roleFormPanel.add(componentIdField);

bodyPanel.add(roleFormPanel);
}

@Override
protected void preSubmit() {
if (componentFactoryIdField.getValue() == null || componentIdField.getValue() == null) {
ConsoleInfo.display("Error", CONSOLE_MSGS.allFieldsRequired());
}
super.preSubmit();
}

@Override
public void submit() {
GwtConfigComponentCreator creator = new GwtConfigComponentCreator();
creator.setComponentFactoryId(componentFactoryIdField.getValue());
creator.setComponentId(componentIdField.getValue());

GWT_DEVICE_MANAGEMENT_SERVICE.createComponentConfiguration(xsrfToken, device, creator, new AsyncCallback<Void>() {

@Override
public void onSuccess(Void result) {
exitStatus = true;
exitMessage = MSGS.dialogDeviceConfigComponentAddConfirmation();
hide();
}

@Override
public void onFailure(Throwable cause) {
exitStatus = false;
status.hide();
formPanel.getButtonBar().enable();
unmask();
submitButton.enable();
cancelButton.enable();

FailureHandler.handleFormException(formPanel, cause);
}

});
}

@Override
public String getHeaderMessage() {
return MSGS.dialogDeviceConfigComponentAddHeader();
}

@Override
public String getInfoMessage() {
return MSGS.dialogDeviceConfigComponentAddInfo();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
Expand All @@ -45,15 +46,19 @@
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaErrorCode;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaException;
import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.api.client.resources.icons.IconSet;
import org.eclipse.kapua.app.console.module.api.client.resources.icons.KapuaIcon;
import org.eclipse.kapua.app.console.module.api.client.ui.button.AddButton;
import org.eclipse.kapua.app.console.module.api.client.ui.button.DiscardButton;
import org.eclipse.kapua.app.console.module.api.client.ui.button.KapuaButton;
import org.eclipse.kapua.app.console.module.api.client.ui.button.RefreshButton;
import org.eclipse.kapua.app.console.module.api.client.ui.button.SaveButton;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.ActionDialog;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.InfoDialog;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.InfoDialog.InfoDialogType;
import org.eclipse.kapua.app.console.module.api.client.ui.dialog.KapuaMessageBox;
Expand All @@ -75,9 +80,6 @@
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceService;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceServiceAsync;

import java.util.ArrayList;
import java.util.List;

public class DeviceConfigComponents extends LayoutContainer {

private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);
Expand All @@ -97,6 +99,9 @@ public class DeviceConfigComponents extends LayoutContainer {
private Button refreshButton;
private boolean refreshProcess;

private Button addButton;
private boolean addProcess;

private Button apply;
private Button reset;

Expand Down Expand Up @@ -211,6 +216,14 @@ public void componentSelected(ButtonEvent ce) {
}
});

addButton = new AddButton(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent buttonEvent) {
add();
}
});

apply = new SaveButton(new SelectionListener<ButtonEvent>() {

@Override
Expand Down Expand Up @@ -259,6 +272,8 @@ public void componentSelected(ButtonEvent buttonEvent) {

toolBar.add(refreshButton);
toolBar.add(new SeparatorToolItem());
toolBar.add(addButton);
toolBar.add(new SeparatorToolItem());
toolBar.add(apply);
toolBar.add(new SeparatorToolItem());
toolBar.add(reset);
Expand Down Expand Up @@ -501,6 +516,36 @@ public void handleEvent(BaseEvent be) {
}
}

public void add() {
if (addProcess) {
return;
}
addButton.setEnabled(false);
addProcess = true;

DeviceConfigAddDialog dialog = new DeviceConfigAddDialog(gwtSession, selectedDevice);
dialog.addListener(Events.Hide, new Listener<ComponentEvent>() {
@Override
public void handleEvent(ComponentEvent be) {
addProcess = false;
addButton.setEnabled(true);

// Show exit popup
ActionDialog dialog = be.getComponent();
Boolean exitStatus = dialog.getExitStatus();
if (exitStatus == null) {
return;
}
if (exitStatus) {
ConsoleInfo.display(MSGS.popupInfo(), dialog.getExitMessage());
} else {
ConsoleInfo.display(MSGS.popupError(), dialog.getExitMessage());
}
}
});
dialog.show();
}

public void apply() {
if (!devConfPanel.isValid()) {
InfoDialog exitDialog = new InfoDialog(InfoDialogType.ERROR, DEVICE_MSGS.deviceConfigError());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void setEntity(GwtDevice gwtDevice) {

if (gwtDevice != null) {
setEnabled(gwtDevice.isOnline() && currentSession.hasPermission(DeviceManagementSessionPermission.read()));
getHeader().setVisible(gwtDevice.hasApplication(GwtDevice.GwtDeviceApplication.APP_CONFIGURATION));
getHeader().setVisible(gwtDevice.hasApplication(GwtDevice.GwtDeviceApplication.APP_CONFIGURATION_V1) || gwtDevice.hasApplication(GwtDevice.GwtDeviceApplication.APP_CONFIGURATION_V2));
setText(MSGS.tabConfiguration());

if (!gwtDevice.isOnline()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.kapua.app.console.module.api.setting.ConsoleSetting;
import org.eclipse.kapua.app.console.module.api.setting.ConsoleSettingKeys;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigComponent;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigComponentCreator;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigParameter;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigParameter.GwtConfigParameterType;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtXSRFToken;
Expand Down Expand Up @@ -437,6 +438,27 @@ public void updateComponentConfiguration(GwtXSRFToken xsrfToken, GwtDevice gwtDe
throw KapuaExceptionHandler.buildExceptionFromError(t);
}
}

@Override
public void createComponentConfiguration(GwtXSRFToken xsrfToken, GwtDevice gwtDevice, GwtConfigComponentCreator command) throws GwtKapuaException {
// Checking validity of the given XSRF Token
checkXSRFToken(xsrfToken);

// execute the update
try {
KapuaId scopeId = KapuaEid.parseCompactId(gwtDevice.getScopeId());
KapuaId deviceId = KapuaEid.parseCompactId(gwtDevice.getId());

CONFIGURATION_MANAGEMENT_SERVICE.create(scopeId, deviceId, command.getUnescapedComponentFactoryId(), command.getUnescapedComponentId(), null);
// Add an additional delay after the configuration update
// to give the time to the device to apply the received
// configuration
Thread.sleep(1000);
} catch (Throwable t) {
throw KapuaExceptionHandler.buildExceptionFromError(t);
}
}

// Configuration Store Settings
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class GwtDevice extends GwtUpdatableEntityModel implements Serializable {

public enum GwtDeviceApplication implements IsSerializable {

APP_CONFIGURATION("CONF-V1"), //
APP_CONFIGURATION_V1("CONF-V1"), //
APP_CONFIGURATION_V2("CONF-V2"), //
APP_COMMAND("CMD-V1"), //
APP_DEPLOY_V1("DEPLOY-V1"), //
APP_DEPLOY_V2("DEPLOY-V2"), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaException;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigComponent;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigComponentCreator;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtXSRFToken;
import org.eclipse.kapua.app.console.module.device.shared.model.GwtDevice;
import org.eclipse.kapua.app.console.module.device.shared.model.management.bundles.GwtBundle;
Expand Down Expand Up @@ -91,6 +92,8 @@ List<GwtConfigComponent> findDeviceConfigurations(GwtDevice device)
void updateComponentConfiguration(GwtXSRFToken xsrfToken, GwtDevice device, GwtConfigComponent configComponent)
throws GwtKapuaException;

void createComponentConfiguration(GwtXSRFToken xsrfToken, GwtDevice device, GwtConfigComponentCreator creator) throws GwtKapuaException;

boolean isStoreServiceEnabled(String scopeIdString, String deviceIdString) throws GwtKapuaException;

GwtDeviceConfigurationStoreSettings getApplicationSettings(String scopeId, String deviceId) throws GwtKapuaException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,9 @@ dialogDeviceTagDeleteError=Error while removing tag: {0}
dialogDeviceDeleteError=Error when deleting device: {0}
deviceConnectionError=Selected device not connected. Please refresh device list.
dialogDeviceAddInfoMessage=Create a new device providing Client ID, Display Name, Access Group and Device Status.
# Add device config component
dialogDeviceConfigComponentAddHeader=New Configuration
dialogDeviceConfigComponentAddInfo=Create a new device component configuration.
dialogDeviceConfigComponentAddConfirmation=Configuration successfully created.
dialogDeviceConfigComponentAddComponentFactoryIdFieldName=Component Factory ID
dialogDeviceConfigComponentAddComponentIdFieldName=Component ID
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ public enum ConfigurationMetrics implements DeviceAppMetrics {
APP_ID("CONF"),

/**
* Application version.
* Application version 1.
*
* @since 1.0.0
*/
APP_VERSION("V1"),
APP_VERSION_1("V1"),

/**
* Application version2.
*
* @since 1.6.10
*/
APP_VERSION_2("V2"),
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ DeviceConfiguration get(KapuaId scopeId, KapuaId deviceId,
Long timeout)
throws KapuaException;

void create(KapuaId scopeId, KapuaId deviceId, String componentFactoryId, String componentId, Long timeout) throws KapuaException;

/**
* Put the provided configuration to the device identified by the provided device identifier
*
Expand Down
Loading

0 comments on commit 2a1023f

Please sign in to comment.