Package: RawMqttCloudConnectionFactory

RawMqttCloudConnectionFactory

nameinstructionbranchcomplexitylinemethod
RawMqttCloudConnectionFactory()
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%
createConfiguration(String)
M: 130 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 26 C: 0
0%
M: 1 C: 0
0%
deleteConfiguration(String)
M: 67 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getFactoryPid()
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%
getManagedCloudConnectionPids()
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getStackComponentsPids(String)
M: 84 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
lambda$0(ServiceReference)
M: 25 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
lambda$1(ServiceReference)
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%
setConfigurationService(ConfigurationService)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
unsetConfigurationService(ConfigurationService)
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 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: *******************************************************************************/
10: package org.eclipse.kura.cloudconnection.raw.mqtt.factory;
11:
12: import java.util.ArrayList;
13: import java.util.HashMap;
14: import java.util.List;
15: import java.util.Map;
16: import java.util.Set;
17: import java.util.regex.Pattern;
18: import java.util.stream.Collectors;
19:
20: import org.eclipse.kura.KuraErrorCode;
21: import org.eclipse.kura.KuraException;
22: import org.eclipse.kura.cloudconnection.CloudEndpoint;
23: import org.eclipse.kura.cloudconnection.factory.CloudConnectionFactory;
24: import org.eclipse.kura.configuration.ConfigurationService;
25: import org.osgi.framework.BundleContext;
26: import org.osgi.framework.FrameworkUtil;
27: import org.osgi.framework.InvalidSyntaxException;
28: import org.osgi.service.component.ComponentConstants;
29:
30: public class RawMqttCloudConnectionFactory implements CloudConnectionFactory {
31:
32: private static final String FACTORY_PID = "org.eclipse.kura.cloudconnection.raw.mqtt.factory.RawMqttCloudConnectionFactory";
33:
34: // The following constants must match the factory component definitions
35: private static final String CLOUD_ENDPOINT_FACTORY_PID = "org.eclipse.kura.cloudconnection.raw.mqtt.cloud.RawMqttCloudEndpoint";
36: private static final String DATA_SERVICE_FACTORY_PID = "org.eclipse.kura.data.DataService";
37: private static final String DATA_TRANSPORT_SERVICE_FACTORY_PID = "org.eclipse.kura.core.data.transport.mqtt.MqttDataTransport";
38:
39: private static final String CLOUD_SERVICE_PID = "org.eclipse.kura.cloudconnection.raw.mqtt.CloudEndpoint";
40: private static final String DATA_SERVICE_PID = "org.eclipse.kura.cloudconnection.raw.mqtt.DataService";
41: private static final String DATA_TRANSPORT_SERVICE_PID = "org.eclipse.kura.cloudconnection.raw.mqtt.MqttDataTransport";
42:
43: private static final String DATA_SERVICE_REFERENCE_NAME = "DataService";
44: private static final String DATA_TRANSPORT_SERVICE_REFERENCE_NAME = "DataTransportService";
45:
46: private static final String REFERENCE_TARGET_VALUE_FORMAT = "(" + ConfigurationService.KURA_SERVICE_PID + "=%s)";
47:
48: private static final Pattern MANAGED_CLOUD_SERVICE_PID_PATTERN = Pattern
49: .compile("^org\\.eclipse\\.kura\\.cloudconnection\\.raw\\.mqtt\\.CloudEndpoint(-[a-zA-Z0-9]+)?$");
50:
51: private ConfigurationService configurationService;
52:
53: protected void setConfigurationService(ConfigurationService configurationService) {
54: this.configurationService = configurationService;
55: }
56:
57: protected void unsetConfigurationService(ConfigurationService configurationService) {
58:• if (configurationService == this.configurationService) {
59: this.configurationService = null;
60: }
61: }
62:
63: @Override
64: public String getFactoryPid() {
65: return CLOUD_ENDPOINT_FACTORY_PID;
66: }
67:
68: @Override
69: public void createConfiguration(String pid) throws KuraException {
70: String[] parts = pid.split("-");
71:• if (parts.length != 0 && CLOUD_SERVICE_PID.equals(parts[0])) {
72: String suffix = null;
73:• if (parts.length > 1) {
74: suffix = parts[1];
75: }
76:
77: String dataServicePid = DATA_SERVICE_PID;
78: String dataTransportServicePid = DATA_TRANSPORT_SERVICE_PID;
79:• if (suffix != null) {
80: dataServicePid += "-" + suffix;
81: dataTransportServicePid += "-" + suffix;
82: }
83:
84: // create the CloudService layer and set the selective dependency on the DataService PID
85: Map<String, Object> cloudServiceProperties = new HashMap<>();
86: String name = DATA_SERVICE_REFERENCE_NAME + ComponentConstants.REFERENCE_TARGET_SUFFIX;
87: cloudServiceProperties.put(name, String.format(REFERENCE_TARGET_VALUE_FORMAT, dataServicePid));
88: cloudServiceProperties.put(KURA_CLOUD_CONNECTION_FACTORY_PID, FACTORY_PID);
89:
90: this.configurationService.createFactoryConfiguration(CLOUD_ENDPOINT_FACTORY_PID, pid,
91: cloudServiceProperties, false);
92:
93: // create the DataService layer and set the selective dependency on the DataTransportService PID
94: Map<String, Object> dataServiceProperties = new HashMap<>();
95: name = DATA_TRANSPORT_SERVICE_REFERENCE_NAME + ComponentConstants.REFERENCE_TARGET_SUFFIX;
96: dataServiceProperties.put(name, String.format(REFERENCE_TARGET_VALUE_FORMAT, dataTransportServicePid));
97:
98: this.configurationService.createFactoryConfiguration(DATA_SERVICE_FACTORY_PID, dataServicePid,
99: dataServiceProperties, false);
100:
101: // create the DataTransportService layer and take a snapshot
102: this.configurationService.createFactoryConfiguration(DATA_TRANSPORT_SERVICE_FACTORY_PID,
103: dataTransportServicePid, null, true);
104: } else {
105: throw new KuraException(KuraErrorCode.INVALID_PARAMETER, "Invalid PID '{}'", pid);
106: }
107: }
108:
109: @Override
110: public void deleteConfiguration(String pid) throws KuraException {
111: String[] parts = pid.split("-");
112:• if (parts.length != 0 && CLOUD_SERVICE_PID.equals(parts[0])) {
113: String suffix = null;
114:• if (parts.length > 1) {
115: suffix = parts[1];
116: }
117:
118: String dataServicePid = DATA_SERVICE_PID;
119: String dataTransportServicePid = DATA_TRANSPORT_SERVICE_PID;
120:• if (suffix != null) {
121: dataServicePid += "-" + suffix;
122: dataTransportServicePid += "-" + suffix;
123: }
124:
125: this.configurationService.deleteFactoryConfiguration(pid, false);
126: this.configurationService.deleteFactoryConfiguration(dataServicePid, false);
127: this.configurationService.deleteFactoryConfiguration(dataTransportServicePid, true);
128: }
129: }
130:
131: @Override
132: public List<String> getStackComponentsPids(String pid) throws KuraException {
133: List<String> componentPids = new ArrayList<>();
134: String[] parts = pid.split("-");
135:• if (parts.length != 0 && CLOUD_SERVICE_PID.equals(parts[0])) {
136: String suffix = null;
137:• if (parts.length > 1) {
138: suffix = parts[1];
139: }
140:
141: String dataServicePid = DATA_SERVICE_PID;
142: String dataTransportServicePid = DATA_TRANSPORT_SERVICE_PID;
143:• if (suffix != null) {
144: dataServicePid += "-" + suffix;
145: dataTransportServicePid += "-" + suffix;
146: }
147:
148: componentPids.add(pid);
149: componentPids.add(dataServicePid);
150: componentPids.add(dataTransportServicePid);
151: return componentPids;
152: } else {
153: throw new KuraException(KuraErrorCode.INVALID_PARAMETER, "Invalid PID '{}'", pid);
154: }
155: }
156:
157: @Override
158: public Set<String> getManagedCloudConnectionPids() throws KuraException {
159:
160: final BundleContext context = FrameworkUtil.getBundle(RawMqttCloudConnectionFactory.class).getBundleContext();
161:
162: try {
163: return context.getServiceReferences(CloudEndpoint.class, null).stream().filter(ref -> {
164: final Object kuraServicePid = ref.getProperty(ConfigurationService.KURA_SERVICE_PID);
165:
166:• if (!(kuraServicePid instanceof String)) {
167: return false;
168: }
169:
170:• return MANAGED_CLOUD_SERVICE_PID_PATTERN.matcher((String) kuraServicePid).matches()
171:• && (FACTORY_PID.equals(ref.getProperty(KURA_CLOUD_CONNECTION_FACTORY_PID)));
172: }).map(ref -> (String) ref.getProperty(ConfigurationService.KURA_SERVICE_PID)).collect(Collectors.toSet());
173: } catch (InvalidSyntaxException e) {
174: throw new KuraException(KuraErrorCode.CONFIGURATION_ATTRIBUTE_INVALID, e);
175: }
176: }
177: }