Skip to content

Package: DeviceEventServiceImpl

DeviceEventServiceImpl

nameinstructionbranchcomplexitylinemethod
DeviceEventServiceImpl()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
count(KapuaQuery)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(DeviceEventCreator)
M: 56 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
delete(KapuaId, KapuaId)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
find(KapuaId, KapuaId)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
lambda$count$3(KapuaQuery, EntityManager)
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%
lambda$create$0(DeviceEventCreator, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$delete$4(KapuaId, KapuaId, EntityManager)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
lambda$find$1(KapuaId, KapuaId, EntityManager)
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%
lambda$query$2(KapuaQuery, EntityManager)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
query(KapuaQuery)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 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: 1 C: 0
0%
M: 1 C: 0
0%
updateLastEventOnDevice(DeviceEvent)
M: 95 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 19 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.registry.event.internal;
14:
15: import org.eclipse.kapua.KapuaEntityNotFoundException;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.KapuaOptimisticLockingException;
18: import org.eclipse.kapua.commons.service.internal.AbstractKapuaService;
19: import org.eclipse.kapua.commons.util.ArgumentValidator;
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.model.query.KapuaQuery;
25: import org.eclipse.kapua.service.authorization.AuthorizationService;
26: import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
27: import org.eclipse.kapua.service.device.registry.Device;
28: import org.eclipse.kapua.service.device.registry.DeviceDomains;
29: import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
30: import org.eclipse.kapua.service.device.registry.event.DeviceEvent;
31: import org.eclipse.kapua.service.device.registry.event.DeviceEventCreator;
32: import org.eclipse.kapua.service.device.registry.event.DeviceEventListResult;
33: import org.eclipse.kapua.service.device.registry.event.DeviceEventService;
34: import org.eclipse.kapua.service.device.registry.internal.DeviceEntityManagerFactory;
35: import org.slf4j.Logger;
36: import org.slf4j.LoggerFactory;
37:
38: import javax.inject.Inject;
39:
40: /**
41: * {@link DeviceEventService} implementation.
42: *
43: * @since 1.0.0
44: */
45: @KapuaProvider
46: public class DeviceEventServiceImpl extends AbstractKapuaService implements DeviceEventService {
47:
48: private static final Logger LOG = LoggerFactory.getLogger(DeviceEventServiceImpl.class);
49:
50: private static final int MAX_RETRY = 3;
51: private static final double MAX_WAIT = 200d;
52:
53: @Inject
54: private AuthorizationService authorizationService;
55: @Inject
56: private PermissionFactory permissionFactory;
57: @Inject
58: private DeviceRegistryService deviceRegistryService;
59:
60: /**
61: * Constructor
62: */
63: public DeviceEventServiceImpl() {
64: super(DeviceEntityManagerFactory.instance());
65:
66: KapuaLocator locator = KapuaLocator.getInstance();
67:
68: authorizationService = locator.getService(AuthorizationService.class);
69: permissionFactory = locator.getFactory(PermissionFactory.class);
70: deviceRegistryService = locator.getService(DeviceRegistryService.class);
71: }
72:
73: // Operations
74:
75: @Override
76: public DeviceEvent create(DeviceEventCreator deviceEventCreator) throws KapuaException {
77: //
78: // Argument Validation
79: ArgumentValidator.notNull(deviceEventCreator, "deviceEventCreator");
80: ArgumentValidator.notNull(deviceEventCreator.getScopeId(), "deviceEventCreator.scopeId");
81: ArgumentValidator.notNull(deviceEventCreator.getDeviceId(), "deviceEventCreator.deviceId");
82: ArgumentValidator.notNull(deviceEventCreator.getReceivedOn(), "deviceEventCreator.receivedOn");
83: ArgumentValidator.notEmptyOrNull(deviceEventCreator.getResource(), "deviceEventCreator.eventType");
84:
85: //
86: // Check Access
87: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_EVENT_DOMAIN, Actions.write, deviceEventCreator.getScopeId()));
88:
89: //
90: // Check that device exists
91:• if (deviceRegistryService.find(deviceEventCreator.getScopeId(), deviceEventCreator.getDeviceId()) == null) {
92: throw new KapuaEntityNotFoundException(Device.TYPE, deviceEventCreator.getDeviceId());
93: }
94:
95: // Create the event
96: DeviceEvent deviceEvent = entityManagerSession.doTransactedAction(entityManager -> DeviceEventDAO.create(entityManager, deviceEventCreator));
97:
98: updateLastEventOnDevice(deviceEvent);
99:
100: return deviceEvent;
101: }
102:
103: @Override
104: public DeviceEvent find(KapuaId scopeId, KapuaId entityId)
105: throws KapuaException {
106: //
107: // Argument Validation
108: ArgumentValidator.notNull(scopeId, "scopeId");
109: ArgumentValidator.notNull(entityId, "entityId");
110:
111: //
112: // Check Access
113: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_EVENT_DOMAIN, Actions.read, scopeId));
114:
115: return entityManagerSession.doAction(em -> DeviceEventDAO.find(em, scopeId, entityId));
116: }
117:
118: @Override
119: public DeviceEventListResult query(KapuaQuery query)
120: throws KapuaException {
121: //
122: // Argument Validation
123: ArgumentValidator.notNull(query, "query");
124:
125: //
126: // Check Access
127: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_EVENT_DOMAIN, Actions.read, query.getScopeId()));
128:
129: return entityManagerSession.doAction(em -> DeviceEventDAO.query(em, query));
130: }
131:
132: @Override
133: public long count(KapuaQuery query)
134: throws KapuaException {
135: //
136: // Argument Validation
137: ArgumentValidator.notNull(query, "query");
138:
139: //
140: // Check Access
141: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_EVENT_DOMAIN, Actions.read, query.getScopeId()));
142:
143: return entityManagerSession.doAction(em -> DeviceEventDAO.count(em, query));
144: }
145:
146: @Override
147: public void delete(KapuaId scopeId, KapuaId deviceEventId) throws KapuaException {
148: //
149: // Argument Validation
150: ArgumentValidator.notNull(deviceEventId, "deviceEvent.id");
151: ArgumentValidator.notNull(scopeId, "deviceEvent.scopeId");
152:
153: //
154: // Check Access
155: authorizationService.checkPermission(permissionFactory.newPermission(DeviceDomains.DEVICE_EVENT_DOMAIN, Actions.delete, scopeId));
156:
157: entityManagerSession.doTransactedAction(em -> {
158:• if (DeviceEventDAO.find(em, scopeId, deviceEventId) == null) {
159: throw new KapuaEntityNotFoundException(DeviceEvent.TYPE, deviceEventId);
160: }
161:
162: return DeviceEventDAO.delete(em, scopeId, deviceEventId);
163: });
164: }
165:
166:
167: /**
168: * Updates the {@link Device#getLastEventId()} with the given {@link DeviceEvent}.
169: *
170: * @param deviceEvent The {@link DeviceEvent} that needs to be set.
171: * @throws KapuaException If {@link Device} does not exist or updating the entity causes an error that is not {@link KapuaOptimisticLockingException} which is ignored.
172: * @since 1.2.0
173: */
174: private void updateLastEventOnDevice(DeviceEvent deviceEvent) throws KapuaException {
175: int retry = 0;
176: do {
177: retry++;
178: try {
179: Device device = deviceRegistryService.find(deviceEvent.getScopeId(), deviceEvent.getDeviceId());
180:
181:• if (device == null) {
182: throw new KapuaEntityNotFoundException(Device.TYPE, deviceEvent.getDeviceId());
183: }
184:
185:• if (device.getLastEvent() == null ||
186:• device.getLastEvent().getReceivedOn().before(deviceEvent.getReceivedOn())) {
187: device.setLastEventId(deviceEvent.getId());
188: deviceRegistryService.update(device);
189: }
190: break;
191: } catch (KapuaOptimisticLockingException e) {
192:• LOG.warn("Concurrent update for device: {} - Event id: {} Attempt: {}/{}. {}", deviceEvent.getDeviceId(), deviceEvent.getId(), retry, MAX_RETRY, retry < MAX_RETRY ? "Retrying..." : "Skipping update!");
193:
194:• if (retry < MAX_RETRY) {
195: try {
196: Thread.sleep((long) (Math.random() * MAX_WAIT));
197: } catch (InterruptedException e1) {
198: LOG.warn("Error while waiting retry: {}", e.getMessage());
199: }
200: }
201: }
202: }
203:• while (retry < MAX_RETRY);
204: }
205:
206: }