/
DeviceManagementBundles.java
145 lines (134 loc) · 5.95 KB
/
DeviceManagementBundles.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
/*******************************************************************************
* Copyright (c) 2016, 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.api.resources.v1.resources;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
import org.eclipse.kapua.app.api.core.model.EntityId;
import org.eclipse.kapua.app.api.core.model.ScopeId;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.query.SortOrder;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.device.management.bundle.DeviceBundle;
import org.eclipse.kapua.service.device.management.bundle.DeviceBundleManagementService;
import org.eclipse.kapua.service.device.management.bundle.DeviceBundles;
import org.eclipse.kapua.service.device.registry.Device;
import com.google.common.base.Strings;
@Path("{scopeId}/devices/{deviceId}/bundles")
public class DeviceManagementBundles extends AbstractKapuaResource {
private final KapuaLocator locator = KapuaLocator.getInstance();
private final DeviceBundleManagementService bundleService = locator.getService(DeviceBundleManagementService.class);
/**
* Returns the list of all the Bundles installed on the device.
*
* @param scopeId
* The {@link ScopeId} of the {@link Device}.
* @param deviceId
* The id of the device
* @param sortParam
* The name of the parameter that will be used as a sorting key
* @param sortDir
* The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
* @param timeout
* The timeout of the operation in milliseconds
* @return The list of Bundles
* @throws KapuaException
* Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DeviceBundles get(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("deviceId") EntityId deviceId,
@QueryParam("sortParam") String sortParam,
@QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
@QueryParam("timeout") Long timeout) throws KapuaException {
DeviceBundles deviceBundles = bundleService.get(scopeId, deviceId, timeout);
if (!Strings.isNullOrEmpty(sortParam)) {
deviceBundles.getBundles().sort((b1, b2) -> sortBundles(sortParam, sortDir, b1, b2));
}
return deviceBundles;
}
private int sortBundles(String sortParam, SortOrder sortDir, DeviceBundle b1, DeviceBundle b2) {
switch(sortParam.toUpperCase()) {
default:
case "ID":
return (sortDir == SortOrder.DESCENDING ? (int) (b2.getId() - b1.getId()) : (int) (b1.getId() - b2.getId()));
case "NAME":
return (sortDir == SortOrder.DESCENDING ? b2.getName().compareToIgnoreCase(b1.getName()) : b1.getName().compareToIgnoreCase(b2.getName()));
case "STATE":
return (sortDir == SortOrder.DESCENDING ? b2.getState().compareToIgnoreCase(b1.getState()) : b1.getState().compareToIgnoreCase(b2.getState()));
case "VERSION":
return (sortDir == SortOrder.DESCENDING ? b2.getVersion().compareToIgnoreCase(b1.getVersion()) : b1.getVersion().compareToIgnoreCase(b2.getVersion()));
}
}
/**
* Starts the bundle
*
* @param scopeId
* The {@link ScopeId} of the {@link Device}.
* @param deviceId
* The {@link Device} ID.
* @param bundleId
* the ID of the bundle to start
* @param timeout
* The timeout of the operation in milliseconds
* @return HTTP 200 if operation has completed successfully.
* @throws KapuaException
* Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@POST
@Path("{bundleId}/_start")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response start(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("deviceId") EntityId deviceId,
@PathParam("bundleId") String bundleId,
@QueryParam("timeout") Long timeout) throws KapuaException {
bundleService.start(scopeId, deviceId, bundleId, timeout);
return returnNoContent();
}
/**
* Stops the bundle
*
* @param deviceId
* The {@link Device} ID.
* @param bundleId
* the ID of the bundle to stop
* @return HTTP 200 if operation has completed successfully.
* @throws KapuaException
* Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@POST
@Path("{bundleId}/_stop")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response stop(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("deviceId") EntityId deviceId,
@PathParam("bundleId") String bundleId,
@QueryParam("timeout") Long timeout) throws KapuaException {
bundleService.stop(scopeId, deviceId, bundleId, timeout);
return returnNoContent();
}
}