Package: DeviceConnectionServiceImpl
DeviceConnectionServiceImpl
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DeviceConnectionServiceImpl() |
|
|
|
|
|
||||||||||||||||||||
DeviceConnectionServiceImpl(DeviceEntityManagerFactory) |
|
|
|
|
|
||||||||||||||||||||
connect(DeviceConnectionCreator) |
|
|
|
|
|
||||||||||||||||||||
count(KapuaQuery) |
|
|
|
|
|
||||||||||||||||||||
create(DeviceConnectionCreator) |
|
|
|
|
|
||||||||||||||||||||
delete(KapuaId, KapuaId) |
|
|
|
|
|
||||||||||||||||||||
deleteConnectionByAccountId(KapuaId, KapuaId) |
|
|
|
|
|
||||||||||||||||||||
disconnect(KapuaId, String) |
|
|
|
|
|
||||||||||||||||||||
find(KapuaId, KapuaId) |
|
|
|
|
|
||||||||||||||||||||
findByClientId(KapuaId, String) |
|
|
|
|
|
||||||||||||||||||||
lambda$count$5(KapuaQuery, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$create$0(DeviceConnectionCreator, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$delete$6(KapuaId, KapuaId, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$delete$7(KapuaId, KapuaId, Object) |
|
|
|
|
|
||||||||||||||||||||
lambda$find$3(KapuaId, KapuaId, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$query$4(KapuaQuery, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$update$1(DeviceConnection, EntityManager) |
|
|
|
|
|
||||||||||||||||||||
lambda$update$2(DeviceConnection) |
|
|
|
|
|
||||||||||||||||||||
onKapuaEvent(ServiceEvent) |
|
|
|
|
|
||||||||||||||||||||
query(KapuaQuery) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
update(DeviceConnection) |
|
|
|
|
|
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.registry.connection.internal;
14:
15: import org.eclipse.kapua.KapuaDuplicateNameException;
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableService;
19: import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
20: import org.eclipse.kapua.commons.util.ArgumentValidator;
21: import org.eclipse.kapua.event.ServiceEvent;
22: import org.eclipse.kapua.locator.KapuaLocator;
23: import org.eclipse.kapua.locator.KapuaProvider;
24: import org.eclipse.kapua.model.domain.Actions;
25: import org.eclipse.kapua.model.id.KapuaId;
26: import org.eclipse.kapua.model.query.KapuaQuery;
27: import org.eclipse.kapua.service.authorization.AuthorizationService;
28: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
29: import org.eclipse.kapua.service.device.registry.DeviceDomains;
30: import org.eclipse.kapua.service.device.registry.common.DeviceValidationRegex;
31: import org.eclipse.kapua.service.device.registry.connection.DeviceConnection;
32: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionAttributes;
33: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionCreator;
34: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory;
35: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionListResult;
36: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery;
37: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService;
38: import org.eclipse.kapua.service.device.registry.internal.DeviceEntityManagerFactory;
39: import org.eclipse.kapua.service.device.registry.internal.DeviceRegistryCache;
40: import org.eclipse.kapua.service.device.registry.internal.DeviceRegistryCacheFactory;
41: import org.slf4j.Logger;
42: import org.slf4j.LoggerFactory;
43:
44: /**
45: * DeviceConnectionService exposes APIs to retrieve Device connections under a scope.
46: * It includes APIs to find, list, and update devices connections associated with a scope.
47: *
48: * @since 1.0
49: */
50: @KapuaProvider
51: public class DeviceConnectionServiceImpl extends AbstractKapuaConfigurableService implements DeviceConnectionService {
52:
53: private static final Logger LOGGER = LoggerFactory.getLogger(DeviceConnectionServiceImpl.class);
54:
55: public DeviceConnectionServiceImpl() {
56: this(DeviceEntityManagerFactory.instance());
57: }
58:
59: public DeviceConnectionServiceImpl(DeviceEntityManagerFactory deviceEntityManagerFactory) {
60: super(DeviceConnectionService.class.getName(), DeviceDomains.DEVICE_CONNECTION_DOMAIN,
61: deviceEntityManagerFactory, DeviceRegistryCacheFactory.getInstance());
62: }
63:
64: @Override
65: public DeviceConnection create(DeviceConnectionCreator deviceConnectionCreator)
66: throws KapuaException {
67: //
68: // Argument Validation
69: ArgumentValidator.notNull(deviceConnectionCreator, "deviceConnectionCreator");
70: ArgumentValidator.notNull(deviceConnectionCreator.getScopeId(), "deviceConnectionCreator.scopeId");
71: ArgumentValidator.notNull(deviceConnectionCreator.getUserId(), "deviceConnectionCreator.userId");
72: ArgumentValidator.notEmptyOrNull(deviceConnectionCreator.getClientId(), "deviceConnectionCreator.clientId");
73: ArgumentValidator.lengthRange(deviceConnectionCreator.getClientId(), 1, 255, "deviceCreator.clientId");
74: ArgumentValidator.match(deviceConnectionCreator.getClientId(), DeviceValidationRegex.CLIENT_ID, "deviceCreator.clientId");
75:
76: //
77: // Check Access
78: KapuaLocator locator = KapuaLocator.getInstance();
79: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
80: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
81: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_CONNECTION_DOMAIN, Actions.write, null));
82:
83: //
84: // Check duplicate clientId
85: DeviceConnectionQuery query = new DeviceConnectionQueryImpl(deviceConnectionCreator.getScopeId());
86: query.setPredicate(query.attributePredicate(DeviceConnectionAttributes.CLIENT_ID, deviceConnectionCreator.getClientId()));
87:
88:• if (count(query) > 0) {
89: throw new KapuaDuplicateNameException(deviceConnectionCreator.getClientId());
90: }
91:
92: //
93: // Do create
94: return entityManagerSession.doTransactedAction(EntityManagerContainer.<DeviceConnection>create().onResultHandler(entityManager -> DeviceConnectionDAO.create(entityManager, deviceConnectionCreator)));
95: }
96:
97: @Override
98: public DeviceConnection update(DeviceConnection deviceConnection)
99: throws KapuaException {
100: //
101: // Argument Validation
102: ArgumentValidator.notNull(deviceConnection, "deviceConnection");
103: ArgumentValidator.notNull(deviceConnection.getId(), "deviceConnection.id");
104: ArgumentValidator.notNull(deviceConnection.getScopeId(), "deviceConnection.scopeId");
105:
106: //
107: // Check Access
108: KapuaLocator locator = KapuaLocator.getInstance();
109: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
110: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
111: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_CONNECTION_DOMAIN, Actions.write, null));
112:
113: return entityManagerSession.doTransactedAction(EntityManagerContainer.<DeviceConnection>create().onResultHandler(entityManager -> {
114:• if (DeviceConnectionDAO.find(entityManager, deviceConnection.getScopeId(), deviceConnection.getId()) == null) {
115: throw new KapuaEntityNotFoundException(DeviceConnection.TYPE, deviceConnection.getId());
116: }
117: return DeviceConnectionDAO.update(entityManager, deviceConnection);
118: }).onBeforeHandler(() -> {
119: ((DeviceRegistryCache) entityCache).removeByDeviceConnectionId(deviceConnection.getScopeId(), deviceConnection.getId());
120: return null;
121: }));
122: }
123:
124: @Override
125: public DeviceConnection find(KapuaId scopeId, KapuaId entityId)
126: throws KapuaException {
127: //
128: // Argument Validation
129: ArgumentValidator.notNull(scopeId, "scopeId");
130: ArgumentValidator.notNull(entityId, "entityId");
131:
132: //
133: // Check Access
134: KapuaLocator locator = KapuaLocator.getInstance();
135: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
136: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
137: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_CONNECTION_DOMAIN, Actions.read, scopeId));
138:
139: return entityManagerSession.doAction(EntityManagerContainer.<DeviceConnection>create()
140: .onResultHandler(entityManager -> DeviceConnectionDAO.find(entityManager, scopeId, entityId)));
141: }
142:
143: @Override
144: public DeviceConnection findByClientId(KapuaId scopeId, String clientId)
145: throws KapuaException {
146: //
147: // Argument Validation
148: ArgumentValidator.notNull(scopeId, "scopeId");
149: ArgumentValidator.notEmptyOrNull(clientId, "clientId");
150:
151: //
152: // Build query
153: DeviceConnectionQueryImpl query = new DeviceConnectionQueryImpl(scopeId);
154: query.setPredicate(query.attributePredicate(DeviceConnectionAttributes.CLIENT_ID, clientId));
155:
156: //
157: // Query and parse result
158: DeviceConnectionListResult result = query(query);
159: return result.getFirstItem();
160: }
161:
162: @Override
163: public DeviceConnectionListResult query(KapuaQuery query)
164: throws KapuaException {
165: //
166: // Argument Validation
167: ArgumentValidator.notNull(query, "query");
168:
169: //
170: // Check Access
171: KapuaLocator locator = KapuaLocator.getInstance();
172: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
173: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
174: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_CONNECTION_DOMAIN, Actions.read, query.getScopeId()));
175:
176: return entityManagerSession.doAction(EntityManagerContainer.<DeviceConnectionListResult>create().onResultHandler(entityManager -> DeviceConnectionDAO.query(entityManager, query)));
177: }
178:
179: @Override
180: public long count(KapuaQuery query)
181: throws KapuaException {
182: //
183: // Argument Validation
184: ArgumentValidator.notNull(query, "query");
185:
186: //
187: // Check Access
188: KapuaLocator locator = KapuaLocator.getInstance();
189: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
190: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
191: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_CONNECTION_DOMAIN, Actions.read, query.getScopeId()));
192:
193: return entityManagerSession.doAction(EntityManagerContainer.<Long>create().onResultHandler(entityManager -> DeviceConnectionDAO.count(entityManager, query)));
194: }
195:
196: @Override
197: public void delete(KapuaId scopeId, KapuaId deviceConnectionId)
198: throws KapuaException {
199: //
200: // Argument Validation
201: ArgumentValidator.notNull(deviceConnectionId, "deviceConnection.id");
202: ArgumentValidator.notNull(scopeId, "deviceConnection.scopeId");
203:
204: //
205: // Check Access
206: KapuaLocator locator = KapuaLocator.getInstance();
207: AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
208: PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
209: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_CONNECTION_DOMAIN, Actions.write, null));
210:
211: entityManagerSession.doTransactedAction(EntityManagerContainer.create().onResultHandler(entityManager -> {
212: // TODO: check if it is correct to remove this statement (already thrown by the delete method, but
213: // without TYPE)
214:• if (DeviceConnectionDAO.find(entityManager, scopeId, deviceConnectionId) == null) {
215: throw new KapuaEntityNotFoundException(DeviceConnection.TYPE, deviceConnectionId);
216: }
217: return DeviceConnectionDAO.delete(entityManager, scopeId, deviceConnectionId);
218: }).onAfterHandler((emptyParam) -> ((DeviceRegistryCache) entityCache).removeByDeviceConnectionId(scopeId, deviceConnectionId)));
219: }
220:
221: @Override
222: public void connect(DeviceConnectionCreator creator)
223: throws KapuaException {
224: // TODO Auto-generated method stub
225:
226: }
227:
228: @Override
229: public void disconnect(KapuaId scopeId, String clientId)
230: throws KapuaException {
231: // TODO Auto-generated method stub
232:
233: }
234:
235: //@ListenServiceEvent(fromAddress="account")
236: public void onKapuaEvent(ServiceEvent kapuaEvent) throws KapuaException {
237:• if (kapuaEvent == null) {
238: //service bus error. Throw some exception?
239: }
240: LOGGER.info("DeviceConnectionService: received kapua event from {}, operation {}", kapuaEvent.getService(), kapuaEvent.getOperation());
241:• if ("account".equals(kapuaEvent.getService()) && "delete".equals(kapuaEvent.getOperation())) {
242: deleteConnectionByAccountId(kapuaEvent.getScopeId(), kapuaEvent.getEntityId());
243: }
244: }
245:
246: private void deleteConnectionByAccountId(KapuaId scopeId, KapuaId accountId) throws KapuaException {
247: KapuaLocator locator = KapuaLocator.getInstance();
248: DeviceConnectionFactory deviceConnectionFactory = locator.getFactory(DeviceConnectionFactory.class);
249:
250: DeviceConnectionQuery query = deviceConnectionFactory.newQuery(accountId);
251:
252: DeviceConnectionListResult deviceConnectionsToDelete = query(query);
253:
254:• for (DeviceConnection dc : deviceConnectionsToDelete.getItems()) {
255: delete(dc.getScopeId(), dc.getId());
256: }
257: }
258:
259: }