Skip to content

Package: DeviceConnections

DeviceConnections

nameinstructionbranchcomplexitylinemethod
DeviceConnections()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
count(ScopeId, DeviceConnectionQuery)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
find(ScopeId, EntityId)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, DeviceConnectionQuery)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, String, String, String, DeviceConnectionStatus, int, int)
M: 64 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 14 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 com.google.common.base.Strings;
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
19: import org.eclipse.kapua.app.api.core.model.CountResult;
20: import org.eclipse.kapua.app.api.core.model.EntityId;
21: import org.eclipse.kapua.app.api.core.model.ScopeId;
22: import org.eclipse.kapua.locator.KapuaLocator;
23: import org.eclipse.kapua.model.query.predicate.AndPredicate;
24: import org.eclipse.kapua.service.KapuaService;
25: import org.eclipse.kapua.service.device.registry.Device;
26: import org.eclipse.kapua.service.device.registry.connection.DeviceConnection;
27: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionAttributes;
28: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionFactory;
29: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionListResult;
30: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionQuery;
31: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService;
32: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionStatus;
33:
34: import javax.ws.rs.Consumes;
35: import javax.ws.rs.DefaultValue;
36: import javax.ws.rs.GET;
37: import javax.ws.rs.POST;
38: import javax.ws.rs.Path;
39: import javax.ws.rs.PathParam;
40: import javax.ws.rs.Produces;
41: import javax.ws.rs.QueryParam;
42: import javax.ws.rs.core.MediaType;
43:
44: @Path("{scopeId}/deviceconnections")
45: public class DeviceConnections extends AbstractKapuaResource {
46:
47: private final KapuaLocator locator = KapuaLocator.getInstance();
48: private final DeviceConnectionFactory deviceConnectionFactory = locator.getFactory(DeviceConnectionFactory.class);
49: private final DeviceConnectionService deviceConnectionService = locator.getService(DeviceConnectionService.class);
50:
51: /**
52: * Gets the {@link DeviceConnection} list in the scope.
53: *
54: * @param scopeId The {@link ScopeId} in which to search results.
55: * @param clientId The id of the {@link Device} in which to search results
56: * @param status The {@link DeviceConnectionStatus} in which to search results
57: * @param offset The result set offset.
58: * @param limit The result set limit.
59: * @return The {@link DeviceConnectionListResult} of all the deviceConnections associated to the current selected scope.
60: * @throws KapuaException 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 DeviceConnectionListResult simpleQuery(
66: @PathParam("scopeId") ScopeId scopeId,
67: @QueryParam("clientId") String clientId,
68: @QueryParam("clientIp") String clientIp,
69: @QueryParam("protocol") String protocol,
70: @QueryParam("status") DeviceConnectionStatus status,
71: @QueryParam("offset") @DefaultValue("0") int offset,
72: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
73: DeviceConnectionQuery query = deviceConnectionFactory.newQuery(scopeId);
74:
75: AndPredicate andPredicate = query.andPredicate();
76:• if (!Strings.isNullOrEmpty(clientId)) {
77: andPredicate.and(query.attributePredicate(DeviceConnectionAttributes.CLIENT_ID, clientId));
78: }
79:• if (!Strings.isNullOrEmpty(clientIp)) {
80: andPredicate.and(query.attributePredicate(DeviceConnectionAttributes.CLIENT_IP, clientIp));
81: }
82:• if (!Strings.isNullOrEmpty(protocol)) {
83: andPredicate.and(query.attributePredicate(DeviceConnectionAttributes.PROTOCOL, protocol));
84: }
85:• if (status != null) {
86: andPredicate.and(query.attributePredicate(DeviceConnectionAttributes.STATUS, status));
87: }
88: query.setPredicate(andPredicate);
89:
90: query.setOffset(offset);
91: query.setLimit(limit);
92:
93: return query(scopeId, query);
94: }
95:
96: /**
97: * Queries the results with the given {@link DeviceConnectionQuery} parameter.
98: *
99: * @param scopeId The {@link ScopeId} in which to search results.
100: * @param query The {@link DeviceConnectionQuery} to use to filter results.
101: * @return The {@link DeviceConnectionListResult} of all the result matching the given {@link DeviceConnectionQuery} parameter.
102: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
103: * @since 1.0.0
104: */
105: @POST
106: @Path("_query")
107: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
108: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
109: public DeviceConnectionListResult query(
110: @PathParam("scopeId") ScopeId scopeId,
111: DeviceConnectionQuery query) throws KapuaException {
112: query.setScopeId(scopeId);
113:
114: return deviceConnectionService.query(query);
115: }
116:
117: /**
118: * Counts the results with the given {@link DeviceConnectionQuery} parameter.
119: *
120: * @param scopeId The {@link ScopeId} in which to search results.
121: * @param query The {@link DeviceConnectionQuery} to use to filter results.
122: * @return The count of all the result matching the given {@link DeviceConnectionQuery} parameter.
123: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
124: * @since 1.0.0
125: */
126: @POST
127: @Path("_count")
128: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
129: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
130: public CountResult count(
131: @PathParam("scopeId") ScopeId scopeId,
132: DeviceConnectionQuery query) throws KapuaException {
133: query.setScopeId(scopeId);
134:
135: return new CountResult(deviceConnectionService.count(query));
136: }
137:
138: /**
139: * Returns the DeviceConnection specified by the "deviceConnectionId" path parameter.
140: *
141: * @param scopeId The {@link ScopeId} of the requested {@link DeviceConnection}.
142: * @param deviceConnectionId The id of the requested DeviceConnection.
143: * @return The requested DeviceConnection object.
144: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
145: * @since 1.0.0
146: */
147: @GET
148: @Path("{deviceConnectionId}")
149: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
150: public DeviceConnection find(
151: @PathParam("scopeId") ScopeId scopeId,
152: @PathParam("deviceConnectionId") EntityId deviceConnectionId) throws KapuaException {
153: DeviceConnection deviceConnection = deviceConnectionService.find(scopeId, deviceConnectionId);
154:
155:• if (deviceConnection != null) {
156: return deviceConnection;
157: } else {
158: throw new KapuaEntityNotFoundException(DeviceConnection.TYPE, deviceConnectionId);
159: }
160: }
161:
162: }