blob: 2bed18ba8d2fd8ba740b5b7ac1a224f4ff4e6951 [file] [log] [blame]
Brian Stankeb9170d92016-02-19 14:18:42 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stankeb9170d92016-02-19 14:18:42 -05003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.rest.resources;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.net.key.DeviceKey;
21import org.onosproject.net.key.DeviceKeyAdminService;
22import org.onosproject.net.key.DeviceKeyId;
23import org.onosproject.net.key.DeviceKeyService;
24import org.onosproject.rest.AbstractWebResource;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.Context;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import javax.ws.rs.core.UriBuilder;
37import javax.ws.rs.core.UriInfo;
38import java.io.IOException;
39import java.io.InputStream;
40
41import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkeyb784adb2018-04-02 15:33:07 -070042import static org.onlab.util.Tools.readTreeFromStream;
Brian Stankeb9170d92016-02-19 14:18:42 -050043
44/**
45 * Query and Manage Device Keys.
46 */
47@Path("keys")
48public class DeviceKeyWebResource extends AbstractWebResource {
49
50 @Context
Jian Licc730a62016-05-10 16:36:16 -070051 private UriInfo uriInfo;
Brian Stankeb9170d92016-02-19 14:18:42 -050052
53 private static final String DEVICE_KEY_NOT_FOUND = "Device key was not found";
54
55 /**
56 * Gets all device keys.
57 * Returns array of all device keys.
58 *
Jian Licc730a62016-05-10 16:36:16 -070059 * @return 200 OK with a collection of device keys
Brian Stankeb8ff6412016-02-25 14:16:19 -050060 * @onos.rsModel Devicekeys
Brian Stankeb9170d92016-02-19 14:18:42 -050061 */
62 @GET
Jian Licc730a62016-05-10 16:36:16 -070063 @Produces(MediaType.APPLICATION_JSON)
Brian Stankeb9170d92016-02-19 14:18:42 -050064 public Response getDeviceKeys() {
65 Iterable<DeviceKey> deviceKeys = get(DeviceKeyService.class).getDeviceKeys();
66 return ok(encodeArray(DeviceKey.class, "keys", deviceKeys)).build();
67 }
68
69 /**
Jian Licc730a62016-05-10 16:36:16 -070070 * Gets a single device key by device key unique identifier.
Brian Stankeb9170d92016-02-19 14:18:42 -050071 * Returns the specified device key.
72 *
Brian Stankeb8ff6412016-02-25 14:16:19 -050073 * @param id device key identifier
Jian Licc730a62016-05-10 16:36:16 -070074 * @return 200 OK with a device key, 404 not found
Brian Stankeb8ff6412016-02-25 14:16:19 -050075 * @onos.rsModel Devicekey
Brian Stankeb9170d92016-02-19 14:18:42 -050076 */
77 @GET
78 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070079 @Produces(MediaType.APPLICATION_JSON)
Brian Stankeb9170d92016-02-19 14:18:42 -050080 public Response getDeviceKey(@PathParam("id") String id) {
81 DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)),
82 DEVICE_KEY_NOT_FOUND);
83 return ok(codec(DeviceKey.class).encode(deviceKey, this)).build();
84 }
85
86 /**
Brian Stankeb8ff6412016-02-25 14:16:19 -050087 * Adds a new device key from the JSON input stream.
Brian Stankeb9170d92016-02-19 14:18:42 -050088 *
Brian Stankeb8ff6412016-02-25 14:16:19 -050089 * @param stream device key JSON stream
Brian Stankeb9170d92016-02-19 14:18:42 -050090 * @return status of the request - CREATED if the JSON is correct,
91 * BAD_REQUEST if the JSON is invalid
Brian Stankeb8ff6412016-02-25 14:16:19 -050092 * @onos.rsModel Devicekey
Brian Stankeb9170d92016-02-19 14:18:42 -050093 */
Brian Stankeb9170d92016-02-19 14:18:42 -050094 @POST
95 @Consumes(MediaType.APPLICATION_JSON)
96 @Produces(MediaType.APPLICATION_JSON)
97 public Response addDeviceKey(InputStream stream) {
98 try {
99 DeviceKeyAdminService service = get(DeviceKeyAdminService.class);
Ray Milkeyb784adb2018-04-02 15:33:07 -0700100 ObjectNode root = readTreeFromStream(mapper(), stream);
Brian Stankeb9170d92016-02-19 14:18:42 -0500101 DeviceKey deviceKey = codec(DeviceKey.class).decode(root, this);
102 service.addKey(deviceKey);
103
104 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
105 .path("keys")
106 .path(deviceKey.deviceKeyId().id());
107
108 return Response
109 .created(locationBuilder.build())
110 .build();
111
112 } catch (IOException ioe) {
113 throw new IllegalArgumentException(ioe);
114 }
115 }
116
117 /**
118 * Removes a device key by device key identifier.
119 *
Brian Stankeb8ff6412016-02-25 14:16:19 -0500120 * @param id device key identifier
Jian Licc730a62016-05-10 16:36:16 -0700121 * @return 200 OK with a removed device key, 404 not found
Brian Stankeb9170d92016-02-19 14:18:42 -0500122 */
123 @DELETE
124 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -0700125 @Produces(MediaType.APPLICATION_JSON)
Brian Stankeb9170d92016-02-19 14:18:42 -0500126 public Response removeDeviceKey(@PathParam("id") String id) {
127 DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)),
128 DEVICE_KEY_NOT_FOUND);
129 get(DeviceKeyAdminService.class).removeKey(DeviceKeyId.deviceKeyId(id));
130 return ok(codec(DeviceKey.class).encode(deviceKey, this)).build();
131 }
132}