/
DeviceManagementPackages.java
118 lines (108 loc) · 5.45 KB
/
DeviceManagementPackages.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
/*******************************************************************************
* 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 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.service.KapuaService;
import org.eclipse.kapua.service.device.management.packages.DevicePackageFactory;
import org.eclipse.kapua.service.device.management.packages.DevicePackageManagementService;
import org.eclipse.kapua.service.device.management.packages.model.DevicePackages;
import org.eclipse.kapua.service.device.management.packages.model.download.DevicePackageDownloadOptions;
import org.eclipse.kapua.service.device.management.packages.model.download.DevicePackageDownloadRequest;
import org.eclipse.kapua.service.device.management.packages.model.uninstall.DevicePackageUninstallOptions;
import org.eclipse.kapua.service.device.management.packages.model.uninstall.DevicePackageUninstallRequest;
import org.eclipse.kapua.service.device.registry.Device;
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;
@Path("{scopeId}/devices/{deviceId}/packages")
public class DeviceManagementPackages extends AbstractKapuaResource {
private final KapuaLocator locator = KapuaLocator.getInstance();
private final DevicePackageManagementService packageService = locator.getService(DevicePackageManagementService.class);
private final DevicePackageFactory packageFactory = locator.getFactory(DevicePackageFactory.class);
/**
* Returns the list of all the packages installed on the device.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param deviceId The id of the device
* @param timeout The timeout of the operation
* @return The list of packages installed.
* @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
* @since 1.0.0
*/
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public DevicePackages get(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("deviceId") EntityId deviceId,
@QueryParam("timeout") Long timeout) throws KapuaException {
return packageService.getInstalled(scopeId, deviceId, timeout);
}
/**
* Download and optionally installs a package into the device.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param deviceId The {@link Device} ID.
* @param timeout The timeout of the operation
* @param packageDownloadRequest Mandatory object with all the informations needed to download and install a package
* @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("_download")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response download(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("deviceId") EntityId deviceId,
@QueryParam("timeout") Long timeout,
DevicePackageDownloadRequest packageDownloadRequest)
throws KapuaException {
DevicePackageDownloadOptions options = packageFactory.newPackageDownloadOptions();
options.setTimeout(timeout);
packageService.downloadExec(scopeId, deviceId, packageDownloadRequest, options);
return returnNoContent();
}
/**
* Uninstalls a package into the device.
*
* @param scopeId The {@link ScopeId} in which to search results.
* @param deviceId The {@link Device} ID.
* @param timeout The timeout of the operation
* @param packageUninstallRequest Mandatory object with all the informations needed to uninstall a package
* @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("_uninstall")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response uninstall(
@PathParam("scopeId") ScopeId scopeId,
@PathParam("deviceId") EntityId deviceId,
@QueryParam("timeout") Long timeout,
DevicePackageUninstallRequest packageUninstallRequest) throws KapuaException {
DevicePackageUninstallOptions options = packageFactory.newPackageUninstallOptions();
options.setTimeout(timeout);
packageService.uninstallExec(scopeId, deviceId, packageUninstallRequest, options);
return returnNoContent();
}
}