Search in sources :

Example 1 with SnapshotResponsePayload

use of org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload in project kapua by eclipse.

the class TranslatorAppSnapshotKuraKapua method translate.

@Override
public SnapshotResponseMessage translate(KuraResponseMessage kuraMessage) throws KapuaException {
    // 
    // Kura channel
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraMessage.getChannel().getScope());
    SnapshotResponseChannel commandResponseChannel = translate(kuraMessage.getChannel());
    // 
    // Kura payload
    SnapshotResponsePayload responsePayload = translate(kuraMessage.getPayload());
    // 
    // Kura Message
    SnapshotResponseMessage kapuaMessage = new SnapshotResponseMessage();
    kapuaMessage.setScopeId(account.getId());
    kapuaMessage.setChannel(commandResponseChannel);
    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 : SnapshotResponsePayload(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) SnapshotResponseChannel(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseChannel) SnapshotResponseMessage(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseMessage) AccountService(org.eclipse.kapua.service.account.AccountService)

Example 2 with SnapshotResponsePayload

use of org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload in project kapua by eclipse.

the class DeviceSnapshotManagementServiceImpl method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceSnapshots 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
    SnapshotRequestChannel snapshotRequestChannel = new SnapshotRequestChannel();
    snapshotRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
    snapshotRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
    snapshotRequestChannel.setMethod(KapuaMethod.READ);
    SnapshotRequestPayload snapshotRequestPayload = new SnapshotRequestPayload();
    SnapshotRequestMessage snapshotRequestMessage = new SnapshotRequestMessage();
    snapshotRequestMessage.setScopeId(scopeId);
    snapshotRequestMessage.setDeviceId(deviceId);
    snapshotRequestMessage.setCapturedOn(new Date());
    snapshotRequestMessage.setPayload(snapshotRequestPayload);
    snapshotRequestMessage.setChannel(snapshotRequestChannel);
    // 
    // Do get
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(snapshotRequestMessage, timeout);
    SnapshotResponseMessage responseMessage = (SnapshotResponseMessage) deviceApplicationCall.send();
    SnapshotResponsePayload 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());
    }
    DeviceSnapshots deviceSnapshots = null;
    try {
        deviceSnapshots = XmlUtil.unmarshal(body, DeviceSnapshotsImpl.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(), DeviceSnapshotAppProperties.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 deviceSnapshots;
}
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) SnapshotResponseMessage(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseMessage) SnapshotRequestPayload(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestPayload) SnapshotRequestMessage(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestMessage) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) KapuaException(org.eclipse.kapua.KapuaException) SnapshotRequestChannel(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestChannel) SnapshotResponsePayload(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload) 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) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) DeviceSnapshots(org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots)

Example 3 with SnapshotResponsePayload

use of org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload in project kapua by eclipse.

the class TranslatorAppSnapshotKuraKapua method translate.

private SnapshotResponsePayload translate(KuraResponsePayload kuraPayload) throws KapuaException {
    SnapshotResponsePayload snapshotResponsePayload = new SnapshotResponsePayload();
    snapshotResponsePayload.setExceptionMessage((String) kuraPayload.getMetrics().get(ResponseMetrics.RESP_METRIC_EXCEPTION_MESSAGE.getValue()));
    snapshotResponsePayload.setExceptionStack((String) kuraPayload.getMetrics().get(ResponseMetrics.RESP_METRIC_EXCEPTION_STACK.getValue()));
    DeviceManagementSetting config = DeviceManagementSetting.getInstance();
    String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
    KuraSnapshotIds snapshotIdResult = null;
    if (kuraPayload.getBody() != null) {
        String body = null;
        try {
            body = new String(kuraPayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, snapshotResponsePayload.getBody());
        }
        try {
            snapshotIdResult = XmlUtil.unmarshal(body, KuraSnapshotIds.class);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
        }
    }
    translateBody(snapshotResponsePayload, charEncoding, snapshotIdResult);
    // Return Kapua Payload
    return snapshotResponsePayload;
}
Also used : SnapshotResponsePayload(org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload) KuraSnapshotIds(org.eclipse.kapua.service.device.call.kura.model.snapshot.KuraSnapshotIds) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

SnapshotResponsePayload (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponsePayload)3 KapuaException (org.eclipse.kapua.KapuaException)2 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)2 SnapshotResponseMessage (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseMessage)2 Date (java.util.Date)1 Account (org.eclipse.kapua.service.account.Account)1 AccountService (org.eclipse.kapua.service.account.AccountService)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 KuraSnapshotIds (org.eclipse.kapua.service.device.call.kura.model.snapshot.KuraSnapshotIds)1 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)1 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)1 SnapshotRequestChannel (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestChannel)1 SnapshotRequestMessage (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestMessage)1 SnapshotRequestPayload (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestPayload)1 SnapshotResponseChannel (org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotResponseChannel)1 DeviceSnapshots (org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots)1 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)1 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)1