Search in sources :

Example 1 with DeviceConfiguration

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration in project kapua by eclipse.

the class DeviceConfigurationManagementServiceImpl method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceConfiguration get(KapuaId scopeId, KapuaId deviceId, String configurationId, String configurationComponentPid, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomain.DEVICE_MANAGEMENT, Actions.read, scopeId));
    // 
    // Prepare the request
    ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
    configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
    configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
    configurationRequestChannel.setMethod(KapuaMethod.READ);
    configurationRequestChannel.setConfigurationId(configurationId);
    configurationRequestChannel.setComponentId(configurationComponentPid);
    ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
    ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
    configurationRequestMessage.setScopeId(scopeId);
    configurationRequestMessage.setDeviceId(deviceId);
    configurationRequestMessage.setCapturedOn(new Date());
    configurationRequestMessage.setPayload(configurationRequestPayload);
    configurationRequestMessage.setChannel(configurationRequestChannel);
    // 
    // Do get
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(configurationRequestMessage, timeout);
    ConfigurationResponseMessage responseMessage = (ConfigurationResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    ConfigurationResponsePayload responsePayload = responseMessage.getPayload();
    DeviceManagementSetting config = DeviceManagementSetting.getInstance();
    String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
    DeviceConfiguration deviceConfiguration = null;
    if (responsePayload.getBody() != null) {
        String body = null;
        try {
            body = new String(responsePayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
        }
        try {
            deviceConfiguration = XmlUtil.unmarshal(body, DeviceConfigurationImpl.class);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
        }
    }
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceConfigurationAppProperties.APP_NAME.getValue());
    deviceEventCreator.setPosition(responseMessage.getPosition());
    deviceEventCreator.setSentOn(responseMessage.getSentOn());
    deviceEventCreator.setAction(KapuaMethod.READ);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
    return deviceConfiguration;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) ConfigurationResponsePayload(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponsePayload) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) SAXException(org.xml.sax.SAXException) KapuaException(org.eclipse.kapua.KapuaException) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) ConfigurationRequestMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestMessage) ConfigurationRequestChannel(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) ConfigurationRequestPayload(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestPayload) ConfigurationResponseMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponseMessage) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration)

Example 2 with DeviceConfiguration

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration in project kapua by eclipse.

the class DeviceConfigurationManagementServiceImpl method put.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void put(KapuaId scopeId, KapuaId deviceId, DeviceComponentConfiguration deviceComponentConfiguration, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notNull(deviceComponentConfiguration, "componentConfiguration");
    ArgumentValidator.notEmptyOrNull(deviceComponentConfiguration.getId(), "componentConfiguration.componentId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomain.DEVICE_MANAGEMENT, Actions.write, scopeId));
    // 
    // Prepare the request
    ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
    configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
    configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
    configurationRequestChannel.setMethod(KapuaMethod.WRITE);
    configurationRequestChannel.setComponentId(deviceComponentConfiguration.getId());
    ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
    try {
        DeviceConfigurationFactory deviceConfigurationFactory = locator.getFactory(DeviceConfigurationFactory.class);
        DeviceConfiguration deviceConfiguration = deviceConfigurationFactory.newConfigurationInstance();
        deviceConfiguration.getComponentConfigurations().add(deviceComponentConfiguration);
        DeviceManagementSetting deviceManagementConfig = DeviceManagementSetting.getInstance();
        String charEncoding = deviceManagementConfig.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        StringWriter sw = new StringWriter();
        XmlUtil.marshal(deviceConfiguration, sw);
        byte[] requestBody = sw.toString().getBytes(charEncoding);
        configurationRequestPayload.setBody(requestBody);
        new String(configurationRequestPayload.getBody());
    } catch (Exception e) {
        throw new DeviceManagementException(DeviceManagementErrorCodes.REQUEST_EXCEPTION, e, deviceComponentConfiguration);
    }
    ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
    configurationRequestMessage.setScopeId(scopeId);
    configurationRequestMessage.setDeviceId(deviceId);
    configurationRequestMessage.setCapturedOn(new Date());
    configurationRequestMessage.setPayload(configurationRequestPayload);
    configurationRequestMessage.setChannel(configurationRequestChannel);
    // 
    // Do put
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(configurationRequestMessage, timeout);
    ConfigurationResponseMessage responseMessage = (ConfigurationResponseMessage) deviceApplicationCall.send();
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceConfigurationAppProperties.APP_NAME.getValue());
    deviceEventCreator.setPosition(responseMessage.getPosition());
    deviceEventCreator.setSentOn(responseMessage.getSentOn());
    deviceEventCreator.setAction(KapuaMethod.WRITE);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) SAXException(org.xml.sax.SAXException) KapuaException(org.eclipse.kapua.KapuaException) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) ConfigurationRequestMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestMessage) ConfigurationRequestChannel(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel) StringWriter(java.io.StringWriter) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) ConfigurationRequestPayload(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestPayload) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration) ConfigurationResponseMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponseMessage) DeviceConfigurationFactory(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory)

