Search in sources :

Example 1 with PackageResponsePayload

use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload in project kapua by eclipse.

the class TranslatorAppPackageKuraKapua method translate.

@Override
public PackageResponseMessage translate(KuraResponseMessage kuraMessage) throws KapuaException {
    // 
    // Kura channel
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraMessage.getChannel().getScope());
    PackageResponseChannel responseChannel = translate(kuraMessage.getChannel());
    // 
    // Kura payload
    PackageResponsePayload responsePayload = translate(kuraMessage.getPayload());
    // 
    // Kura Message
    PackageResponseMessage kapuaMessage = new PackageResponseMessage();
    kapuaMessage.setScopeId(account.getId());
    kapuaMessage.setChannel(responseChannel);
    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 : BigInteger(java.math.BigInteger) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) PackageResponseMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage) PackageResponseChannel(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseChannel) AccountService(org.eclipse.kapua.service.account.AccountService)

Example 2 with PackageResponsePayload

use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload in project kapua by eclipse.

the class TranslatorAppPackageKuraKapua method translate.

private PackageResponsePayload translate(KuraResponsePayload kuraResponsePayload) throws KapuaException {
    PackageResponsePayload responsePayload = new PackageResponsePayload();
    Map<String, Object> metrics = kuraResponsePayload.getMetrics();
    responsePayload.setExceptionMessage((String) metrics.get(ResponseMetrics.RESP_METRIC_EXCEPTION_MESSAGE.getValue()));
    responsePayload.setExceptionStack((String) metrics.get(ResponseMetrics.RESP_METRIC_EXCEPTION_STACK.getValue()));
    if (metrics.get(PackageMetrics.APP_METRIC_PACKAGE_OPERATION_ID.getValue()) != null) {
        responsePayload.setPackageDownloadOperationId(new KapuaEid(new BigInteger(metrics.get(PackageMetrics.APP_METRIC_PACKAGE_OPERATION_ID.getValue()).toString())));
    }
    if (metrics.get(PackageMetrics.APP_METRIC_PACKAGE_DOWNLOAD_STATUS.getValue()) != null) {
        responsePayload.setPackageDownloadOperationStatus(DevicePackageDownloadStatus.valueOf((String) metrics.get(PackageMetrics.APP_METRIC_PACKAGE_DOWNLOAD_STATUS.getValue())));
    }
    responsePayload.setPackageDownloadOperationSize((Integer) metrics.get(PackageMetrics.APP_METRIC_PACKAGE_DOWNLOAD_SIZE.getValue()));
    responsePayload.setPackageDownloadOperationProgress((Integer) metrics.get(PackageMetrics.APP_METRIC_PACKAGE_DOWNLOAD_PROGRESS.getValue()));
    String body;
    if (kuraResponsePayload.getBody() != null) {
        DeviceManagementSetting config = DeviceManagementSetting.getInstance();
        String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        try {
            body = new String(kuraResponsePayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new // 
            TranslatorException(// 
            TranslatorErrorCodes.INVALID_PAYLOAD, // 
            e, // 
            kuraResponsePayload.getBody());
        }
        KuraDeploymentPackages kuraDeploymentPackages = null;
        try {
            kuraDeploymentPackages = XmlUtil.unmarshal(body, KuraDeploymentPackages.class);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
        }
        translate(responsePayload, charEncoding, kuraDeploymentPackages);
    }
    // Return Kapua Payload
    return responsePayload;
}
Also used : PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) KuraDeploymentPackages(org.eclipse.kapua.service.device.call.kura.model.deploy.KuraDeploymentPackages) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) BigInteger(java.math.BigInteger) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException)

Example 3 with PackageResponsePayload

use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload in project kapua by eclipse.

the class DevicePackageManagementServiceImpl method getInstalled.

@Override
public DevicePackages getInstalled(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
    PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
    packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
    packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
    packageRequestChannel.setMethod(KapuaMethod.READ);
    packageRequestChannel.setPackageResource(null);
    PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
    PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
    packageRequestMessage.setScopeId(scopeId);
    packageRequestMessage.setDeviceId(deviceId);
    packageRequestMessage.setCapturedOn(new Date());
    packageRequestMessage.setPayload(packageRequestPayload);
    packageRequestMessage.setChannel(packageRequestChannel);
    // 
    // Do get
    @SuppressWarnings({ "rawtypes", "unchecked" }) DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(packageRequestMessage, timeout);
    PackageResponseMessage responseMessage = (PackageResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    PackageResponsePayload responsePayload = responseMessage.getPayload();
    DevicePackages devicePackages;
    if (responsePayload.getBody() != null) {
        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());
        }
        try {
            devicePackages = XmlUtil.unmarshal(body, DevicePackagesImpl.class);
        } catch (Exception e) {
            throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
        }
    } else {
        devicePackages = new DevicePackagesImpl();
    }
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), PackageAppProperties.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 devicePackages;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PackageResponseMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage) DevicePackagesImpl(org.eclipse.kapua.service.device.management.packages.model.internal.DevicePackagesImpl) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) 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) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages) PackageRequestChannel(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PackageRequestMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage) 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) PackageRequestPayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)

