Search in sources :

Example 1 with CommandResponsePayload

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

the class TranslatorAppCommandKuraKapua method translate.

private CommandResponsePayload translate(KuraResponsePayload kuraPayload) throws KapuaException {
    CommandResponsePayload commandResponsePayload = new CommandResponsePayload();
    Map<String, Object> metrics = kuraPayload.getMetrics();
    commandResponsePayload.setStderr((String) metrics.get(CommandMetrics.APP_METRIC_STDERR.getValue()));
    commandResponsePayload.setStdout((String) metrics.get(CommandMetrics.APP_METRIC_STDOUT.getValue()));
    commandResponsePayload.setExitCode((Integer) metrics.get(CommandMetrics.APP_METRIC_EXIT_CODE.getValue()));
    Boolean timedout = (Boolean) metrics.get(CommandMetrics.APP_METRIC_TIMED_OUT.getValue());
    if (timedout != null) {
        commandResponsePayload.setTimedout(timedout);
    }
    commandResponsePayload.setExceptionMessage((String) metrics.get(ResponseMetrics.RESP_METRIC_EXCEPTION_MESSAGE.getValue()));
    commandResponsePayload.setExceptionStack((String) metrics.get(ResponseMetrics.RESP_METRIC_EXCEPTION_STACK.getValue()));
    // Return Kapua Payload
    return commandResponsePayload;
}
Also used : CommandResponsePayload(org.eclipse.kapua.service.device.management.command.message.internal.CommandResponsePayload)

Example 2 with CommandResponsePayload

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

the class TranslatorAppCommandKuraKapua method translate.

@Override
public CommandResponseMessage translate(KuraResponseMessage kuraMessage) throws KapuaException {
    // 
    // Kura channel
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraMessage.getChannel().getScope());
    CommandResponseChannel commandResponseChannel = translate(kuraMessage.getChannel());
    // 
    // Kura payload
    CommandResponsePayload responsePayload = translate(kuraMessage.getPayload());
    // 
    // Kura Message
    CommandResponseMessage kapuaMessage = new CommandResponseMessage();
    kapuaMessage.setScopeId(account.getId());
    kapuaMessage.setChannel(commandResponseChannel);
    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;
}
Also used : CommandResponsePayload(org.eclipse.kapua.service.device.management.command.message.internal.CommandResponsePayload) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) CommandResponseChannel(org.eclipse.kapua.service.device.management.command.message.internal.CommandResponseChannel) CommandResponseMessage(org.eclipse.kapua.service.device.management.command.message.internal.CommandResponseMessage) AccountService(org.eclipse.kapua.service.account.AccountService)

Example 3 with CommandResponsePayload

use of org.eclipse.kapua.service.device.management.command.message.internal.CommandResponsePayload 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

CommandResponsePayload (org.eclipse.kapua.service.device.management.command.message.internal.CommandResponsePayload)3 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 CommandResponseMessage (org.eclipse.kapua.service.device.management.command.message.internal.CommandResponseMessage)2 Date (java.util.Date)1 Account (org.eclipse.kapua.service.account.Account)1 AccountService (org.eclipse.kapua.service.account.AccountService)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 CommandResponseChannel (org.eclipse.kapua.service.device.management.command.message.internal.CommandResponseChannel)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