-
Notifications
You must be signed in to change notification settings - Fork 160
/
GwtDeviceInventoryManagementServiceImpl.java
201 lines (163 loc) · 10.6 KB
/
GwtDeviceInventoryManagementServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.device.server;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import org.eclipse.kapua.app.console.module.api.client.GwtKapuaException;
import org.eclipse.kapua.app.console.module.api.server.KapuaRemoteServiceServlet;
import org.eclipse.kapua.app.console.module.api.server.util.KapuaExceptionHandler;
import org.eclipse.kapua.app.console.module.api.setting.ConsoleSetting;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtXSRFToken;
import org.eclipse.kapua.app.console.module.device.shared.model.management.inventory.GwtInventoryBundle;
import org.eclipse.kapua.app.console.module.device.shared.model.management.inventory.GwtInventoryDeploymentPackage;
import org.eclipse.kapua.app.console.module.device.shared.model.management.inventory.GwtInventoryItem;
import org.eclipse.kapua.app.console.module.device.shared.model.management.inventory.GwtInventorySystemPackage;
import org.eclipse.kapua.app.console.module.device.shared.service.GwtDeviceInventoryManagementService;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.device.management.inventory.DeviceInventoryManagementFactory;
import org.eclipse.kapua.service.device.management.inventory.DeviceInventoryManagementService;
import org.eclipse.kapua.service.device.management.inventory.model.bundle.DeviceInventoryBundle;
import org.eclipse.kapua.service.device.management.inventory.model.bundle.DeviceInventoryBundleAction;
import org.eclipse.kapua.service.device.management.inventory.model.bundle.DeviceInventoryBundles;
import org.eclipse.kapua.service.device.management.inventory.model.inventory.DeviceInventory;
import org.eclipse.kapua.service.device.management.inventory.model.inventory.DeviceInventoryItem;
import org.eclipse.kapua.service.device.management.inventory.model.packages.DeviceInventoryPackage;
import org.eclipse.kapua.service.device.management.inventory.model.packages.DeviceInventoryPackages;
import org.eclipse.kapua.service.device.management.inventory.model.system.DeviceInventorySystemPackage;
import org.eclipse.kapua.service.device.management.inventory.model.system.DeviceInventorySystemPackages;
import java.util.ArrayList;
import java.util.List;
/**
* The server side implementation of the Device RPC service.
*/
public class GwtDeviceInventoryManagementServiceImpl extends KapuaRemoteServiceServlet implements GwtDeviceInventoryManagementService {
private static final long serialVersionUID = -1391026997499175151L;
private static final ConsoleSetting CONSOLE_SETTING = ConsoleSetting.getInstance();
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final DeviceInventoryManagementService DEVICE_INVENTORY_MANAGEMENT_SERVICE = LOCATOR.getService(DeviceInventoryManagementService.class);
private static final DeviceInventoryManagementFactory DEVICE_INVENTORY_MANAGEMENT_FACTORY = LOCATOR.getFactory(DeviceInventoryManagementFactory.class);
@Override
public ListLoadResult<GwtInventoryItem> findDeviceInventory(String scopeIdString, String deviceIdString)
throws GwtKapuaException {
try {
KapuaId scopeId = KapuaEid.parseCompactId(scopeIdString);
KapuaId deviceId = KapuaEid.parseCompactId(deviceIdString);
DeviceInventory deviceInventory = DEVICE_INVENTORY_MANAGEMENT_SERVICE.getInventory(scopeId, deviceId, null);
List<GwtInventoryItem> gwtInventoryItems = new ArrayList<GwtInventoryItem>();
for (DeviceInventoryItem inventoryItem : deviceInventory.getInventoryItems()) {
GwtInventoryItem gwtInventoryItem = new GwtInventoryItem();
gwtInventoryItem.setName(inventoryItem.getName());
gwtInventoryItem.setVersion(inventoryItem.getVersion());
gwtInventoryItem.setType(inventoryItem.getItemType());
gwtInventoryItems.add(gwtInventoryItem);
}
return new BaseListLoadResult<GwtInventoryItem>(gwtInventoryItems);
} catch (Exception exception) {
throw KapuaExceptionHandler.buildExceptionFromError(exception);
}
}
@Override
public ListLoadResult<GwtInventoryBundle> findDeviceBundles(String scopeIdString, String deviceIdString)
throws GwtKapuaException {
try {
KapuaId scopeId = KapuaEid.parseCompactId(scopeIdString);
KapuaId deviceId = KapuaEid.parseCompactId(deviceIdString);
DeviceInventoryBundles inventoryBundles = DEVICE_INVENTORY_MANAGEMENT_SERVICE.getBundles(scopeId, deviceId, null);
List<GwtInventoryBundle> gwtInventoryBundles = new ArrayList<GwtInventoryBundle>();
for (DeviceInventoryBundle inventoryBundle : inventoryBundles.getInventoryBundles()) {
GwtInventoryBundle gwtInventoryBundle = new GwtInventoryBundle();
gwtInventoryBundle.setId(inventoryBundle.getId());
gwtInventoryBundle.setName(inventoryBundle.getName());
gwtInventoryBundle.setVersion(inventoryBundle.getVersion());
gwtInventoryBundle.setStatus(inventoryBundle.getStatus());
gwtInventoryBundle.setSigned(inventoryBundle.getSigned());
gwtInventoryBundles.add(gwtInventoryBundle);
}
return new BaseListLoadResult<GwtInventoryBundle>(gwtInventoryBundles);
} catch (Exception exception) {
throw KapuaExceptionHandler.buildExceptionFromError(exception);
}
}
@Override
public void execDeviceBundle(GwtXSRFToken xsrfToken, String scopeIdString, String deviceIdString, GwtInventoryBundle gwtInventoryBundle, boolean startOrStop) throws GwtKapuaException {
checkXSRFToken(xsrfToken);
try {
KapuaId scopeId = KapuaEid.parseCompactId(scopeIdString);
KapuaId deviceId = KapuaEid.parseCompactId(deviceIdString);
DeviceInventoryBundle deviceInventoryBundle = DEVICE_INVENTORY_MANAGEMENT_FACTORY.newDeviceInventoryBundle();
deviceInventoryBundle.setName(gwtInventoryBundle.getName());
deviceInventoryBundle.setVersion(gwtInventoryBundle.getVersion());
DEVICE_INVENTORY_MANAGEMENT_SERVICE.execBundle(
scopeId,
deviceId,
deviceInventoryBundle,
startOrStop ? DeviceInventoryBundleAction.START : DeviceInventoryBundleAction.STOP,
null);
} catch (Exception exception) {
throw KapuaExceptionHandler.buildExceptionFromError(exception);
}
}
@Override
public ListLoadResult<GwtInventorySystemPackage> findDeviceSystemPackages(String scopeIdString, String deviceIdString)
throws GwtKapuaException {
try {
KapuaId scopeId = KapuaEid.parseCompactId(scopeIdString);
KapuaId deviceId = KapuaEid.parseCompactId(deviceIdString);
DeviceInventorySystemPackages systemPackages = DEVICE_INVENTORY_MANAGEMENT_SERVICE.getSystemPackages(scopeId, deviceId, null);
List<GwtInventorySystemPackage> gwtInventorySystemPackages = new ArrayList<GwtInventorySystemPackage>();
for (DeviceInventorySystemPackage inventorySystemPackage : systemPackages.getSystemPackages()) {
GwtInventorySystemPackage gwtInventorySystemPackage = new GwtInventorySystemPackage();
gwtInventorySystemPackage.setName(inventorySystemPackage.getName());
gwtInventorySystemPackage.setVersion(inventorySystemPackage.getVersion());
gwtInventorySystemPackage.setType(inventorySystemPackage.getPackageType());
gwtInventorySystemPackages.add(gwtInventorySystemPackage);
}
return new BaseListLoadResult<GwtInventorySystemPackage>(gwtInventorySystemPackages);
} catch (Exception exception) {
throw KapuaExceptionHandler.buildExceptionFromError(exception);
}
}
@Override
public List<GwtInventoryDeploymentPackage> findDeviceDeploymentPackages(String scopeShortId, String deviceShortId)
throws GwtKapuaException {
try {
KapuaId scopeId = KapuaEid.parseCompactId(scopeShortId);
KapuaId deviceId = KapuaEid.parseCompactId(deviceShortId);
DeviceInventoryPackages deploymentPackages = DEVICE_INVENTORY_MANAGEMENT_SERVICE.getDeploymentPackages(scopeId, deviceId, null);
List<GwtInventoryDeploymentPackage> gwtInventoryDeploymentPackages = new ArrayList<GwtInventoryDeploymentPackage>();
for (DeviceInventoryPackage deploymentPackage : deploymentPackages.getPackages()) {
GwtInventoryDeploymentPackage gwtInventoryDeploymentPackage = new GwtInventoryDeploymentPackage();
gwtInventoryDeploymentPackage.setName(deploymentPackage.getName());
gwtInventoryDeploymentPackage.setVersion(deploymentPackage.getVersion());
List<DeviceInventoryBundle> packageBundles = deploymentPackage.getPackageBundles();
List<GwtInventoryBundle> gwtPackageBundles = new ArrayList<GwtInventoryBundle>();
for (DeviceInventoryBundle bundleInfo : packageBundles) {
GwtInventoryBundle inventoryBundle = new GwtInventoryBundle();
inventoryBundle.setId(bundleInfo.getId());
inventoryBundle.setName(bundleInfo.getName());
inventoryBundle.setVersion(bundleInfo.getVersion());
inventoryBundle.setStatus(bundleInfo.getStatus());
inventoryBundle.setSigned(bundleInfo.getSigned());
gwtPackageBundles.add(inventoryBundle);
}
gwtInventoryDeploymentPackage.setBundles(gwtPackageBundles);
gwtInventoryDeploymentPackages.add(gwtInventoryDeploymentPackage);
}
return gwtInventoryDeploymentPackages;
} catch (Exception exception) {
throw KapuaExceptionHandler.buildExceptionFromError(exception);
}
}
}