Search in sources :

Example 26 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator 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 27 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator 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 28 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator 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 29 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class TranslatorLifeAppsKuraKapua method translate.

@Override
public KapuaAppsMessage translate(KuraAppsMessage kuraAppsMessage) throws KapuaException {
    KapuaAppsMessage kapuaAppsMessage = new KapuaAppsMessageImpl();
    kapuaAppsMessage.setChannel(translate(kuraAppsMessage.getChannel()));
    kapuaAppsMessage.setPayload(translate(kuraAppsMessage.getPayload()));
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraAppsMessage.getChannel().getScope());
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = deviceRegistryService.findByClientId(account.getId(), kuraAppsMessage.getChannel().getClientId());
    if (device == null) {
        throw new KapuaEntityNotFoundException(Device.class.toString(), kuraAppsMessage.getChannel().getClientId());
    }
    kapuaAppsMessage.setDeviceId(device.getId());
    kapuaAppsMessage.setScopeId(account.getId());
    kapuaAppsMessage.setCapturedOn(kuraAppsMessage.getPayload().getTimestamp());
    kapuaAppsMessage.setSentOn(kuraAppsMessage.getPayload().getTimestamp());
    kapuaAppsMessage.setReceivedOn(kuraAppsMessage.getTimestamp());
    kapuaAppsMessage.setPosition(TranslatorKuraKapuaUtils.translate(kuraAppsMessage.getPayload().getPosition()));
    return kapuaAppsMessage;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) KapuaAppsMessageImpl(org.eclipse.kapua.message.internal.device.lifecycle.KapuaAppsMessageImpl) Device(org.eclipse.kapua.service.device.registry.Device) KapuaAppsMessage(org.eclipse.kapua.message.device.lifecycle.KapuaAppsMessage) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) AccountService(org.eclipse.kapua.service.account.AccountService) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException)

Example 30 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class TranslatorLifeBirthKuraKapua method translate.

@Override
public KapuaBirthMessage translate(KuraBirthMessage kuraBirthMessage) throws KapuaException {
    KapuaBirthMessage kapuaBirthMessage = new KapuaBirthMessageImpl();
    kapuaBirthMessage.setChannel(translate(kuraBirthMessage.getChannel()));
    kapuaBirthMessage.setPayload(translate(kuraBirthMessage.getPayload()));
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraBirthMessage.getChannel().getScope());
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = deviceRegistryService.findByClientId(account.getId(), kuraBirthMessage.getChannel().getClientId());
    kapuaBirthMessage.setScopeId(account.getId());
    if (device != null) {
        kapuaBirthMessage.setDeviceId(device.getId());
    } else {
        kapuaBirthMessage.setClientId(kuraBirthMessage.getChannel().getClientId());
    }
    kapuaBirthMessage.setCapturedOn(kuraBirthMessage.getPayload().getTimestamp());
    kapuaBirthMessage.setSentOn(kuraBirthMessage.getPayload().getTimestamp());
    kapuaBirthMessage.setReceivedOn(kuraBirthMessage.getTimestamp());
    kapuaBirthMessage.setPosition(TranslatorKuraKapuaUtils.translate(kuraBirthMessage.getPayload().getPosition()));
    return kapuaBirthMessage;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) KapuaBirthMessage(org.eclipse.kapua.message.device.lifecycle.KapuaBirthMessage) KapuaBirthMessageImpl(org.eclipse.kapua.message.internal.device.lifecycle.KapuaBirthMessageImpl) Device(org.eclipse.kapua.service.device.registry.Device) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) AccountService(org.eclipse.kapua.service.account.AccountService)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)150 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)61 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)58 KapuaId (org.eclipse.kapua.model.id.KapuaId)54 KapuaException (org.eclipse.kapua.KapuaException)52 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)42 Account (org.eclipse.kapua.service.account.Account)37 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)36 AccountService (org.eclipse.kapua.service.account.AccountService)34 Date (java.util.Date)30 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)23 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)23 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)23 Device (org.eclipse.kapua.service.device.registry.Device)22 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)21 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)17 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)11 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)10