Search in sources :

Example 71 with KapuaId

use of org.eclipse.kapua.model.id.KapuaId in project kapua by eclipse.

the class KapuaConverter method convertTo.

/**
 * Convert incoming message to a Kapua message (depending on messageType parameter)
 *
 * @param exchange
 * @param value
 * @param messageType expected incoming message type
 * @return Message container that contains message of asked type
 * @throws KapuaException if incoming message does not contain a javax.jms.BytesMessage or an error during conversion occurred
 */
private CamelKapuaMessage<?> convertTo(Exchange exchange, Object value, MESSAGE_TYPE messageType) throws KapuaException {
    // assume that the message is a Camel Jms message
    org.apache.camel.component.jms.JmsMessage message = (org.apache.camel.component.jms.JmsMessage) exchange.getIn();
    if ((Message) message.getJmsMessage() instanceof javax.jms.BytesMessage) {
        try {
            Date queuedOn = new Date(message.getHeader(CamelConstants.JMS_HEADER_TIMESTAMP, Long.class));
            KapuaId connectionId = (KapuaId) message.getHeader(MessageConstants.HEADER_KAPUA_CONNECTION_ID);
            ConnectorDescriptor connectorDescriptor = (ConnectorDescriptor) message.getHeader(MessageConstants.HEADER_KAPUA_CONNECTOR_DEVICE_PROTOCOL);
            return JmsUtil.convertToCamelKapuaMessage(connectorDescriptor, messageType, (byte[]) value, CamelUtil.getTopic(message), queuedOn, connectionId);
        } catch (JMSException e) {
            metricConverterErrorMessage.inc();
            logger.error("Exception converting message {}", e.getMessage(), e);
            throw KapuaException.internalError(e, "Cannot convert the message type " + exchange.getIn().getClass());
        }
    }
    metricConverterErrorMessage.inc();
    throw KapuaException.internalError("Cannot convert the message - Wrong instance type: " + exchange.getIn().getClass());
}
Also used : JMSException(javax.jms.JMSException) Date(java.util.Date) ConnectorDescriptor(org.eclipse.kapua.broker.core.plugin.ConnectorDescriptor) KapuaId(org.eclipse.kapua.model.id.KapuaId) JmsMessage(org.apache.camel.component.jms.JmsMessage) JmsMessage(org.apache.camel.component.jms.JmsMessage)

Example 72 with KapuaId

use of org.eclipse.kapua.model.id.KapuaId in project kapua by eclipse.

the class DevicePackageManagementServiceImpl method installExec.

@Override
public void installExec(KapuaId scopeId, KapuaId deviceId, DevicePackageInstallRequest deployInstallRequest, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notNull(deployInstallRequest, "deployInstallRequest");
    // 
    // 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));
    // 
    // Generate requestId
    IdGeneratorService idGeneratorService = locator.getService(IdGeneratorService.class);
    KapuaId operationId = idGeneratorService.generate();
    // 
    // Prepare the request
    PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
    packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
    packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
    packageRequestChannel.setMethod(KapuaMethod.EXECUTE);
    packageRequestChannel.setPackageResource(PackageResource.INSTALL);
    PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
    packageRequestPayload.setOperationId(operationId);
    packageRequestPayload.setPackageDownloadName(deployInstallRequest.getName());
    packageRequestPayload.setPackageDownloadVersion(deployInstallRequest.getVersion());
    packageRequestPayload.setReboot(deployInstallRequest.isReboot());
    packageRequestPayload.setRebootDelay(deployInstallRequest.getRebootDelay());
    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();
    // 
    // 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.EXECUTE);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
}
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) IdGeneratorService(org.eclipse.kapua.service.generator.id.IdGeneratorService) 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) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PackageRequestMessage(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) KapuaId(org.eclipse.kapua.model.id.KapuaId) PackageRequestPayload(org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)

Example 73 with KapuaId

use of org.eclipse.kapua.model.id.KapuaId in project kapua by eclipse.

the class DeviceLifeCycleServiceImpl method applications.

@Override
public void applications(KapuaId connectionId, KapuaAppsMessage 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(), "APPLICATION");
    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)

Example 74 with KapuaId

use of org.eclipse.kapua.model.id.KapuaId in project kapua by eclipse.

the class DeviceLifeCycleServiceImpl method birth.

