Skip to content

Package: TranslatorRequestKuraMqtt

TranslatorRequestKuraMqtt

nameinstructionbranchcomplexitylinemethod
TranslatorRequestKuraMqtt()
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%
generateResponseTopic(KuraRequestChannel)
M: 52 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 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: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
translate(KuraPayload)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
translate(KuraRequestChannel)
M: 54 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
translate(KuraRequestMessage)
M: 34 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 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: *******************************************************************************/
13: package org.eclipse.kapua.translator.kura.mqtt;
14:
15: import org.eclipse.kapua.service.device.call.message.kura.KuraPayload;
16: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestChannel;
17: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestMessage;
18: import org.eclipse.kapua.service.device.call.message.kura.data.KuraDataChannel;
19: import org.eclipse.kapua.service.device.call.message.kura.setting.DeviceCallSettingKeys;
20: import org.eclipse.kapua.service.device.call.message.kura.setting.DeviceCallSettings;
21: import org.eclipse.kapua.translator.Translator;
22: import org.eclipse.kapua.translator.exception.InvalidChannelException;
23: import org.eclipse.kapua.translator.exception.InvalidMessageException;
24: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
25: import org.eclipse.kapua.translator.exception.TranslateException;
26: import org.eclipse.kapua.transport.message.mqtt.MqttMessage;
27: import org.eclipse.kapua.transport.message.mqtt.MqttPayload;
28: import org.eclipse.kapua.transport.message.mqtt.MqttTopic;
29:
30: import java.util.ArrayList;
31: import java.util.Collections;
32: import java.util.List;
33:
34: /**
35: * {@link Translator} implementation from {@link KuraRequestMessage} to {@link MqttMessage}
36: *
37: * @since 1.0.0
38: */
39: public class TranslatorRequestKuraMqtt extends Translator<KuraRequestMessage, MqttMessage> {
40:
41: private static final String REPLY_PART = DeviceCallSettings.getInstance().getString(DeviceCallSettingKeys.DESTINATION_REPLY_PART);
42:
43: @Override
44: public MqttMessage translate(KuraRequestMessage kuraRequestMessage) throws TranslateException {
45: try {
46: // Mqtt request topic
47: MqttTopic mqttRequestTopic = translate(kuraRequestMessage.getChannel());
48:
49: // Mqtt response topic
50: MqttTopic mqttResponseTopic = generateResponseTopic(kuraRequestMessage.getChannel());
51:
52: // Mqtt payload
53: MqttPayload mqttPayload = translate(kuraRequestMessage.getPayload());
54:
55: // Return Mqtt message
56: return new MqttMessage(mqttRequestTopic, mqttResponseTopic, mqttPayload);
57: } catch (InvalidChannelException | InvalidPayloadException te) {
58: throw te;
59: } catch (Exception e) {
60: throw new InvalidMessageException(e, kuraRequestMessage);
61: }
62: }
63:
64: /**
65: * Translates the given {@link KuraRequestChannel} to the {@link MqttTopic} equivalent.
66: *
67: * @param kuraRequestChannel The {@link KuraDataChannel} to translate.
68: * @return The translated {@link MqttTopic}
69: * @throws InvalidChannelException if translation encounters any error.
70: * @since 1.0.0
71: */
72: public MqttTopic translate(KuraRequestChannel kuraRequestChannel) throws InvalidChannelException {
73: try {
74: List<String> topicTokens = new ArrayList<>();
75:
76:• if (kuraRequestChannel.getMessageClassification() != null) {
77: topicTokens.add(kuraRequestChannel.getMessageClassification());
78: }
79:
80: topicTokens.add(kuraRequestChannel.getScope());
81: topicTokens.add(kuraRequestChannel.getClientId());
82: topicTokens.add(kuraRequestChannel.getAppId());
83: topicTokens.add(kuraRequestChannel.getMethod().name());
84:
85: Collections.addAll(topicTokens, kuraRequestChannel.getResources());
86:
87: // Return Mqtt Topic
88: return new MqttTopic(topicTokens.toArray(new String[0]));
89: } catch (Exception e) {
90: throw new InvalidChannelException(e, kuraRequestChannel);
91: }
92: }
93:
94: /**
95: * Generates the response {@link MqttTopic} for the given {@link KuraRequestChannel}.
96: *
97: * @param kuraRequestChannel The {@link KuraRequestChannel}
98: * @return The response {@link MqttTopic}.
99: * @throws InvalidChannelException if generation encounters any error.
100: * @since 1.0.0
101: */
102: public MqttTopic generateResponseTopic(KuraRequestChannel kuraRequestChannel) throws InvalidChannelException {
103: try {
104: List<String> topicTokens = new ArrayList<>();
105:
106:• if (kuraRequestChannel.getMessageClassification() != null) {
107: topicTokens.add(kuraRequestChannel.getMessageClassification());
108: }
109:
110: topicTokens.add(kuraRequestChannel.getScope());
111: topicTokens.add(kuraRequestChannel.getRequesterClientId());
112: topicTokens.add(kuraRequestChannel.getAppId());
113: topicTokens.add(REPLY_PART);
114: topicTokens.add(kuraRequestChannel.getRequestId());
115:
116: return new MqttTopic(topicTokens.toArray(new String[0]));
117: } catch (Exception e) {
118: throw new InvalidChannelException(e, kuraRequestChannel);
119: }
120: }
121:
122: /**
123: * Translates the given {@link KuraPayload} to the {@link MqttPayload} equivalent.
124: *
125: * @param kuraPayload The {@link KuraDataChannel} to translate.
126: * @return The translated {@link MqttPayload}
127: * @throws InvalidPayloadException if translation encounters any error.
128: * @since 1.0.0
129: */
130: public MqttPayload translate(KuraPayload kuraPayload) throws InvalidPayloadException {
131: try {
132: return new MqttPayload(kuraPayload.toByteArray());
133: } catch (Exception e) {
134: throw new InvalidPayloadException(e, kuraPayload);
135: }
136: }
137:
138: @Override
139: public Class<KuraRequestMessage> getClassFrom() {
140: return KuraRequestMessage.class;
141: }
142:
143: @Override
144: public Class<MqttMessage> getClassTo() {
145: return MqttMessage.class;
146: }
147: }