use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage 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;
}
use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage in project kapua by eclipse.
the class DevicePackageManagementServiceImpl method downloadExec.
@Override
public void downloadExec(KapuaId scopeId, KapuaId deviceId, DevicePackageDownloadRequest packageDownloadRequest, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
ArgumentValidator.notNull(packageDownloadRequest, "packageDownloadRequest");
//
// 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.DOWNLOAD);
PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
packageRequestPayload.setOperationId(operationId);
packageRequestPayload.setPackageDownloadURI(packageDownloadRequest.getURI());
packageRequestPayload.setPackageDownloadName(packageDownloadRequest.getName());
packageRequestPayload.setPackageDownloadVersion(packageDownloadRequest.getVersion());
packageRequestPayload.setPackageDownloadnstall(packageDownloadRequest.isInstall());
packageRequestPayload.setReboot(packageDownloadRequest.isReboot());
packageRequestPayload.setRebootDelay(packageDownloadRequest.getRebootDelay());
PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
packageRequestMessage.setScopeId(scopeId);
packageRequestMessage.setDeviceId(deviceId);
packageRequestMessage.setCapturedOn(new Date());
packageRequestMessage.setPayload(packageRequestPayload);
packageRequestMessage.setChannel(packageRequestChannel);
//
// Do exec
@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.service.device.management.packages.message.internal.PackageResponseMessage in project kapua by eclipse.
the class DevicePackageManagementServiceImpl method uninstallExec.
@Override
public void uninstallExec(KapuaId scopeId, KapuaId deviceId, DevicePackageUninstallRequest deployUninstallRequest, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
ArgumentValidator.notNull(deployUninstallRequest, "deployUninstallRequest");
//
// 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.UNINSTALL);
PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
packageRequestPayload.setOperationId(operationId);
packageRequestPayload.setPackageUninstallName(deployUninstallRequest.getName());
packageRequestPayload.setPackageUninstallVersion(deployUninstallRequest.getVersion());
packageRequestPayload.setReboot(deployUninstallRequest.isReboot());
packageRequestPayload.setRebootDelay(deployUninstallRequest.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.service.device.management.packages.message.internal.PackageResponseMessage in project kapua by eclipse.
the class DevicePackageManagementServiceImpl method getInstalled.
@Override
public DevicePackages getInstalled(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
PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
packageRequestChannel.setMethod(KapuaMethod.READ);
packageRequestChannel.setPackageResource(null);
PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
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();
//
// Parse the response
PackageResponsePayload responsePayload = responseMessage.getPayload();
DevicePackages devicePackages;
if (responsePayload.getBody() != null) {
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());
}
try {
devicePackages = XmlUtil.unmarshal(body, DevicePackagesImpl.class);
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
}
} else {
devicePackages = new DevicePackagesImpl();
}
//
// 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.READ);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
return devicePackages;
}
use of org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage in project kapua by eclipse.
the class DevicePackageManagementServiceImpl method installStatus.
@Override
public DevicePackageInstallOperation installStatus(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
PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
packageRequestChannel.setMethod(KapuaMethod.READ);
packageRequestChannel.setPackageResource(PackageResource.INSTALL);
PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
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();
//
// Parse the response
PackageResponsePayload 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());
}
DevicePackageInstallOperation installOperation = null;
try {
installOperation = XmlUtil.unmarshal(body, DevicePackageInstallOperationImpl.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(), PackageAppProperties.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 installOperation;
}
Aggregations