@Override
public void birth(KapuaId connectionId, KapuaBirthMessage message) throws KapuaException {
    KapuaBirthPayload payload = message.getPayload();
    KapuaBirthChannel channel = message.getChannel();
    KapuaId scopeId = message.getScopeId();
    KapuaId deviceId = message.getDeviceId();
    // 
    // Device update
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = null;
    if (deviceId == null) {
        String clientId = channel.getClientId();
        DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
        DeviceCreator deviceCreator = deviceFactory.newCreator(scopeId, clientId);
        deviceCreator.setDisplayName(payload.getDisplayName());
        deviceCreator.setSerialNumber(payload.getSerialNumber());
        deviceCreator.setModelId(payload.getModelId());
        deviceCreator.setImei(payload.getModemImei());
        deviceCreator.setImsi(payload.getModemImsi());
        deviceCreator.setIccid(payload.getModemIccid());
        deviceCreator.setBiosVersion(payload.getBiosVersion());
        deviceCreator.setFirmwareVersion(payload.getFirmwareVersion());
        deviceCreator.setOsVersion(payload.getOsVersion());
        deviceCreator.setJvmVersion(payload.getJvmVersion());
        deviceCreator.setOsgiFrameworkVersion(payload.getContainerFrameworkVersion());
        deviceCreator.setApplicationIdentifiers(payload.getApplicationIdentifiers());
        deviceCreator.setAcceptEncoding(payload.getAcceptEncoding());
        deviceCreator.setCredentialsMode(DeviceCredentialsMode.LOOSE);
        device = deviceRegistryService.create(deviceCreator);
    } else {
        device = deviceRegistryService.find(scopeId, deviceId);
        device.setDisplayName(payload.getDisplayName());
        device.setSerialNumber(payload.getSerialNumber());
        device.setModelId(payload.getModelId());
        device.setImei(payload.getModemImei());
        device.setImsi(payload.getModemImsi());
        device.setIccid(payload.getModemIccid());
        device.setBiosVersion(payload.getBiosVersion());
        device.setFirmwareVersion(payload.getFirmwareVersion());
        device.setOsVersion(payload.getOsVersion());
        device.setJvmVersion(payload.getJvmVersion());
        device.setOsgiFrameworkVersion(payload.getContainerFrameworkVersion());
        device.setApplicationIdentifiers(payload.getApplicationIdentifiers());
        device.setAcceptEncoding(payload.getAcceptEncoding());
        deviceRegistryService.update(device);
    }
    // 
    // Event create
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, device.getId(), message.getReceivedOn(), "BIRTH");
    deviceEventCreator.setEventMessage(payload.toDisplayString());
    // 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) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) KapuaBirthChannel(org.eclipse.kapua.message.device.lifecycle.KapuaBirthChannel) DeviceCreator(org.eclipse.kapua.service.device.registry.DeviceCreator) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaBirthPayload(org.eclipse.kapua.message.device.lifecycle.KapuaBirthPayload)

Example 75 with KapuaId

use of org.eclipse.kapua.model.id.KapuaId in project kapua by eclipse.

the class IdGeneratorServiceImplTest method testBulkIdGeneration.

@Test
public void testBulkIdGeneration() throws Exception {
    KapuaLocator locator = KapuaLocator.getInstance();
    IdGeneratorService idGeneratorService = locator.getService(IdGeneratorService.class);
    Set<KapuaId> generatedIds = new HashSet<>();
    for (int i = 0; i < 1000; i++) {
        KapuaId id = idGeneratorService.generate();
        assertFalse(generatedIds.contains(id));
        generatedIds.add(id);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaId(org.eclipse.kapua.model.id.KapuaId) IdGeneratorService(org.eclipse.kapua.service.generator.id.IdGeneratorService) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

KapuaId (org.eclipse.kapua.model.id.KapuaId)75 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)54 Account (org.eclipse.kapua.service.account.Account)14 AccountService (org.eclipse.kapua.service.account.AccountService)14 ArrayList (java.util.ArrayList)13 Date (java.util.Date)11 Device (org.eclipse.kapua.service.device.registry.Device)11 Test (org.junit.Test)11 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)10 BaseListLoadResult (com.extjs.gxt.ui.client.data.BaseListLoadResult)9 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)9 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)9 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)8 IdGeneratorService (org.eclipse.kapua.service.generator.id.IdGeneratorService)8 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)7 GwtAccountService (org.eclipse.kapua.app.console.shared.service.GwtAccountService)7 KapuaEid (org.eclipse.kapua.commons.model.id.KapuaEid)7 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)7 User (org.eclipse.kapua.service.user.User)7 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)6