blob: c4020e1f797bb12d1f384ea4d3cfe46c6bf411c3 [file] [log] [blame]
Brian Stankeb9170d92016-02-19 14:18:42 -05001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
42
43/**
44 * Query and Manage Device Keys.
45 */
46@Path("keys")
47public class DeviceKeyWebResource extends AbstractWebResource {
48
49 @Context
50 UriInfo uriInfo;
51
52 private static final String DEVICE_KEY_NOT_FOUND = "Device key was not found";
53
54 /**
55 * Gets all device keys.
56 * Returns array of all device keys.
57 *
58 * @return 200 OK
Brian Stankeb8ff6412016-02-25 14:16:19 -050059 * @onos.rsModel Devicekeys
Brian Stankeb9170d92016-02-19 14:18:42 -050060 */
61 @GET
62 public Response getDeviceKeys() {
63 Iterable<DeviceKey> deviceKeys = get(DeviceKeyService.class).getDeviceKeys();
64 return ok(encodeArray(DeviceKey.class, "keys", deviceKeys)).build();
65 }
66
67 /**
68 * Get a single device key by device key unique identifier.
69 * Returns the specified device key.
70 *
Brian Stankeb8ff6412016-02-25 14:16:19 -050071 * @param id device key identifier
72 * @return 200 OK, 404 not found
73 * @onos.rsModel Devicekey
Brian Stankeb9170d92016-02-19 14:18:42 -050074 */
75 @GET
76 @Path("{id}")
77 public Response getDeviceKey(@PathParam("id") String id) {
78 DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)),
79 DEVICE_KEY_NOT_FOUND);
80 return ok(codec(DeviceKey.class).encode(deviceKey, this)).build();
81 }
82
83 /**
Brian Stankeb8ff6412016-02-25 14:16:19 -050084 * Adds a new device key from the JSON input stream.
Brian Stankeb9170d92016-02-19 14:18:42 -050085 *
Brian Stankeb8ff6412016-02-25 14:16:19 -050086 * @param stream device key JSON stream
Brian Stankeb9170d92016-02-19 14:18:42 -050087 * @return status of the request - CREATED if the JSON is correct,
88 * BAD_REQUEST if the JSON is invalid
Brian Stankeb8ff6412016-02-25 14:16:19 -050089 * @onos.rsModel Devicekey
Brian Stankeb9170d92016-02-19 14:18:42 -050090 */
Brian Stankeb9170d92016-02-19 14:18:42 -050091 @POST
92 @Consumes(MediaType.APPLICATION_JSON)
93 @Produces(MediaType.APPLICATION_JSON)
94 public Response addDeviceKey(InputStream stream) {
95 try {
96 DeviceKeyAdminService service = get(DeviceKeyAdminService.class);
97 ObjectNode root = (ObjectNode) mapper().readTree(stream);
98 DeviceKey deviceKey = codec(DeviceKey.class).decode(root, this);
99 service.addKey(deviceKey);
100
101 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
102 .path("keys")
103 .path(deviceKey.deviceKeyId().id());
104
105 return Response
106 .created(locationBuilder.build())
107 .build();
108
109 } catch (IOException ioe) {
110 throw new IllegalArgumentException(ioe);
111 }
112 }
113
114 /**
115 * Removes a device key by device key identifier.
116 *
Brian Stankeb8ff6412016-02-25 14:16:19 -0500117 * @param id device key identifier
118 * @return 200 OK, 404 not found
Brian Stankeb9170d92016-02-19 14:18:42 -0500119 */
120 @DELETE
121 @Path("{id}")
122 public Response removeDeviceKey(@PathParam("id") String id) {
123 DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)),
124 DEVICE_KEY_NOT_FOUND);
125 get(DeviceKeyAdminService.class).removeKey(DeviceKeyId.deviceKeyId(id));
126 return ok(codec(DeviceKey.class).encode(deviceKey, this)).build();
127 }
128}