Search in sources :

Example 1 with CommandRequestMessage

use of org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestMessage in project kapua by eclipse.

the class DeviceCommandManagementServiceImpl method exec.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceCommandOutput exec(KapuaId scopeId, KapuaId deviceId, DeviceCommandInput commandInput, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notNull(commandInput, "commandInput");
    // 
    // 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
    CommandRequestChannel commandRequestChannel = new CommandRequestChannel();
    commandRequestChannel.setAppName(CommandAppProperties.APP_NAME);
    commandRequestChannel.setVersion(CommandAppProperties.APP_VERSION);
    commandRequestChannel.setMethod(KapuaMethod.EXECUTE);
    CommandRequestPayload commandRequestPayload = new CommandRequestPayload();
    commandRequestPayload.setCommand(commandInput.getCommand());
    commandRequestPayload.setArguments(commandInput.getArguments());
    commandRequestPayload.setStdin(commandInput.getStdin());
    commandRequestPayload.setTimeout(commandInput.getTimeout());
    commandRequestPayload.setWorkingDir(commandInput.getWorkingDir());
    commandRequestPayload.setEnvironmentPairs(commandInput.getEnvironment());
    commandRequestPayload.setRunAsync(commandInput.isRunAsynch());
    commandRequestPayload.setPassword(commandInput.getPassword());
    commandRequestPayload.setBody(commandInput.getBody());
    CommandRequestMessage commandRequestMessage = new CommandRequestMessage();
    commandRequestMessage.setScopeId(scopeId);
    commandRequestMessage.setDeviceId(deviceId);
    commandRequestMessage.setCapturedOn(new Date());
    commandRequestMessage.setPayload(commandRequestPayload);
    commandRequestMessage.setChannel(commandRequestChannel);
    // 
    // Do exec
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(commandRequestMessage, timeout);
    CommandResponseMessage responseMessage = (CommandResponseMessage) deviceApplicationCall.send();
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), CommandAppProperties.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);
    // 
    // Parse the response
    CommandResponsePayload responsePayload = responseMessage.getPayload();
    DeviceCommandOutput deviceCommandOutput = new DeviceCommandOutputImpl();
    deviceCommandOutput.setExceptionMessage(responsePayload.getExceptionMessage());
    deviceCommandOutput.setExceptionStack(responsePayload.getExceptionStack());
    deviceCommandOutput.setExitCode(responsePayload.getExitCode());
    // FIXME: implement track of timeout!!!
    deviceCommandOutput.setHasTimedout(false);
    deviceCommandOutput.setStderr(responsePayload.getStderr());
    deviceCommandOutput.setStdout(responsePayload.getStdout());
    return deviceCommandOutput;
}
Also used : CommandResponsePayload(org.eclipse.kapua.service.device.management.command.message.internal.CommandResponsePayload) DeviceCommandOutput(org.eclipse.kapua.service.device.management.command.DeviceCommandOutput) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) CommandResponseMessage(org.eclipse.kapua.service.device.management.command.message.internal.CommandResponseMessage) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) Date(java.util.Date) CommandRequestMessage(org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestMessage) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) CommandRequestChannel(org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestChannel) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) CommandRequestPayload(org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestPayload) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)

Aggregations

Date (java.util.Date)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 DeviceCommandOutput (org.eclipse.kapua.service.device.management.command.DeviceCommandOutput)1 CommandRequestChannel (org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestChannel)1 CommandRequestMessage (org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestMessage)1 CommandRequestPayload (org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestPayload)1 CommandResponseMessage (org.eclipse.kapua.service.device.management.command.message.internal.CommandResponseMessage)1 CommandResponsePayload (org.eclipse.kapua.service.device.management.command.message.internal.CommandResponsePayload)1 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)1 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)1 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)1 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)1