use of org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestPayload 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;
}
use of org.eclipse.kapua.service.device.management.configuration.snapshot.internal.SnapshotRequestPayload in project kapua by eclipse.
the class DeviceSnapshotManagementServiceImpl method rollback.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void rollback(KapuaId scopeId, KapuaId deviceId, String snapshotId, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
ArgumentValidator.notEmptyOrNull(snapshotId, "snapshotId");
//
// 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.execute, scopeId));
//
// Prepare the request
SnapshotRequestChannel snapshotRequestChannel = new SnapshotRequestChannel();
snapshotRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
snapshotRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
snapshotRequestChannel.setMethod(KapuaMethod.EXECUTE);
snapshotRequestChannel.setSnapshotId(snapshotId);
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 exec
DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(snapshotRequestMessage, timeout);
SnapshotResponseMessage responseMessage = (SnapshotResponseMessage) deviceApplicationCall.send();
//
// 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.EXECUTE);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
}
Aggregations