Skip to content

Package: TranslatorDataKuraMqtt

TranslatorDataKuraMqtt

nameinstructionbranchcomplexitylinemethod
TranslatorDataKuraMqtt()
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%
translate(KuraDataChannel)
M: 39 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
translate(KuraDataMessage)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
translate(KuraDataPayload)
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%

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.kura.mqtt;
15:
16: import org.eclipse.kapua.service.device.call.message.kura.data.KuraDataChannel;
17: import org.eclipse.kapua.service.device.call.message.kura.data.KuraDataMessage;
18: import org.eclipse.kapua.service.device.call.message.kura.data.KuraDataPayload;
19: import org.eclipse.kapua.translator.Translator;
20: import org.eclipse.kapua.translator.exception.InvalidChannelException;
21: import org.eclipse.kapua.translator.exception.InvalidMessageException;
22: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
23: import org.eclipse.kapua.translator.exception.TranslateException;
24: import org.eclipse.kapua.transport.message.mqtt.MqttMessage;
25: import org.eclipse.kapua.transport.message.mqtt.MqttPayload;
26: import org.eclipse.kapua.transport.message.mqtt.MqttTopic;
27:
28: import java.util.ArrayList;
29: import java.util.Date;
30: import java.util.List;
31:
32: /**
33: * {@link Translator} implementation from {@link KuraDataMessage} to {@link MqttMessage}
34: *
35: * @since 1.0.0
36: */
37: public class TranslatorDataKuraMqtt extends Translator<KuraDataMessage, MqttMessage> {
38:
39: @Override
40: public MqttMessage translate(KuraDataMessage kuraDataMessage) throws TranslateException {
41: try {
42: // Mqtt request topic
43: MqttTopic mqttRequestTopic = translate(kuraDataMessage.getChannel());
44:
45: // Mqtt payload
46: MqttPayload mqttPayload = translate(kuraDataMessage.getPayload());
47:
48: // Return Mqtt message
49: return new MqttMessage(mqttRequestTopic, new Date(), mqttPayload);
50: } catch (InvalidChannelException | InvalidPayloadException te) {
51: throw te;
52: } catch (Exception e) {
53: throw new InvalidMessageException(e, kuraDataMessage);
54: }
55: }
56:
57: /**
58: * Translates the given {@link KuraDataChannel} to the {@link MqttTopic} equivalent.
59: *
60: * @param kuraDataChannel The {@link KuraDataChannel} to translate.
61: * @return The translated {@link MqttTopic}
62: * @throws InvalidChannelException if translation encounters any error.
63: * @since 1.0.0
64: */
65: public MqttTopic translate(KuraDataChannel kuraDataChannel) throws InvalidChannelException {
66: try {
67: List<String> topicTokens = new ArrayList<>();
68:
69: topicTokens.add(kuraDataChannel.getScope());
70: topicTokens.add(kuraDataChannel.getClientId());
71:
72:• if (!kuraDataChannel.getSemanticParts().isEmpty()) {
73: topicTokens.addAll(kuraDataChannel.getSemanticParts());
74: }
75:
76: return new MqttTopic(topicTokens.toArray(new String[0]));
77: } catch (Exception e) {
78: throw new InvalidChannelException(e, kuraDataChannel);
79: }
80: }
81:
82: /**
83: * Translates the given {@link KuraDataPayload} to the {@link MqttPayload} equivalent.
84: *
85: * @param kuraDataPayload The {@link KuraDataChannel} to translate.
86: * @return The translated {@link MqttPayload}
87: * @throws InvalidPayloadException if translation encounters any error.
88: * @since 1.0.0
89: */
90: public MqttPayload translate(KuraDataPayload kuraDataPayload) throws InvalidPayloadException {
91: try {
92: return new MqttPayload(kuraDataPayload.toByteArray());
93: } catch (Exception e) {
94: throw new InvalidPayloadException(e, kuraDataPayload);
95: }
96: }
97:
98: @Override
99: public Class<KuraDataMessage> getClassFrom() {
100: return KuraDataMessage.class;
101: }
102:
103: @Override
104: public Class<MqttMessage> getClassTo() {
105: return MqttMessage.class;
106: }
107: }