use of org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException 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;
}
use of org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException in project kapua by eclipse.
the class DeviceConfigurationManagementServiceImpl method get.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceConfiguration get(KapuaId scopeId, KapuaId deviceId, String configurationId, String configurationComponentPid, 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
ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
configurationRequestChannel.setMethod(KapuaMethod.READ);
configurationRequestChannel.setConfigurationId(configurationId);
configurationRequestChannel.setComponentId(configurationComponentPid);
ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
configurationRequestMessage.setScopeId(scopeId);
configurationRequestMessage.setDeviceId(deviceId);
configurationRequestMessage.setCapturedOn(new Date());
configurationRequestMessage.setPayload(configurationRequestPayload);
configurationRequestMessage.setChannel(configurationRequestChannel);
//
// Do get
DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(configurationRequestMessage, timeout);
ConfigurationResponseMessage responseMessage = (ConfigurationResponseMessage) deviceApplicationCall.send();
//
// Parse the response
ConfigurationResponsePayload responsePayload = responseMessage.getPayload();
DeviceManagementSetting config = DeviceManagementSetting.getInstance();
String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
DeviceConfiguration deviceConfiguration = null;
if (responsePayload.getBody() != null) {
String body = null;
try {
body = new String(responsePayload.getBody(), charEncoding);
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
}
try {
deviceConfiguration = XmlUtil.unmarshal(body, DeviceConfigurationImpl.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(), DeviceConfigurationAppProperties.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 deviceConfiguration;
}
use of org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException in project kapua by eclipse.
the class DeviceConfigurationManagementServiceImpl method put.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void put(KapuaId scopeId, KapuaId deviceId, DeviceComponentConfiguration deviceComponentConfiguration, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
ArgumentValidator.notNull(deviceComponentConfiguration, "componentConfiguration");
ArgumentValidator.notEmptyOrNull(deviceComponentConfiguration.getId(), "componentConfiguration.componentId");
//
// 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));
//
// Prepare the request
ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
configurationRequestChannel.setMethod(KapuaMethod.WRITE);
configurationRequestChannel.setComponentId(deviceComponentConfiguration.getId());
ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
try {
DeviceConfigurationFactory deviceConfigurationFactory = locator.getFactory(DeviceConfigurationFactory.class);
DeviceConfiguration deviceConfiguration = deviceConfigurationFactory.newConfigurationInstance();
deviceConfiguration.getComponentConfigurations().add(deviceComponentConfiguration);
DeviceManagementSetting deviceManagementConfig = DeviceManagementSetting.getInstance();
String charEncoding = deviceManagementConfig.getString(DeviceManagementSettingKey.CHAR_ENCODING);
StringWriter sw = new StringWriter();
XmlUtil.marshal(deviceConfiguration, sw);
byte[] requestBody = sw.toString().getBytes(charEncoding);
configurationRequestPayload.setBody(requestBody);
new String(configurationRequestPayload.getBody());
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.REQUEST_EXCEPTION, e, deviceComponentConfiguration);
}
ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
configurationRequestMessage.setScopeId(scopeId);
configurationRequestMessage.setDeviceId(deviceId);
configurationRequestMessage.setCapturedOn(new Date());
configurationRequestMessage.setPayload(configurationRequestPayload);
configurationRequestMessage.setChannel(configurationRequestChannel);
//
// Do put
DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(configurationRequestMessage, timeout);
ConfigurationResponseMessage responseMessage = (ConfigurationResponseMessage) deviceApplicationCall.send();
//
// Create event
DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceConfigurationAppProperties.APP_NAME.getValue());
deviceEventCreator.setPosition(responseMessage.getPosition());
deviceEventCreator.setSentOn(responseMessage.getSentOn());
deviceEventCreator.setAction(KapuaMethod.WRITE);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
}
use of org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException in project kapua by eclipse.
the class DeviceCallExecutor method send.
@SuppressWarnings({ "unchecked" })
public RS send() throws KapuaException {
//
// Get the correct device call
KapuaLocator locator = KapuaLocator.getInstance();
DeviceCallFactory kapuaDeviceCallFactory = locator.getFactory(DeviceCallFactory.class);
DeviceCall<DeviceRequestMessage, DeviceResponseMessage> deviceCall = kapuaDeviceCallFactory.newDeviceCall();
Translator tKapuaToClient = Translator.getTranslatorFor(requestMessage.getRequestClass(), deviceCall.getBaseMessageClass());
DeviceResponseMessage responseMessage;
timeout = timeout == null ? DeviceManagementSetting.getInstance().getLong(DeviceManagementSettingKey.REQUEST_TIMEOUT) : timeout;
DeviceRequestMessage deviceRequestMessage = (DeviceRequestMessage) tKapuaToClient.translate(requestMessage);
switch(requestMessage.getChannel().getMethod()) {
case CREATE:
{
responseMessage = deviceCall.create(deviceRequestMessage, timeout);
}
break;
case READ:
{
responseMessage = deviceCall.read(deviceRequestMessage, timeout);
}
break;
case OPTIONS:
{
responseMessage = deviceCall.options(deviceRequestMessage, timeout);
}
break;
case DELETE:
{
responseMessage = deviceCall.delete(deviceRequestMessage, timeout);
}
break;
case EXECUTE:
{
responseMessage = deviceCall.execute(deviceRequestMessage, timeout);
}
break;
case WRITE:
{
responseMessage = deviceCall.write(deviceRequestMessage, timeout);
}
break;
default:
throw new DeviceManagementException(DeviceManagementErrorCodes.REQUEST_BAD_METHOD, null, requestMessage.getChannel().getMethod());
}
Translator tClientToKapua = Translator.getTranslatorFor(deviceCall.getBaseMessageClass(), requestMessage.getResponseClass());
return (RS) tClientToKapua.translate(responseMessage);
}
use of org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException 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;
}
Aggregations