Search in sources :

Example 1 with BundleResponseMessage

use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage in project kapua by eclipse.

the class TranslatorAppBundleKuraKapua method translate.

@Override
public BundleResponseMessage translate(KuraResponseMessage kuraMessage) throws KapuaException {
    // 
    // Kura channel
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraMessage.getChannel().getScope());
    BundleResponseChannel bundleResponseChannel = translate(kuraMessage.getChannel());
    // 
    // Kura payload
    BundleResponsePayload responsePayload = translate(kuraMessage.getPayload());
    // 
    // Kura Message
    BundleResponseMessage kapuaMessage = new BundleResponseMessage();
    kapuaMessage.setScopeId(account.getId());
    kapuaMessage.setChannel(bundleResponseChannel);
    kapuaMessage.setPayload(responsePayload);
    kapuaMessage.setCapturedOn(kuraMessage.getPayload().getTimestamp());
    kapuaMessage.setSentOn(kuraMessage.getPayload().getTimestamp());
    kapuaMessage.setReceivedOn(kuraMessage.getTimestamp());
    kapuaMessage.setResponseCode(TranslatorKuraKapuaUtils.translate((Integer) kuraMessage.getPayload().getMetrics().get(ResponseMetrics.RESP_METRIC_EXIT_CODE.getValue())));
    // Return Kapua Message
    return kapuaMessage;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) BundleResponsePayload(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponsePayload) BundleResponseChannel(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseChannel) AccountService(org.eclipse.kapua.service.account.AccountService) BundleResponseMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)

Example 2 with BundleResponseMessage

use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage 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 3 with BundleResponseMessage

use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage in project kapua by eclipse.

the class DeviceBundleManagementServiceImpl method stop.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void stop(KapuaId scopeId, KapuaId deviceId, String bundleId, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notEmptyOrNull(bundleId, "bundleID");
    // 
    // 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.execute, scopeId));
    // 
    // Prepare the request
    BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
    bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
    bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
    bundleRequestChannel.setMethod(KapuaMethod.EXECUTE);
    bundleRequestChannel.setStart(false);
    bundleRequestChannel.setBundleId(bundleId);
    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();
    // 
    // 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.EXECUTE);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) BundleRequestMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestMessage) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) 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) Date(java.util.Date) BundleResponseMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)

Example 4 with BundleResponseMessage

use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage in project kapua by eclipse.

the class DeviceBundleManagementServiceImpl method start.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void start(KapuaId scopeId, KapuaId deviceId, String bundleId, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notEmptyOrNull(bundleId, "bundleId");
    // 
    // 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.execute, scopeId));
    // 
    // Prepare the request
    BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
    bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
    bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
    bundleRequestChannel.setMethod(KapuaMethod.EXECUTE);
    bundleRequestChannel.setStart(true);
    bundleRequestChannel.setBundleId(bundleId);
    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();
    // 
    // 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.EXECUTE);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) BundleRequestMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestMessage) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) 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) Date(java.util.Date) BundleResponseMessage(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)4 BundleResponseMessage (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseMessage)4 Date (java.util.Date)3 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)3 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)3 BundleRequestChannel (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel)3 BundleRequestMessage (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestMessage)3 BundleRequestPayload (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestPayload)3 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)3 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)3 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)3 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)3 BundleResponsePayload (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponsePayload)2 KapuaException (org.eclipse.kapua.KapuaException)1 Account (org.eclipse.kapua.service.account.Account)1 AccountService (org.eclipse.kapua.service.account.AccountService)1 DeviceBundles (org.eclipse.kapua.service.device.management.bundle.DeviceBundles)1 BundleResponseChannel (org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponseChannel)1 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)1 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)1