Skip to content

Package: DeviceConfigurationManagementServiceImpl

DeviceConfigurationManagementServiceImpl

nameinstructionbranchcomplexitylinemethod
DeviceConfigurationManagementServiceImpl()
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%
get(KapuaId, KapuaId, String, String, Long)
M: 179 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 42 C: 0
0%
M: 1 C: 0
0%
lambda$get$0(ConfigurationResponseMessage)
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%
put(KapuaId, KapuaId, DeviceComponentConfiguration, Long)
M: 127 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 34 C: 0
0%
M: 1 C: 0
0%
put(KapuaId, KapuaId, DeviceConfiguration, Long)
M: 111 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 30 C: 0
0%
M: 1 C: 0
0%
put(KapuaId, KapuaId, String, Long)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 14 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: *******************************************************************************/
13: package org.eclipse.kapua.service.device.management.configuration.internal;
14:
15: import com.google.common.base.Strings;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.KapuaIllegalArgumentException;
18: import org.eclipse.kapua.commons.util.ArgumentValidator;
19: import org.eclipse.kapua.commons.util.xml.XmlUtil;
20: import org.eclipse.kapua.locator.KapuaLocator;
21: import org.eclipse.kapua.locator.KapuaProvider;
22: import org.eclipse.kapua.model.domain.Actions;
23: import org.eclipse.kapua.model.id.KapuaId;
24: import org.eclipse.kapua.service.device.management.DeviceManagementDomains;
25: import org.eclipse.kapua.service.device.management.commons.AbstractDeviceManagementServiceImpl;
26: import org.eclipse.kapua.service.device.management.commons.call.DeviceCallBuilder;
27: import org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration;
28: import org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration;
29: import org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory;
30: import org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService;
31: import org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel;
32: import org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestMessage;
33: import org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestPayload;
34: import org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponseMessage;
35: import org.eclipse.kapua.service.device.management.configuration.store.DeviceConfigurationStoreService;
36: import org.eclipse.kapua.service.device.management.exception.DeviceManagementRequestContentException;
37: import org.eclipse.kapua.service.device.management.exception.DeviceNeverConnectedException;
38: import org.eclipse.kapua.service.device.management.message.KapuaMethod;
39: import org.slf4j.Logger;
40: import org.slf4j.LoggerFactory;
41: import org.xml.sax.SAXException;
42:
43: import javax.xml.bind.JAXBException;
44: import java.util.Date;
45:
46: /**
47: * {@link DeviceConfigurationManagementService} implementation.
48: *
49: * @since 1.0.0
50: */
51: @KapuaProvider
52: public class DeviceConfigurationManagementServiceImpl extends AbstractDeviceManagementServiceImpl implements DeviceConfigurationManagementService {
53:
54: private static final Logger LOG = LoggerFactory.getLogger(DeviceConfigurationManagementServiceImpl.class);
55:
56: private static final DeviceConfigurationFactory DEVICE_CONFIGURATION_FACTORY = LOCATOR.getFactory(DeviceConfigurationFactory.class);
57:
58: private static final DeviceConfigurationStoreService CONFIGURATION_STORE_SERVICE = KapuaLocator.getInstance().getService(DeviceConfigurationStoreService.class);
59:
60: private static final String SCOPE_ID = "scopeId";
61: private static final String DEVICE_ID = "deviceId";
62:
63: @Override
64: public DeviceConfiguration get(KapuaId scopeId, KapuaId deviceId, String configurationId, String configurationComponentPid, Long timeout)
65: throws KapuaException {
66: //
67: // Argument Validation
68: ArgumentValidator.notNull(scopeId, SCOPE_ID);
69: ArgumentValidator.notNull(deviceId, DEVICE_ID);
70:
71: //
72: // Check Access
73: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.read, scopeId));
74:
75: //
76: // Prepare the request
77: ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
78: configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
79: configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
80: configurationRequestChannel.setMethod(KapuaMethod.READ);
81: configurationRequestChannel.setConfigurationId(configurationId);
82: configurationRequestChannel.setComponentId(configurationComponentPid);
83:
84: ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
85:
86: ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
87: configurationRequestMessage.setScopeId(scopeId);
88: configurationRequestMessage.setDeviceId(deviceId);
89: configurationRequestMessage.setCapturedOn(new Date());
90: configurationRequestMessage.setPayload(configurationRequestPayload);
91: configurationRequestMessage.setChannel(configurationRequestChannel);
92:
93: //
94: // Build request
95: DeviceCallBuilder<ConfigurationRequestChannel, ConfigurationRequestPayload, ConfigurationRequestMessage, ConfigurationResponseMessage> configurationDeviceCallBuilder =
96: DeviceCallBuilder
97: .newBuilder()
98: .withRequestMessage(configurationRequestMessage)
99: .withTimeoutOrDefault(timeout);
100:
101: //
102: // Do get
103:• if (isDeviceConnected(scopeId, deviceId)) {
104: ConfigurationResponseMessage responseMessage;
105: try {
106: responseMessage = configurationDeviceCallBuilder.send();
107: } catch (Exception e) {
108: LOG.error("Error while getting DeviceConfiguration with id {} and DeviceComponentConfiguration id {} for Device {}. Error: {}", configurationId, configurationComponentPid, deviceId, e.getMessage(), e);
109: throw e;
110: }
111:
112: //
113: // Create event
114: createDeviceEvent(scopeId, deviceId, configurationRequestMessage, responseMessage);
115:
116: //
117: // Check response
118: DeviceConfiguration onlineDeviceConfiguration = checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getDeviceConfigurations());
119:
120: //
121: // Store config and return
122:• if (CONFIGURATION_STORE_SERVICE.isServiceEnabled(scopeId) &&
123:• CONFIGURATION_STORE_SERVICE.isApplicationEnabled(scopeId, deviceId)) {
124:• if (Strings.isNullOrEmpty(configurationComponentPid)) {
125: // If all DeviceConfiguration has been requested, store it overriding any previous value
126: CONFIGURATION_STORE_SERVICE.storeConfigurations(scopeId, deviceId, onlineDeviceConfiguration);
127: } else {
128: // If only one DeviceComponentConfiguration has been requested, store it overriding only the selected DeviceComponentConfiguration
129: CONFIGURATION_STORE_SERVICE.storeConfigurations(scopeId, deviceId, onlineDeviceConfiguration.getComponentConfigurations().get(0));
130: }
131: }
132:
133: return onlineDeviceConfiguration;
134: } else {
135:• if (CONFIGURATION_STORE_SERVICE.isServiceEnabled(scopeId) &&
136:• CONFIGURATION_STORE_SERVICE.isApplicationEnabled(scopeId, deviceId)) {
137:• if (configurationComponentPid == null) {
138: return CONFIGURATION_STORE_SERVICE.getConfigurations(scopeId, deviceId);
139: } else {
140: DeviceConfiguration deviceConfigurationReturned = DEVICE_CONFIGURATION_FACTORY.newConfigurationInstance();
141: DeviceComponentConfiguration componentConfiguration = CONFIGURATION_STORE_SERVICE.getConfigurations(scopeId, deviceId, configurationComponentPid);
142: deviceConfigurationReturned.addComponentConfiguration(componentConfiguration);
143: return deviceConfigurationReturned;
144: }
145: } else {
146: throw new DeviceNeverConnectedException(deviceId);
147: }
148: }
149: }
150:
151: @Override
152: public void put(KapuaId scopeId, KapuaId deviceId, DeviceComponentConfiguration deviceComponentConfiguration, Long timeout)
153: throws KapuaException {
154: //
155: // Argument Validation
156: ArgumentValidator.notNull(scopeId, SCOPE_ID);
157: ArgumentValidator.notNull(deviceId, DEVICE_ID);
158: ArgumentValidator.notNull(deviceComponentConfiguration, "componentConfiguration");
159: ArgumentValidator.notEmptyOrNull(deviceComponentConfiguration.getId(), "componentConfiguration.componentId");
160:
161: //
162: // Check Access
163: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.write, scopeId));
164:
165: //
166: // Prepare the request
167: ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
168: configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
169: configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
170: configurationRequestChannel.setMethod(KapuaMethod.WRITE);
171: configurationRequestChannel.setComponentId(deviceComponentConfiguration.getId());
172:
173: ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
174:
175: DeviceConfiguration deviceConfiguration = DEVICE_CONFIGURATION_FACTORY.newConfigurationInstance();
176: try {
177: deviceConfiguration.getComponentConfigurations().add(deviceComponentConfiguration);
178:
179: configurationRequestPayload.setDeviceConfigurations(deviceConfiguration);
180: } catch (Exception e) {
181: throw new DeviceManagementRequestContentException(e, deviceConfiguration);
182: }
183:
184: ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
185: configurationRequestMessage.setScopeId(scopeId);
186: configurationRequestMessage.setDeviceId(deviceId);
187: configurationRequestMessage.setCapturedOn(new Date());
188: configurationRequestMessage.setPayload(configurationRequestPayload);
189: configurationRequestMessage.setChannel(configurationRequestChannel);
190:
191: //
192: // Build request
193: DeviceCallBuilder<ConfigurationRequestChannel, ConfigurationRequestPayload, ConfigurationRequestMessage, ConfigurationResponseMessage> configurationDeviceCallBuilder =
194: DeviceCallBuilder
195: .newBuilder()
196: .withRequestMessage(configurationRequestMessage)
197: .withTimeoutOrDefault(timeout);
198:
199: //
200: // Do put
201: ConfigurationResponseMessage responseMessage;
202: try {
203: responseMessage = configurationDeviceCallBuilder.send();
204: } catch (Exception e) {
205: LOG.error("Error while putting DeviceComponentConfiguration {} for Device {}. Error: {}", deviceComponentConfiguration, deviceId, e.getMessage(), e);
206: throw e;
207: }
208:
209: //
210: // Create event
211: createDeviceEvent(scopeId, deviceId, configurationRequestMessage, responseMessage);
212:
213: //
214: // Check response
215: checkResponseAcceptedOrThrowError(responseMessage);
216: }
217:
218: @Override
219: public void put(KapuaId scopeId, KapuaId deviceId, String xmlDeviceConfig, Long timeout)
220: throws KapuaException {
221: try {
222: put(scopeId,
223: deviceId,
224: XmlUtil.unmarshal(xmlDeviceConfig, DeviceConfigurationImpl.class),
225: timeout);
226: } catch (JAXBException | SAXException e) {
227: throw new KapuaIllegalArgumentException("xmlDeviceConfig", xmlDeviceConfig);
228: }
229: }
230:
231: @Override
232: public void put(KapuaId scopeId, KapuaId deviceId, DeviceConfiguration deviceConfiguration, Long timeout)
233: throws KapuaException {
234: //
235: // Argument Validation
236: ArgumentValidator.notNull(scopeId, SCOPE_ID);
237: ArgumentValidator.notNull(deviceId, DEVICE_ID);
238: ArgumentValidator.notNull(deviceConfiguration, "componentConfiguration");
239:
240: //
241: // Check Access
242: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.write, scopeId));
243:
244: //
245: // Prepare the request
246: ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
247: configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
248: configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
249: configurationRequestChannel.setMethod(KapuaMethod.WRITE);
250:
251: ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
252:
253: try {
254: configurationRequestPayload.setDeviceConfigurations(deviceConfiguration);
255: } catch (Exception e) {
256: throw new DeviceManagementRequestContentException(e, deviceConfiguration);
257: }
258:
259: ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
260: configurationRequestMessage.setScopeId(scopeId);
261: configurationRequestMessage.setDeviceId(deviceId);
262: configurationRequestMessage.setCapturedOn(new Date());
263: configurationRequestMessage.setPayload(configurationRequestPayload);
264: configurationRequestMessage.setChannel(configurationRequestChannel);
265:
266: //
267: // Build request
268: DeviceCallBuilder<ConfigurationRequestChannel, ConfigurationRequestPayload, ConfigurationRequestMessage, ConfigurationResponseMessage> configurationDeviceCallBuilder =
269: DeviceCallBuilder
270: .newBuilder()
271: .withRequestMessage(configurationRequestMessage)
272: .withTimeoutOrDefault(timeout);
273:
274: //
275: // Do put
276: ConfigurationResponseMessage responseMessage;
277: try {
278: responseMessage = configurationDeviceCallBuilder.send();
279: } catch (Exception e) {
280: LOG.error("Error while putting DeviceConfiguration {} for Device {}. Error: {}", deviceConfiguration, deviceId, e.getMessage(), e);
281: throw e;
282: }
283:
284: //
285: // Create event
286: createDeviceEvent(scopeId, deviceId, configurationRequestMessage, responseMessage);
287:
288: //
289: // Check response
290: checkResponseAcceptedOrThrowError(responseMessage);
291: }
292: }