Example 4 with PackageResponsePayload

use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload in project kapua by eclipse.

the class DevicePackageManagementServiceImpl method installStatus.

@Override
public DevicePackageInstallOperation installStatus(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
    PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
    packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
    packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
    packageRequestChannel.setMethod(KapuaMethod.READ);
    packageRequestChannel.setPackageResource(PackageResource.INSTALL);
    PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
    PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
    packageRequestMessage.setScopeId(scopeId);
    packageRequestMessage.setDeviceId(deviceId);
    packageRequestMessage.setCapturedOn(new Date());
    packageRequestMessage.setPayload(packageRequestPayload);
    packageRequestMessage.setChannel(packageRequestChannel);
    // 
    // Do get
    @SuppressWarnings({ "rawtypes", "unchecked" }) DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(packageRequestMessage, timeout);
    PackageResponseMessage responseMessage = (PackageResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    PackageResponsePayload 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());
    }
    DevicePackageInstallOperation installOperation = null;
    try {
        installOperation = XmlUtil.unmarshal(body, DevicePackageInstallOperationImpl.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(), PackageAppProperties.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 installOperation;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PackageResponseMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) 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) PackageRequestChannel(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) DevicePackageInstallOperation(org.eclipse.kapua.service.device.management.packages.model.install.DevicePackageInstallOperation) PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PackageRequestMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage) 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) DevicePackageInstallOperationImpl(org.eclipse.kapua.service.device.management.packages.model.install.internal.DevicePackageInstallOperationImpl) PackageRequestPayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)

Example 5 with PackageResponsePayload

use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload in project kapua by eclipse.

the class DevicePackageManagementServiceImpl method downloadStatus.

@Override
public DevicePackageDownloadOperation downloadStatus(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
    PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
    packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
    packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
    packageRequestChannel.setMethod(KapuaMethod.READ);
    packageRequestChannel.setPackageResource(PackageResource.DOWNLOAD);
    PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
    PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
    packageRequestMessage.setScopeId(scopeId);
    packageRequestMessage.setDeviceId(deviceId);
    packageRequestMessage.setCapturedOn(new Date());
    packageRequestMessage.setPayload(packageRequestPayload);
    packageRequestMessage.setChannel(packageRequestChannel);
    // 
    // Do get
    @SuppressWarnings({ "rawtypes", "unchecked" }) DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(packageRequestMessage, timeout);
    PackageResponseMessage responseMessage = (PackageResponseMessage) deviceApplicationCall.send();
    // 
    // Parse the response
    PackageResponsePayload responsePayload = responseMessage.getPayload();
    DevicePackageDownloadOperation downloadOperation = new DevicePackageDownloadOperationImpl();
    downloadOperation.setId(responsePayload.getPackageDownloadOperationId());
    downloadOperation.setStatus(responsePayload.getPackageDownloadOperationStatus());
    downloadOperation.setSize(responsePayload.getPackageDownloadOperationSize());
    downloadOperation.setProgress(responsePayload.getPackageDownloadOperationProgress());
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), PackageAppProperties.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 downloadOperation;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DevicePackageDownloadOperation(org.eclipse.kapua.service.device.management.packages.model.download.DevicePackageDownloadOperation) PackageResponseMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) Date(java.util.Date) PackageRequestChannel(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) PackageResponsePayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PackageRequestMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage) DevicePackageDownloadOperationImpl(org.eclipse.kapua.service.device.management.packages.model.download.internal.DevicePackageDownloadOperationImpl) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) PackageRequestPayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)

Aggregations

PackageResponsePayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponsePayload)6 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)5 PackageResponseMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage)5 Date (java.util.Date)4 KapuaException (org.eclipse.kapua.KapuaException)4 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)4 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)4 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)4 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)4 PackageRequestChannel (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel)4 PackageRequestMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage)4 PackageRequestPayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)4 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)4 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)4 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)4 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)3 BigInteger (java.math.BigInteger)2 KapuaEid (org.eclipse.kapua.commons.model.id.KapuaEid)1 Account (org.eclipse.kapua.service.account.Account)1 AccountService (org.eclipse.kapua.service.account.AccountService)1