/
TranslatorKuraKapuaUtils.java
166 lines (146 loc) · 7.55 KB
/
TranslatorKuraKapuaUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************
* Copyright (c) 2016, 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
* Red Hat Inc
*******************************************************************************/
package org.eclipse.kapua.translator.kura.kapua;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.setting.system.SystemSetting;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.message.KapuaMessageFactory;
import org.eclipse.kapua.message.KapuaPosition;
import org.eclipse.kapua.service.device.call.message.DevicePosition;
import org.eclipse.kapua.service.device.call.message.app.DeviceAppMetrics;
import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponseChannel;
import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponseCode;
import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponseMetrics;
import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponsePayload;
import org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting;
import org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSettingKey;
import org.eclipse.kapua.service.device.management.message.response.KapuaResponseCode;
import org.eclipse.kapua.service.device.management.message.response.KapuaResponsePayload;
import org.eclipse.kapua.translator.exception.TranslatorErrorCodes;
import org.eclipse.kapua.translator.exception.TranslatorException;
/**
* {@link org.eclipse.kapua.translator.Translator} utilities.<br>
* It provides helpful methods for translate {@link DevicePosition} and {@link KuraResponseCode}.
*
* @since 1.0.0
*/
public final class TranslatorKuraKapuaUtils {
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final KapuaMessageFactory KAPUA_MESSAGE_FACTORY = LOCATOR.getFactory(KapuaMessageFactory.class);
private static final String CONTROL_MESSAGE_CLASSIFIER = SystemSetting.getInstance().getMessageClassifier();
private static final boolean SHOW_STACKTRACE = DeviceManagementSetting.getInstance().getBoolean(DeviceManagementSettingKey.SHOW_STACKTRACE, false);
private TranslatorKuraKapuaUtils() {
}
/**
* Validates the given {@link KuraResponseChannel}.
* <p>
* Checks that:
* <ul>
* <li>the {@link KuraResponseChannel#getMessageClassification()} matches the configured {@link SystemSetting#getMessageClassifier()}</li>
* <li>the {@link KuraResponseChannel#getAppId()} is formatted as {@code appId-appVersion}</li>
* <li>the {@link KuraResponseChannel#getAppId()} first token matches the given application name</li>
* <li>the {@link KuraResponseChannel#getAppId()} second token matches the given application version</li>
* </ul>
*
* @param kuraResponseChannel the {@link KuraResponseChannel} to check.
* @param appName the application name.
* @param appVersion the application version.
* @throws TranslatorException The any of the constraints fails.
* @since 1.2.0
*/
public static void validateKuraResponseChannel(KuraResponseChannel kuraResponseChannel, DeviceAppMetrics appName, DeviceAppMetrics appVersion) throws TranslatorException {
if (!CONTROL_MESSAGE_CLASSIFIER.equals(kuraResponseChannel.getMessageClassification())) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_CHANNEL_CLASSIFIER, null, kuraResponseChannel.getMessageClassification());
}
String[] appIdTokens = kuraResponseChannel.getAppId().split("-");
if (appIdTokens.length < 2) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_CHANNEL_APP_NAME, null, (Object) appIdTokens);
}
if (!appName.getName().equals(appIdTokens[0])) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_CHANNEL_APP_NAME, null, appIdTokens[0]);
}
if (!appVersion.getName().equals(appIdTokens[1])) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_CHANNEL_APP_VERSION, null, appIdTokens[1]);
}
}
/**
* Builds the {@link KapuaResponsePayload} with commons property in {@link KuraResponsePayload}
*
* @param kuraResponsePayload The {@link KuraResponsePayload}.
* @param appResponsePayload The application specific {@link KapuaResponsePayload}.
* @param <P> The application specific {@link KapuaResponsePayload} type.
* @return The built base application specific {@link KapuaResponsePayload}}
*/
public static <P extends KapuaResponsePayload> P buildBaseResponsePayload(KuraResponsePayload kuraResponsePayload, P appResponsePayload) {
appResponsePayload.setExceptionMessage(kuraResponsePayload.getExceptionMessage());
if (SHOW_STACKTRACE) {
appResponsePayload.setExceptionStack(kuraResponsePayload.getExceptionStack());
kuraResponsePayload.getMetrics().remove(KuraResponseMetrics.EXCEPTION_STACK.getName());
}
return appResponsePayload;
}
/**
* Translates {@link DevicePosition} to {@link KapuaPosition}
*
* @param devicePosition The {@link DevicePosition} to translate.
* @return The translated {@link KapuaPosition}.
* @since 1.0.0
*/
public static KapuaPosition translate(DevicePosition devicePosition) {
KapuaPosition kapuaPosition = null;
if (devicePosition != null) {
kapuaPosition = KAPUA_MESSAGE_FACTORY.newPosition();
kapuaPosition.setAltitude(devicePosition.getAltitude());
kapuaPosition.setHeading(devicePosition.getHeading());
kapuaPosition.setLatitude(devicePosition.getLatitude());
kapuaPosition.setLongitude(devicePosition.getLongitude());
kapuaPosition.setPrecision(devicePosition.getPrecision());
kapuaPosition.setSatellites(devicePosition.getSatellites());
kapuaPosition.setSpeed(devicePosition.getSpeed());
kapuaPosition.setStatus(devicePosition.getStatus());
kapuaPosition.setTimestamp(devicePosition.getTimestamp());
}
return kapuaPosition;
}
/**
* Translate {@link KuraResponseCode} to {@link KapuaResponseCode}
*
* @param kuraResponseCode The {@link KuraResponseCode} to translate.
* @return The translated {@link KapuaResponseCode}
* @since 1.0.0
*/
public static KapuaResponseCode translate(KuraResponseCode kuraResponseCode) throws KapuaException {
if (kuraResponseCode == null) {
return null;
}
KapuaResponseCode responseCode;
switch (kuraResponseCode) {
case ACCEPTED:
responseCode = KapuaResponseCode.ACCEPTED;
break;
case BAD_REQUEST:
responseCode = KapuaResponseCode.BAD_REQUEST;
break;
case NOT_FOUND:
responseCode = KapuaResponseCode.NOT_FOUND;
break;
case INTERNAL_ERROR:
responseCode = KapuaResponseCode.INTERNAL_ERROR;
break;
default:
throw KapuaException.internalError("Kura Response code not mapped");
}
return responseCode;
}
}