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