Example 3 with DeviceConfiguration

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method findDeviceConfigurations.

// 
// Configurations
// 
@Override
public List<GwtConfigComponent> findDeviceConfigurations(GwtDevice device) throws GwtKapuaException {
    List<GwtConfigComponent> gwtConfigs = new ArrayList<GwtConfigComponent>();
    try {
        // get the configuration
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceConfigurationManagementService deviceConfiguratiomManagementService = locator.getService(DeviceConfigurationManagementService.class);
        KapuaId scopeId = KapuaEid.parseShortId(device.getScopeId());
        KapuaId deviceId = KapuaEid.parseShortId(device.getId());
        DeviceConfiguration deviceConfigurations = deviceConfiguratiomManagementService.get(scopeId, deviceId, null, null, null);
        if (deviceConfigurations != null) {
            // sort the list alphabetically by service name
            List<DeviceComponentConfiguration> configs = deviceConfigurations.getComponentConfigurations();
            Collections.sort(configs, new Comparator<DeviceComponentConfiguration>() {

                @Override
                public int compare(DeviceComponentConfiguration arg0, DeviceComponentConfiguration arg1) {
                    String name0 = arg0.getId();
                    String name1 = arg1.getId();
                    if (name0.contains(".")) {
                        name0 = name0.substring(name0.lastIndexOf('.'));
                    }
                    if (name1.contains(".")) {
                        name1 = name1.substring(name1.lastIndexOf('.'));
                    }
                    return name0.compareTo(name1);
                }
            });
            // prepare results
            ConsoleSetting consoleConfig = ConsoleSetting.getInstance();
            List<String> serviceIgnore = consoleConfig.getList(String.class, ConsoleSettingKeys.DEVICE_CONFIGURATION_SERVICE_IGNORE);
            for (DeviceComponentConfiguration config : deviceConfigurations.getComponentConfigurations()) {
                // ignore items we want to hide
                if (serviceIgnore != null && serviceIgnore.contains(config.getId())) {
                    continue;
                }
                KapuaTocd ocd = config.getDefinition();
                if (ocd != null) {
                    GwtConfigComponent gwtConfig = new GwtConfigComponent();
                    gwtConfig.setId(config.getId());
                    gwtConfig.setName(ocd.getName());
                    gwtConfig.setDescription(ocd.getDescription());
                    if (ocd.getIcon() != null && ocd.getIcon().size() > 0) {
                        KapuaTicon icon = ocd.getIcon().get(0);
                        checkIconResource(icon);
                        gwtConfig.setComponentIcon(icon.getResource());
                    }
                    List<GwtConfigParameter> gwtParams = new ArrayList<GwtConfigParameter>();
                    gwtConfig.setParameters(gwtParams);
                    for (KapuaTad ad : ocd.getAD()) {
                        if (ad != null) {
                            GwtConfigParameter gwtParam = new GwtConfigParameter();
                            gwtParam.setId(ad.getId());
                            gwtParam.setName(ad.getName());
                            gwtParam.setDescription(ad.getDescription());
                            gwtParam.setType(GwtConfigParameterType.fromString(ad.getType().value()));
                            gwtParam.setRequired(ad.isRequired());
                            gwtParam.setCardinality(ad.getCardinality());
                            if (ad.getOption() != null && ad.getOption().size() > 0) {
                                Map<String, String> options = new HashMap<String, String>();
                                for (KapuaToption option : ad.getOption()) {
                                    options.put(option.getLabel(), option.getValue());
                                }
                                gwtParam.setOptions(options);
                            }
                            gwtParam.setMin(ad.getMin());
                            gwtParam.setMax(ad.getMax());
                            if (config.getProperties() != null) {
                                // handle the value based on the cardinality of the attribute
                                int cardinality = ad.getCardinality();
                                Object value = config.getProperties().get(ad.getId());
                                if (value != null) {
                                    if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
                                        gwtParam.setValue(value.toString());
                                    } else {
                                        // this could be an array value
                                        if (value instanceof Object[]) {
                                            Object[] objValues = (Object[]) value;
                                            List<String> strValues = new ArrayList<String>();
                                            for (Object v : objValues) {
                                                if (v != null) {
                                                    strValues.add(v.toString());
                                                }
                                            }
                                            gwtParam.setValues(strValues.toArray(new String[] {}));
                                        }
                                    }
                                }
                                gwtParams.add(gwtParam);
                            }
                        }
                    }
                    gwtConfigs.add(gwtConfig);
                }
            }
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtConfigs;
}
Also used : KapuaTad(org.eclipse.kapua.model.config.metatype.KapuaTad) KapuaToption(org.eclipse.kapua.model.config.metatype.KapuaToption) KapuaTicon(org.eclipse.kapua.model.config.metatype.KapuaTicon) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsoleSetting(org.eclipse.kapua.app.console.setting.ConsoleSetting) KapuaId(org.eclipse.kapua.model.id.KapuaId) GwtConfigComponent(org.eclipse.kapua.app.console.shared.model.GwtConfigComponent) DeviceComponentConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration) DeviceConfigurationManagementService(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtConfigParameter(org.eclipse.kapua.app.console.shared.model.GwtConfigParameter) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration)

