Search in sources :

Example 1 with DeviceEventService

use of org.eclipse.kapua.service.device.registry.event.DeviceEventService in project kapua by eclipse.

the class DeviceBundleManagementServiceImpl method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceBundles get(KapuaId scopeId, KapuaId deviceId, 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
    BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
    bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
    bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
    bundleRequestChannel.setMethod(KapuaMethod.READ);
    BundleRequestPayload bundleRequestPayload = new BundleRequestPayload();
    BundleRequestMessage bundleRequestMessage = new BundleRequestMessage();
    bundleRequestMessage.setScopeId(scopeId);
    bundleRequestMessage.setDeviceId(deviceId);
    bundleRequestMessage.setCapturedOn(new Date());
    bundleRequestMessage.setPayload(bundleRequestPayload);
    bundleRequestMessage.setChannel(bundleRequestChannel);
    // 
    // Do get
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(bundleRequestMessage, timeout);
    BundleResponseMessage responseMessage = (BundleResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    BundleResponsePayload responsePayload = responseMessage.getPayload();
    DeviceManagementSetting config = DeviceManagementSetting.getInstance();
    String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
    String body = null;
    try {
        body = new String(responsePayload.getBody(), charEncoding);
    } catch (Exception e) {
        throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
    }
    DeviceBundles deviceBundleList = null;
    try {
        deviceBundleList = XmlUtil.unmarshal(body, DeviceBundlesImpl.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(), DeviceBundleAppProperties.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 deviceBundleList;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceBundles(org.eclipse.kapua.service.device.management.bundle.DeviceBundles) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) BundleResponsePayload(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponsePayload) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) KapuaException(org.eclipse.kapua.KapuaException) BundleRequestMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestMessage) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) 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) BundleRequestChannel(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) BundleRequestPayload(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestPayload) BundleResponseMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)

Example 2 with DeviceEventService

use of org.eclipse.kapua.service.device.registry.event.DeviceEventService 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 3 with DeviceEventService

use of org.eclipse.kapua.service.device.registry.event.DeviceEventService 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 4 with DeviceEventService

use of org.eclipse.kapua.service.device.registry.event.DeviceEventService in project kapua by eclipse.

the class DeviceLifeCycleServiceImpl method death.

@Override
public void death(KapuaId connectionId, KapuaDisconnectMessage message) throws KapuaException {
    KapuaPayload payload = message.getPayload();
    KapuaId scopeId = message.getScopeId();
    KapuaId deviceId = message.getDeviceId();
    // 
    // Device update
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = deviceRegistryService.find(scopeId, deviceId);
    // 
    // Event create
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, message.getReceivedOn(), "DEATH");
    deviceEventCreator.setReceivedOn(message.getReceivedOn());
    // TODO check this change
    deviceEventCreator.setResponseCode(KapuaResponseCode.ACCEPTED);
    deviceEventCreator.setSentOn(message.getSentOn());
    KapuaPosition position = message.getPosition();
    if (position != null) {
        deviceEventCreator.setPosition(position);
    }
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaPosition(org.eclipse.kapua.message.KapuaPosition) Device(org.eclipse.kapua.service.device.registry.Device) KapuaPayload(org.eclipse.kapua.message.KapuaPayload) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Example 5 with DeviceEventService

use of org.eclipse.kapua.service.device.registry.event.DeviceEventService in project kapua by eclipse.

the class DeviceLifeCycleServiceImpl method missing.

@Override
public void missing(KapuaId connectionId, KapuaMissingMessage message) throws KapuaException {
    KapuaPayload payload = message.getPayload();
    KapuaId scopeId = message.getScopeId();
    KapuaId deviceId = message.getDeviceId();
    // 
    // Device update
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = deviceRegistryService.find(scopeId, deviceId);
    // 
    // Event create
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, device.getId(), message.getReceivedOn(), "MISSING");
    deviceEventCreator.setEventMessage(payload.toDisplayString());
    // TODO check this change
    deviceEventCreator.setResponseCode(KapuaResponseCode.ACCEPTED);
    deviceEventCreator.setSentOn(message.getReceivedOn());
    KapuaPosition position = message.getPosition();
    if (position != null) {
        deviceEventCreator.setPosition(position);
    }
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaPosition(org.eclipse.kapua.message.KapuaPosition) Device(org.eclipse.kapua.service.device.registry.Device) KapuaPayload(org.eclipse.kapua.message.KapuaPayload) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)23 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)23 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)23 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)21 Date (java.util.Date)17 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)17 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)17 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)17 KapuaId (org.eclipse.kapua.model.id.KapuaId)9 KapuaException (org.eclipse.kapua.KapuaException)8 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)8 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)8 PackageRequestChannel (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel)8 PackageRequestMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage)8 PackageRequestPayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)8 PackageResponseMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage)8 Device (org.eclipse.kapua.service.device.registry.Device)5 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)5 KapuaPosition (org.eclipse.kapua.message.KapuaPosition)4 PackageResponsePayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload)4