Skip to content

Package: TranslatorAppNotifyKuraKapua

TranslatorAppNotifyKuraKapua

nameinstructionbranchcomplexitylinemethod
TranslatorAppNotifyKuraKapua()
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: 86 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
translate(KuraNotifyChannel)
M: 43 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
translate(KuraNotifyMessage)
M: 101 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
translate(KuraNotifyPayload)
M: 55 C: 0
0%
M: 4 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2020 Eurotech and/or its affiliates and others
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech - initial API and implementation
11: *******************************************************************************/
12: package org.eclipse.kapua.translator.kura.kapua;
13:
14: import org.eclipse.kapua.KapuaEntityNotFoundException;
15: import org.eclipse.kapua.locator.KapuaLocator;
16: import org.eclipse.kapua.model.id.KapuaIdFactory;
17: import org.eclipse.kapua.service.account.Account;
18: import org.eclipse.kapua.service.account.AccountService;
19: import org.eclipse.kapua.service.device.call.kura.app.AssetMetrics;
20: import org.eclipse.kapua.service.device.call.kura.app.BundleMetrics;
21: import org.eclipse.kapua.service.device.call.kura.app.CommandMetrics;
22: import org.eclipse.kapua.service.device.call.kura.app.ConfigurationMetrics;
23: import org.eclipse.kapua.service.device.call.kura.app.PackageMetrics;
24: import org.eclipse.kapua.service.device.call.message.kura.app.notification.KuraNotifyChannel;
25: import org.eclipse.kapua.service.device.call.message.kura.app.notification.KuraNotifyMessage;
26: import org.eclipse.kapua.service.device.call.message.kura.app.notification.KuraNotifyPayload;
27: import org.eclipse.kapua.service.device.management.asset.internal.DeviceAssetAppProperties;
28: import org.eclipse.kapua.service.device.management.bundle.internal.DeviceBundleAppProperties;
29: import org.eclipse.kapua.service.device.management.command.internal.CommandAppProperties;
30: import org.eclipse.kapua.service.device.management.commons.message.notification.KapuaNotifyChannelImpl;
31: import org.eclipse.kapua.service.device.management.commons.message.notification.KapuaNotifyMessageImpl;
32: import org.eclipse.kapua.service.device.management.commons.message.notification.KapuaNotifyPayloadImpl;
33: import org.eclipse.kapua.service.device.management.configuration.internal.DeviceConfigurationAppProperties;
34: import org.eclipse.kapua.service.device.management.message.KapuaAppProperties;
35: import org.eclipse.kapua.service.device.management.message.notification.KapuaNotifyChannel;
36: import org.eclipse.kapua.service.device.management.message.notification.KapuaNotifyMessage;
37: import org.eclipse.kapua.service.device.management.message.notification.KapuaNotifyPayload;
38: import org.eclipse.kapua.service.device.management.message.notification.OperationStatus;
39: import org.eclipse.kapua.service.device.management.packages.message.internal.PackageAppProperties;
40: import org.eclipse.kapua.service.device.registry.Device;
41: import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
42: import org.eclipse.kapua.translator.Translator;
43: import org.eclipse.kapua.translator.exception.InvalidChannelException;
44: import org.eclipse.kapua.translator.exception.InvalidMessageException;
45: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
46: import org.eclipse.kapua.translator.exception.TranslateException;
47:
48: import java.math.BigInteger;
49: import java.util.HashMap;
50: import java.util.Map;
51:
52: /**
53: * {@link Translator} implementation from {@link KuraNotifyMessage} to {@link KapuaNotifyMessage}
54: *
55: * @since 1.0.0
56: */
57: public class TranslatorAppNotifyKuraKapua extends Translator<KuraNotifyMessage, KapuaNotifyMessage> {
58:
59: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
60:
61: private static final AccountService ACCOUNT_SERVICE = LOCATOR.getService(AccountService.class);
62: private static final DeviceRegistryService DEVICE_REGISTRY_SERVICE = LOCATOR.getService(DeviceRegistryService.class);
63:
64: private static final KapuaIdFactory KAPUA_ID_FACTORY = LOCATOR.getFactory(KapuaIdFactory.class);
65:
66: private static final Map<String, KapuaAppProperties> APP_NAME_DICTIONARY;
67: private static final Map<String, KapuaAppProperties> APP_VERSION_DICTIONARY;
68:
69: static {
70: APP_NAME_DICTIONARY = new HashMap<>();
71:
72: APP_NAME_DICTIONARY.put(AssetMetrics.APP_ID.getName(), DeviceAssetAppProperties.APP_NAME);
73: APP_NAME_DICTIONARY.put(BundleMetrics.APP_ID.getName(), DeviceBundleAppProperties.APP_NAME);
74: APP_NAME_DICTIONARY.put(CommandMetrics.APP_ID.getName(), CommandAppProperties.APP_NAME);
75: APP_NAME_DICTIONARY.put(ConfigurationMetrics.APP_ID.getName(), DeviceConfigurationAppProperties.APP_NAME);
76: APP_NAME_DICTIONARY.put(PackageMetrics.APP_ID.getName(), PackageAppProperties.APP_NAME);
77:
78: APP_VERSION_DICTIONARY = new HashMap<>();
79:
80: APP_VERSION_DICTIONARY.put(AssetMetrics.APP_ID.getName(), DeviceAssetAppProperties.APP_VERSION);
81: APP_VERSION_DICTIONARY.put(BundleMetrics.APP_ID.getName(), DeviceBundleAppProperties.APP_VERSION);
82: APP_VERSION_DICTIONARY.put(CommandMetrics.APP_ID.getName(), CommandAppProperties.APP_VERSION);
83: APP_VERSION_DICTIONARY.put(ConfigurationMetrics.APP_ID.getName(), DeviceConfigurationAppProperties.APP_VERSION);
84: APP_VERSION_DICTIONARY.put(PackageMetrics.APP_ID.getName(), PackageAppProperties.APP_VERSION);
85: }
86:
87: @Override
88: public KapuaNotifyMessage translate(KuraNotifyMessage kuraNotifyMessage) throws TranslateException {
89:
90: try {
91: KapuaNotifyMessage kapuaNotifyMessage = new KapuaNotifyMessageImpl();
92: kapuaNotifyMessage.setChannel(translate(kuraNotifyMessage.getChannel()));
93: kapuaNotifyMessage.setPayload(translate(kuraNotifyMessage.getPayload()));
94:
95: Account account = ACCOUNT_SERVICE.findByName(kuraNotifyMessage.getChannel().getScope());
96:• if (account == null) {
97: throw new KapuaEntityNotFoundException(Account.TYPE, kuraNotifyMessage.getChannel().getScope());
98: }
99:
100: Device device = DEVICE_REGISTRY_SERVICE.findByClientId(account.getId(), kuraNotifyMessage.getChannel().getClientId());
101:• if (device == null) {
102: throw new KapuaEntityNotFoundException(Device.class.toString(), kuraNotifyMessage.getChannel().getClientId());
103: }
104:
105: kapuaNotifyMessage.setDeviceId(device.getId());
106: kapuaNotifyMessage.setScopeId(account.getId());
107: kapuaNotifyMessage.setCapturedOn(kuraNotifyMessage.getPayload().getTimestamp());
108: kapuaNotifyMessage.setSentOn(kuraNotifyMessage.getPayload().getTimestamp());
109: kapuaNotifyMessage.setReceivedOn(kuraNotifyMessage.getTimestamp());
110: kapuaNotifyMessage.setPosition(TranslatorKuraKapuaUtils.translate(kuraNotifyMessage.getPayload().getPosition()));
111:
112: return kapuaNotifyMessage;
113: } catch (InvalidChannelException | InvalidPayloadException te) {
114: throw te;
115: } catch (Exception e) {
116: throw new InvalidMessageException(e, kuraNotifyMessage);
117: }
118: }
119:
120: private KapuaNotifyChannel translate(KuraNotifyChannel kuraNotifyChannel) throws InvalidChannelException {
121: try {
122: String kuraAppIdName = kuraNotifyChannel.getAppId().split("-")[0];
123: String kuraAppIdVersion = kuraNotifyChannel.getAppId().split("-")[1];
124:
125: KapuaNotifyChannel kapuaNotifyChannel = new KapuaNotifyChannelImpl();
126: kapuaNotifyChannel.setAppName(APP_NAME_DICTIONARY.get(kuraAppIdName));
127: kapuaNotifyChannel.setVersion(APP_VERSION_DICTIONARY.get(kuraAppIdVersion));
128: kapuaNotifyChannel.setResources(kuraNotifyChannel.getResources());
129:
130: return kapuaNotifyChannel;
131: } catch (Exception e) {
132: throw new InvalidChannelException(e, kuraNotifyChannel);
133: }
134: }
135:
136: private KapuaNotifyPayload translate(KuraNotifyPayload kuraNotifyPayload) throws InvalidPayloadException {
137: try {
138: KapuaNotifyPayload kapuaNotifyPayload = new KapuaNotifyPayloadImpl();
139:
140: kapuaNotifyPayload.setOperationId(KAPUA_ID_FACTORY.newKapuaId(new BigInteger(kuraNotifyPayload.getOperationId().toString())));
141: kapuaNotifyPayload.setResource(kuraNotifyPayload.getResource());
142: kapuaNotifyPayload.setProgress(kuraNotifyPayload.getProgress());
143:
144:• switch (kuraNotifyPayload.getStatus()) {
145: case "IN_PROGRESS":
146: kapuaNotifyPayload.setStatus(OperationStatus.RUNNING);
147: break;
148: case "COMPLETED":
149: kapuaNotifyPayload.setStatus(OperationStatus.COMPLETED);
150: break;
151: case "FAILED":
152: kapuaNotifyPayload.setStatus(OperationStatus.FAILED);
153: break;
154: }
155:
156: kapuaNotifyPayload.setMessage(kuraNotifyPayload.getMessage());
157:
158: return kapuaNotifyPayload;
159: } catch (Exception e) {
160: throw new InvalidPayloadException(e, kuraNotifyPayload);
161: }
162: }
163:
164: @Override
165: public Class<KuraNotifyMessage> getClassFrom() {
166: return KuraNotifyMessage.class;
167: }
168:
169: @Override
170: public Class<KapuaNotifyMessage> getClassTo() {
171: return KapuaNotifyMessage.class;
172: }
173: }