Skip to content

Package: AbstractTranslatorAppKeystoreKuraKapua

AbstractTranslatorAppKeystoreKuraKapua

nameinstructionbranchcomplexitylinemethod
AbstractTranslatorAppKeystoreKuraKapua(Class)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$translate$0(KuraKeystore)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
translate(KuraKeystoreCSR)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
translate(KuraKeystoreItem)
M: 102 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
translate(KuraKeystoreItem[])
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
translate(KuraKeystore[])
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
translateChannel(KuraResponseChannel)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2021, 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.kapua.keystore;
14:
15: import org.eclipse.kapua.locator.KapuaLocator;
16: import org.eclipse.kapua.service.device.call.kura.model.keystore.KeystoreMetrics;
17: import org.eclipse.kapua.service.device.call.kura.model.keystore.KuraKeystore;
18: import org.eclipse.kapua.service.device.call.kura.model.keystore.KuraKeystoreCSR;
19: import org.eclipse.kapua.service.device.call.kura.model.keystore.KuraKeystoreItem;
20: import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponseChannel;
21: import org.eclipse.kapua.service.device.call.message.kura.app.response.KuraResponseMessage;
22: import org.eclipse.kapua.service.device.management.keystore.DeviceKeystoreManagementFactory;
23: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreResponseChannel;
24: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreResponseMessage;
25: import org.eclipse.kapua.service.device.management.keystore.internal.message.response.KeystoreResponsePayload;
26: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystore;
27: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreCSR;
28: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreItem;
29: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreItems;
30: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystoreSubjectAN;
31: import org.eclipse.kapua.service.device.management.keystore.model.DeviceKeystores;
32: import org.eclipse.kapua.translator.Translator;
33: import org.eclipse.kapua.translator.exception.InvalidChannelException;
34: import org.eclipse.kapua.translator.kura.kapua.AbstractSimpleTranslatorResponseKuraKapua;
35: import org.eclipse.kapua.translator.kura.kapua.TranslatorKuraKapuaUtils;
36: import org.slf4j.Logger;
37: import org.slf4j.LoggerFactory;
38:
39: import java.util.Arrays;
40: import java.util.Date;
41: import java.util.stream.Collectors;
42:
43: /**
44: * {@link Translator} {@code abstract} implementation from {@link KuraResponseMessage} to {@link KeystoreResponseMessage}
45: *
46: * @since 1.5.0
47: */
48: public abstract class AbstractTranslatorAppKeystoreKuraKapua<M extends KeystoreResponseMessage> extends AbstractSimpleTranslatorResponseKuraKapua<KeystoreResponseChannel, KeystoreResponsePayload, M> {
49:
50: private static final Logger LOG = LoggerFactory.getLogger(AbstractTranslatorAppKeystoreKuraKapua.class);
51:
52: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
53:
54: private final static DeviceKeystoreManagementFactory DEVICE_KEYSTORE_MANAGEMENT_FACTORY = LOCATOR.getFactory(DeviceKeystoreManagementFactory.class);
55:
56: /**
57: * Constructor.
58: *
59: * @param responseMessageClass The type of the {@link KeystoreResponseMessage}.
60: * @since 1.5.0
61: */
62: public AbstractTranslatorAppKeystoreKuraKapua(Class<M> responseMessageClass) {
63: super(responseMessageClass, KeystoreResponsePayload.class);
64: }
65:
66: @Override
67: protected KeystoreResponseChannel translateChannel(KuraResponseChannel kuraResponseChannel) throws InvalidChannelException {
68: try {
69: TranslatorKuraKapuaUtils.validateKuraResponseChannel(kuraResponseChannel, KeystoreMetrics.APP_ID, KeystoreMetrics.APP_VERSION);
70:
71: return new KeystoreResponseChannel();
72: } catch (Exception e) {
73: throw new InvalidChannelException(e, kuraResponseChannel);
74: }
75: }
76:
77: /**
78: * Translates a {@link KuraKeystore}[] to {@link DeviceKeystores}.
79: *
80: * @param kuraKeystoreArray The {@link KuraKeystore}[] to translate.
81: * @return The translated {@link DeviceKeystores}.
82: * @since 1.5.0
83: */
84: protected DeviceKeystores translate(KuraKeystore[] kuraKeystoreArray) {
85:
86: DeviceKeystores deviceKeystores = DEVICE_KEYSTORE_MANAGEMENT_FACTORY.newDeviceKeystores();
87:
88: deviceKeystores.setKeystores(
89: Arrays.stream(kuraKeystoreArray).map(kuraKeystore -> {
90: DeviceKeystore deviceKeystore = DEVICE_KEYSTORE_MANAGEMENT_FACTORY.newDeviceKeystore();
91:
92: deviceKeystore.setId(kuraKeystore.getKeystoreServicePid());
93: deviceKeystore.setKeystoreType(kuraKeystore.getType());
94: deviceKeystore.setSize(kuraKeystore.getSize());
95:
96: return deviceKeystore;
97: }).collect(Collectors.toList())
98: );
99:
100: return deviceKeystores;
101: }
102:
103: /**
104: * Translates a {@link KuraKeystoreItem}[] to {@link DeviceKeystoreItems}.
105: *
106: * @param kuraKeystoreItemArray The {@link KuraKeystoreItem}[] to translate.
107: * @return The translated {@link DeviceKeystoreItems}.
108: * @since 1.5.0
109: */
110: protected DeviceKeystoreItems translate(KuraKeystoreItem[] kuraKeystoreItemArray) {
111:
112: DeviceKeystoreItems deviceKeystoreItems = DEVICE_KEYSTORE_MANAGEMENT_FACTORY.newDeviceKeystoreItems();
113:
114: deviceKeystoreItems.setKeystoreItems(
115: Arrays.stream(kuraKeystoreItemArray)
116: .map(this::translate)
117: .collect(Collectors.toList())
118: );
119:
120: return deviceKeystoreItems;
121: }
122:
123: /**
124: * Translates a {@link KuraKeystoreItem}to {@link DeviceKeystoreItems}.
125: *
126: * @param kuraKeystoreItem The {@link KuraKeystoreItem} to translate.
127: * @return The translated {@link DeviceKeystoreItems}.
128: * @since 1.5.0
129: */
130: protected DeviceKeystoreItem translate(KuraKeystoreItem kuraKeystoreItem) {
131:
132: DeviceKeystoreItem deviceKeystore = DEVICE_KEYSTORE_MANAGEMENT_FACTORY.newDeviceKeystoreItem();
133:
134: deviceKeystore.setKeystoreId(kuraKeystoreItem.getKeystoreServicePid());
135: deviceKeystore.setItemType(kuraKeystoreItem.getType());
136: deviceKeystore.setSize(kuraKeystoreItem.getSize());
137: deviceKeystore.setAlgorithm(kuraKeystoreItem.getAlgorithm());
138: deviceKeystore.setAlias(kuraKeystoreItem.getAlias());
139: deviceKeystore.setSubjectDN(kuraKeystoreItem.getSubjectDN());
140: deviceKeystore.setIssuer(kuraKeystoreItem.getIssuer());
141: deviceKeystore.setCertificate(kuraKeystoreItem.getCertificate());
142: deviceKeystore.setCertificateChain(kuraKeystoreItem.getCertificateChain());
143:
144:• for (String[] kuraSubjectAN : kuraKeystoreItem.getSubjectAN()) {
145: DeviceKeystoreSubjectAN deviceSubjectAN = DEVICE_KEYSTORE_MANAGEMENT_FACTORY.newDeviceKeystoreSubjectAN();
146:
147:• if (kuraSubjectAN == null || kuraSubjectAN.length != 2) {
148: LOG.warn("Invalid Subject Alternative Names provided from the device: {}", (Object) kuraSubjectAN);
149: continue;
150: }
151:
152: deviceSubjectAN.setANType(kuraSubjectAN[0]);
153: deviceSubjectAN.setValue(kuraSubjectAN[1]);
154:
155: deviceKeystore.addSubjectAN(deviceSubjectAN);
156: }
157:
158:• if (kuraKeystoreItem.getStartDate() != null) {
159: deviceKeystore.setNotBefore(new Date(kuraKeystoreItem.getStartDate()));
160: }
161:
162:• if (kuraKeystoreItem.getExpirationDate() != null) {
163: deviceKeystore.setNotAfter(new Date(kuraKeystoreItem.getExpirationDate()));
164: }
165:
166: return deviceKeystore;
167: }
168:
169: /**
170: * Translates a {@link KuraKeystoreCSR} to {@link DeviceKeystoreCSR}.
171: *
172: * @param kuraKeystoreCSR The {@link KuraKeystoreCSR} to translate.
173: * @return The translated {@link DeviceKeystoreCSR}.
174: * @since 1.5.0
175: */
176: protected DeviceKeystoreCSR translate(KuraKeystoreCSR kuraKeystoreCSR) {
177: DeviceKeystoreCSR deviceKeystoreCSR = DEVICE_KEYSTORE_MANAGEMENT_FACTORY.newDeviceKeystoreCSR();
178:
179: deviceKeystoreCSR.setSigningRequest(kuraKeystoreCSR.getSigningRequest());
180:
181: return deviceKeystoreCSR;
182: }
183: }