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());
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations