Package: DeviceSnapshotManagementServiceImpl
DeviceSnapshotManagementServiceImpl
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DeviceSnapshotManagementServiceImpl() |
|
|
|
|
|
||||||||||||||||||||
get(KapuaId, KapuaId, Long) |
|
|
|
|
|
||||||||||||||||||||
lambda$get$0(SnapshotResponseMessage) |
|
|
|
|
|
||||||||||||||||||||
rollback(KapuaId, KapuaId, String, Long) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
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.snapshot.internal;
14:
15: import org.eclipse.kapua.KapuaException;
16: import org.eclipse.kapua.commons.util.ArgumentValidator;
17: import org.eclipse.kapua.locator.KapuaProvider;
18: import org.eclipse.kapua.model.domain.Actions;
19: import org.eclipse.kapua.model.id.KapuaId;
20: import org.eclipse.kapua.service.device.management.DeviceManagementDomains;
21: import org.eclipse.kapua.service.device.management.commons.AbstractDeviceManagementServiceImpl;
22: import org.eclipse.kapua.service.device.management.commons.call.DeviceCallBuilder;
23: import org.eclipse.kapua.service.device.management.configuration.internal.DeviceConfigurationAppProperties;
24: import org.eclipse.kapua.service.device.management.configuration.internal.DeviceConfigurationManagementServiceImpl;
25: import org.eclipse.kapua.service.device.management.message.KapuaMethod;
26: import org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshotManagementService;
27: import org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshots;
28: import org.eclipse.kapua.service.device.management.snapshot.message.internal.SnapshotRequestChannel;
29: import org.eclipse.kapua.service.device.management.snapshot.message.internal.SnapshotRequestMessage;
30: import org.eclipse.kapua.service.device.management.snapshot.message.internal.SnapshotRequestPayload;
31: import org.eclipse.kapua.service.device.management.snapshot.message.internal.SnapshotResponseMessage;
32: import org.slf4j.Logger;
33: import org.slf4j.LoggerFactory;
34:
35: import java.util.Date;
36:
37: /**
38: * {@link DeviceSnapshotManagementService} implementation.
39: *
40: * @since 1.0.0
41: */
42: @KapuaProvider
43: public class DeviceSnapshotManagementServiceImpl extends AbstractDeviceManagementServiceImpl implements DeviceSnapshotManagementService {
44:
45: private static final Logger LOG = LoggerFactory.getLogger(DeviceConfigurationManagementServiceImpl.class);
46:
47: @Override
48: public DeviceSnapshots get(KapuaId scopeId, KapuaId deviceId, Long timeout)
49: throws KapuaException {
50: //
51: // Argument Validation
52: ArgumentValidator.notNull(scopeId, "scopeId");
53: ArgumentValidator.notNull(deviceId, "deviceId");
54:
55: //
56: // Check Access
57: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.read, scopeId));
58:
59: //
60: // Prepare the request
61: SnapshotRequestChannel snapshotRequestChannel = new SnapshotRequestChannel();
62: snapshotRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
63: snapshotRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
64: snapshotRequestChannel.setMethod(KapuaMethod.READ);
65:
66: SnapshotRequestPayload snapshotRequestPayload = new SnapshotRequestPayload();
67:
68: SnapshotRequestMessage snapshotRequestMessage = new SnapshotRequestMessage();
69: snapshotRequestMessage.setScopeId(scopeId);
70: snapshotRequestMessage.setDeviceId(deviceId);
71: snapshotRequestMessage.setCapturedOn(new Date());
72: snapshotRequestMessage.setPayload(snapshotRequestPayload);
73: snapshotRequestMessage.setChannel(snapshotRequestChannel);
74:
75: //
76: // Build request
77: DeviceCallBuilder<SnapshotRequestChannel, SnapshotRequestPayload, SnapshotRequestMessage, SnapshotResponseMessage> snapshotDeviceCallBuilder =
78: DeviceCallBuilder
79: .newBuilder()
80: .withRequestMessage(snapshotRequestMessage)
81: .withTimeoutOrDefault(timeout);
82:
83: //
84: // Do get
85: SnapshotResponseMessage responseMessage;
86: try {
87: responseMessage = snapshotDeviceCallBuilder.send();
88: } catch (Exception e) {
89: LOG.error("Error while getting DeviceSnapshots for Device {}. Error: {}", deviceId, e.getMessage(), e);
90: throw e;
91: }
92:
93: //
94: // Create event
95: createDeviceEvent(scopeId, deviceId, snapshotRequestMessage, responseMessage);
96:
97: //
98: // Check response
99: return checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getDeviceSnapshots());
100: }
101:
102: @Override
103: public void rollback(KapuaId scopeId, KapuaId deviceId, String snapshotId, Long timeout)
104: throws KapuaException {
105: //
106: // Argument Validation
107: ArgumentValidator.notNull(scopeId, "scopeId");
108: ArgumentValidator.notNull(deviceId, "deviceId");
109: ArgumentValidator.notEmptyOrNull(snapshotId, "snapshotId");
110:
111: //
112: // Check Access
113: AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.execute, scopeId));
114:
115: //
116: // Prepare the request
117: SnapshotRequestChannel snapshotRequestChannel = new SnapshotRequestChannel();
118: snapshotRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
119: snapshotRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
120: snapshotRequestChannel.setMethod(KapuaMethod.EXECUTE);
121: snapshotRequestChannel.setSnapshotId(snapshotId);
122:
123: SnapshotRequestPayload snapshotRequestPayload = new SnapshotRequestPayload();
124:
125: SnapshotRequestMessage snapshotRequestMessage = new SnapshotRequestMessage();
126: snapshotRequestMessage.setScopeId(scopeId);
127: snapshotRequestMessage.setDeviceId(deviceId);
128: snapshotRequestMessage.setCapturedOn(new Date());
129: snapshotRequestMessage.setPayload(snapshotRequestPayload);
130: snapshotRequestMessage.setChannel(snapshotRequestChannel);
131:
132: //
133: // Build request
134: DeviceCallBuilder<SnapshotRequestChannel, SnapshotRequestPayload, SnapshotRequestMessage, SnapshotResponseMessage> snapshotDeviceCallBuilder =
135: DeviceCallBuilder
136: .newBuilder()
137: .withRequestMessage(snapshotRequestMessage)
138: .withTimeoutOrDefault(timeout);
139:
140: //
141: // Do exec
142: SnapshotResponseMessage responseMessage;
143: try {
144: responseMessage = snapshotDeviceCallBuilder.send();
145: } catch (Exception e) {
146: LOG.error("Error while rolling back to DeviceSnapshot {} for Device {}. Error: {}", snapshotId, deviceId, e.getMessage(), e);
147: throw e;
148: }
149:
150: //
151: // Create event
152: createDeviceEvent(scopeId, deviceId, snapshotRequestMessage, responseMessage);
153:
154: //
155: // Check response
156: checkResponseAcceptedOrThrowError(responseMessage);
157: }
158: }