Example 4 with DeviceConfiguration

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration in project kapua by eclipse.

the class DeviceSnapshotsServlet method doGet.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    PrintWriter writer = response.getWriter();
    try {
        // parameter extraction
        String account = request.getParameter("scopeId");
        String clientId = request.getParameter("deviceId");
        String snapshotId = request.getParameter("snapshotId");
        // 
        // get the devices and append them to the exporter
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceConfigurationManagementService deviceConfigurationManagementService = locator.getService(DeviceConfigurationManagementService.class);
        DeviceConfiguration conf = deviceConfigurationManagementService.get(KapuaEid.parseShortId(account), KapuaEid.parseShortId(clientId), snapshotId, null, null);
        String contentDispositionFormat = "attachment; filename*=UTF-8''snapshot_%s_%s_%s.xml; ";
        response.setContentType("application/xml; charset=UTF-8");
        response.setHeader("Cache-Control", "no-transform, max-age=0");
        response.setHeader("Content-Disposition", String.format(contentDispositionFormat, URLEncoder.encode(account, "UTF-8"), URLEncoder.encode(clientId, "UTF-8"), snapshotId));
        XmlUtil.marshal(conf, writer);
    } catch (Exception e) {
        s_logger.error("Error creating Excel export", e);
        throw new ServletException(e);
    } finally {
        if (writer != null)
            writer.close();
    }
}
Also used : ServletException(javax.servlet.ServletException) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) DeviceConfigurationManagementService(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)

Example 5 with DeviceConfiguration

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration in project kapua by eclipse.

the class Devices method getConfigurations.

/**
 * Returns the configuration of a device or the configuration of the OSGi component
 * identified with specified PID (service's persistent identity).
 * In the OSGi framework, the service's persistent identity is defined as the name attribute of the
 * Component Descriptor XML file; at runtime, the same value is also available
 * in the component.name and in the service.pid attributes of the Component Configuration.
 *
 * @param deviceId
 *            The id of the device
 * @param componentId
 *            An optional id of the component to get the configuration for
 * @return The requested configurations
 */
@GET
@Path("{deviceId}/configurations")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DeviceConfiguration getConfigurations(@PathParam("deviceId") String deviceId, @QueryParam("componentId") String componentId) {
    DeviceConfiguration deviceConfiguration = null;
    try {
        KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
        KapuaId id = KapuaEid.parseShortId(deviceId);
        deviceConfiguration = configurationService.get(scopeId, id, null, componentId, null);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(deviceConfiguration);
}
Also used : KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration)

Aggregations

DeviceConfiguration (org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration)6 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)4 KapuaException (org.eclipse.kapua.KapuaException)3 Date (java.util.Date)2 JAXBException (javax.xml.bind.JAXBException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)2 KapuaId (org.eclipse.kapua.model.id.KapuaId)2 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)2 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)2 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)2 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)2 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)2 DeviceConfigurationManagementService (org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)2 ConfigurationRequestChannel (org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel)2 ConfigurationRequestMessage (org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestMessage)2 ConfigurationRequestPayload (org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestPayload)2 ConfigurationResponseMessage (org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponseMessage)2 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)2 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)2