Skip to content

Package: DeviceManagementBundles

DeviceManagementBundles

nameinstructionbranchcomplexitylinemethod
DeviceManagementBundles()
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
get(ScopeId, EntityId, String, SortOrder, Long)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
lambda$get$0(String, SortOrder, DeviceBundle, DeviceBundle)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
sortBundles(String, SortOrder, DeviceBundle, DeviceBundle)
M: 71 C: 0
0%
M: 12 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
start(ScopeId, EntityId, String, Long)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
stop(ScopeId, EntityId, String, Long)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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.app.api.resources.v1.resources;
14:
15: import javax.ws.rs.DefaultValue;
16: import javax.ws.rs.GET;
17: import javax.ws.rs.POST;
18: import javax.ws.rs.Path;
19: import javax.ws.rs.PathParam;
20: import javax.ws.rs.Produces;
21: import javax.ws.rs.QueryParam;
22: import javax.ws.rs.core.MediaType;
23: import javax.ws.rs.core.Response;
24:
25: import org.eclipse.kapua.KapuaException;
26: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
27: import org.eclipse.kapua.app.api.core.model.EntityId;
28: import org.eclipse.kapua.app.api.core.model.ScopeId;
29: import org.eclipse.kapua.locator.KapuaLocator;
30: import org.eclipse.kapua.model.query.SortOrder;
31: import org.eclipse.kapua.service.KapuaService;
32: import org.eclipse.kapua.service.device.management.bundle.DeviceBundle;
33: import org.eclipse.kapua.service.device.management.bundle.DeviceBundleManagementService;
34: import org.eclipse.kapua.service.device.management.bundle.DeviceBundles;
35: import org.eclipse.kapua.service.device.registry.Device;
36:
37: import com.google.common.base.Strings;
38:
39: @Path("{scopeId}/devices/{deviceId}/bundles")
40: public class DeviceManagementBundles extends AbstractKapuaResource {
41:
42: private final KapuaLocator locator = KapuaLocator.getInstance();
43: private final DeviceBundleManagementService bundleService = locator.getService(DeviceBundleManagementService.class);
44:
45: /**
46: * Returns the list of all the Bundles installed on the device.
47: *
48: * @param scopeId
49: * The {@link ScopeId} of the {@link Device}.
50: * @param deviceId
51: * The id of the device
52: * @param sortParam
53: * The name of the parameter that will be used as a sorting key
54: * @param sortDir
55: * The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
56: * @param timeout
57: * The timeout of the operation in milliseconds
58: * @return The list of Bundles
59: * @throws KapuaException
60: * Whenever something bad happens. See specific {@link KapuaService} exceptions.
61: * @since 1.0.0
62: */
63: @GET
64: @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
65: public DeviceBundles get(
66: @PathParam("scopeId") ScopeId scopeId,
67: @PathParam("deviceId") EntityId deviceId,
68: @QueryParam("sortParam") String sortParam,
69: @QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
70: @QueryParam("timeout") @DefaultValue("30000") Long timeout) throws KapuaException {
71: DeviceBundles deviceBundles = bundleService.get(scopeId, deviceId, timeout);
72:• if (!Strings.isNullOrEmpty(sortParam)) {
73: deviceBundles.getBundles().sort((b1, b2) -> sortBundles(sortParam, sortDir, b1, b2));
74: }
75: return deviceBundles;
76: }
77:
78: private int sortBundles(String sortParam, SortOrder sortDir, DeviceBundle b1, DeviceBundle b2) {
79:• switch(sortParam.toUpperCase()) {
80: default:
81: case "ID":
82:• return (sortDir == SortOrder.DESCENDING ? (int) (b2.getId() - b1.getId()) : (int) (b1.getId() - b2.getId()));
83: case "NAME":
84:• return (sortDir == SortOrder.DESCENDING ? b2.getName().compareToIgnoreCase(b1.getName()) : b1.getName().compareToIgnoreCase(b2.getName()));
85: case "STATE":
86:• return (sortDir == SortOrder.DESCENDING ? b2.getState().compareToIgnoreCase(b1.getState()) : b1.getState().compareToIgnoreCase(b2.getState()));
87: case "VERSION":
88:• return (sortDir == SortOrder.DESCENDING ? b2.getVersion().compareToIgnoreCase(b1.getVersion()) : b1.getVersion().compareToIgnoreCase(b2.getVersion()));
89: }
90: }
91:
92: /**
93: * Starts the bundle
94: *
95: * @param scopeId
96: * The {@link ScopeId} of the {@link Device}.
97: * @param deviceId
98: * The {@link Device} ID.
99: * @param bundleId
100: * the ID of the bundle to start
101: * @param timeout
102: * The timeout of the operation in milliseconds
103: * @return HTTP 200 if operation has completed successfully.
104: * @throws KapuaException
105: * Whenever something bad happens. See specific {@link KapuaService} exceptions.
106: * @since 1.0.0
107: */
108: @POST
109: @Path("{bundleId}/_start")
110: @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
111: public Response start(
112: @PathParam("scopeId") ScopeId scopeId,
113: @PathParam("deviceId") EntityId deviceId,
114: @PathParam("bundleId") String bundleId,
115: @QueryParam("timeout") @DefaultValue("30000") Long timeout) throws KapuaException {
116: bundleService.start(scopeId, deviceId, bundleId, timeout);
117:
118: return returnNoContent();
119: }
120:
121: /**
122: * Stops the bundle
123: *
124: * @param deviceId
125: * The {@link Device} ID.
126: * @param bundleId
127: * the ID of the bundle to stop
128: * @return HTTP 200 if operation has completed successfully.
129: * @throws KapuaException
130: * Whenever something bad happens. See specific {@link KapuaService} exceptions.
131: * @since 1.0.0
132: */
133: @POST
134: @Path("{bundleId}/_stop")
135: @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
136: public Response stop(
137: @PathParam("scopeId") ScopeId scopeId,
138: @PathParam("deviceId") EntityId deviceId,
139: @PathParam("bundleId") String bundleId,
140: @QueryParam("timeout") @DefaultValue("30000") Long timeout) throws KapuaException {
141: bundleService.stop(scopeId, deviceId, bundleId, timeout);
142:
143: return returnNoContent();
144: }
145: }