Skip to content

Package: TranslatorAppCommandKapuaKura

TranslatorAppCommandKapuaKura

nameinstructionbranchcomplexitylinemethod
TranslatorAppCommandKapuaKura()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getClassFrom()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getClassTo()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 46 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
translateChannel(CommandRequestChannel)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
translatePayload(CommandRequestPayload)
M: 128 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.translator.kapua.kura;
15:
16: import org.eclipse.kapua.service.device.call.kura.model.command.CommandMetrics;
17: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestChannel;
18: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestMessage;
19: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestPayload;
20: import org.eclipse.kapua.service.device.management.command.internal.CommandAppProperties;
21: import org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestChannel;
22: import org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestMessage;
23: import org.eclipse.kapua.service.device.management.command.message.internal.CommandRequestPayload;
24: import org.eclipse.kapua.translator.exception.InvalidChannelException;
25: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
26:
27: import java.util.EnumMap;
28: import java.util.Map;
29:
30: /**
31: * {@link org.eclipse.kapua.translator.Translator} implementation from {@link CommandRequestMessage} to {@link KuraRequestMessage}
32: *
33: * @since 1.0
34: */
35: public class TranslatorAppCommandKapuaKura extends AbstractTranslatorKapuaKura<CommandRequestChannel, CommandRequestPayload, CommandRequestMessage> {
36:
37: private static final Map<CommandAppProperties, CommandMetrics> PROPERTIES_DICTIONARY = new EnumMap<>(CommandAppProperties.class);
38:
39: static {
40: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_CMD, CommandMetrics.APP_METRIC_CMD);
41: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_ARG, CommandMetrics.APP_METRIC_ARG);
42: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_ENVP, CommandMetrics.APP_METRIC_ENVP);
43: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_DIR, CommandMetrics.APP_METRIC_DIR);
44: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_STDIN, CommandMetrics.APP_METRIC_STDIN);
45: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_TOUT, CommandMetrics.APP_METRIC_TOUT);
46: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_ASYNC, CommandMetrics.APP_METRIC_ASYNC);
47: PROPERTIES_DICTIONARY.put(CommandAppProperties.APP_PROPERTY_PASSWORD, CommandMetrics.APP_METRIC_PASSWORD);
48: }
49:
50: @Override
51: protected KuraRequestChannel translateChannel(CommandRequestChannel kapuaChannel) throws InvalidChannelException {
52: try {
53: KuraRequestChannel kuraRequestChannel = TranslatorKapuaKuraUtils.buildBaseRequestChannel(CommandMetrics.APP_ID, CommandMetrics.APP_VERSION, kapuaChannel.getMethod());
54: kuraRequestChannel.setResources(new String[]{"command"});
55:
56: //
57: // Return Kura Channel
58: return kuraRequestChannel;
59: } catch (Exception e) {
60: throw new InvalidChannelException(e, kapuaChannel);
61: }
62: }
63:
64: @Override
65: protected KuraRequestPayload translatePayload(CommandRequestPayload kapuaPayload) throws InvalidPayloadException {
66: try {
67: KuraRequestPayload kuraRequestPayload = new KuraRequestPayload();
68:
69: // Payload translation
70: Map<String, Object> metrics = kuraRequestPayload.getMetrics();
71: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_CMD).getName(), kapuaPayload.getCommand());
72: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_ENVP).getName(), kapuaPayload.getEnvironmentPairs());
73: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_DIR).getName(), kapuaPayload.getWorkingDir());
74: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_STDIN).getName(), kapuaPayload.getStdin());
75: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_TOUT).getName(), kapuaPayload.getTimeout());
76: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_ASYNC).getName(), kapuaPayload.isRunAsync());
77: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_PASSWORD).getName(), kapuaPayload.getPassword());
78:
79: // argument translation
80: int i = 0;
81: String[] arguments = kapuaPayload.getArguments();
82:• for (String argument : arguments) {
83: metrics.put(PROPERTIES_DICTIONARY.get(CommandAppProperties.APP_PROPERTY_ARG).getName() + i++, argument);
84: }
85:
86: // Body translation
87: kuraRequestPayload.setBody(kapuaPayload.getBody());
88:
89: // Return Kura Payload
90: return kuraRequestPayload;
91: } catch (Exception e) {
92: throw new InvalidPayloadException(e, kapuaPayload);
93: }
94: }
95:
96: @Override
97: public Class<CommandRequestMessage> getClassFrom() {
98: return CommandRequestMessage.class;
99: }
100:
101: @Override
102: public Class<KuraRequestMessage> getClassTo() {
103: return KuraRequestMessage.class;
104: }
105:
106: }