Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.rest.resources; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.net.key.DeviceKey; |
| 21 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 22 | import org.onosproject.net.key.DeviceKeyId; |
| 23 | import org.onosproject.net.key.DeviceKeyService; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.Context; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import javax.ws.rs.core.UriBuilder; |
| 37 | import javax.ws.rs.core.UriInfo; |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | |
| 41 | import static org.onlab.util.Tools.nullIsNotFound; |
| 42 | |
| 43 | /** |
| 44 | * Query and Manage Device Keys. |
| 45 | */ |
| 46 | @Path("keys") |
| 47 | public class DeviceKeyWebResource extends AbstractWebResource { |
| 48 | |
| 49 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 50 | private UriInfo uriInfo; |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 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 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 58 | * @return 200 OK with a collection of device keys |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 59 | * @onos.rsModel Devicekeys |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 60 | */ |
| 61 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 62 | @Produces(MediaType.APPLICATION_JSON) |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 63 | public Response getDeviceKeys() { |
| 64 | Iterable<DeviceKey> deviceKeys = get(DeviceKeyService.class).getDeviceKeys(); |
| 65 | return ok(encodeArray(DeviceKey.class, "keys", deviceKeys)).build(); |
| 66 | } |
| 67 | |
| 68 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 69 | * Gets a single device key by device key unique identifier. |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 70 | * Returns the specified device key. |
| 71 | * |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 72 | * @param id device key identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 73 | * @return 200 OK with a device key, 404 not found |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 74 | * @onos.rsModel Devicekey |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 75 | */ |
| 76 | @GET |
| 77 | @Path("{id}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 78 | @Produces(MediaType.APPLICATION_JSON) |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 79 | public Response getDeviceKey(@PathParam("id") String id) { |
| 80 | DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)), |
| 81 | DEVICE_KEY_NOT_FOUND); |
| 82 | return ok(codec(DeviceKey.class).encode(deviceKey, this)).build(); |
| 83 | } |
| 84 | |
| 85 | /** |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 86 | * Adds a new device key from the JSON input stream. |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 87 | * |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 88 | * @param stream device key JSON stream |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 89 | * @return status of the request - CREATED if the JSON is correct, |
| 90 | * BAD_REQUEST if the JSON is invalid |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 91 | * @onos.rsModel Devicekey |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 92 | */ |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 93 | @POST |
| 94 | @Consumes(MediaType.APPLICATION_JSON) |
| 95 | @Produces(MediaType.APPLICATION_JSON) |
| 96 | public Response addDeviceKey(InputStream stream) { |
| 97 | try { |
| 98 | DeviceKeyAdminService service = get(DeviceKeyAdminService.class); |
| 99 | ObjectNode root = (ObjectNode) mapper().readTree(stream); |
| 100 | DeviceKey deviceKey = codec(DeviceKey.class).decode(root, this); |
| 101 | service.addKey(deviceKey); |
| 102 | |
| 103 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 104 | .path("keys") |
| 105 | .path(deviceKey.deviceKeyId().id()); |
| 106 | |
| 107 | return Response |
| 108 | .created(locationBuilder.build()) |
| 109 | .build(); |
| 110 | |
| 111 | } catch (IOException ioe) { |
| 112 | throw new IllegalArgumentException(ioe); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Removes a device key by device key identifier. |
| 118 | * |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 119 | * @param id device key identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 120 | * @return 200 OK with a removed device key, 404 not found |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 121 | */ |
| 122 | @DELETE |
| 123 | @Path("{id}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 124 | @Produces(MediaType.APPLICATION_JSON) |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 125 | public Response removeDeviceKey(@PathParam("id") String id) { |
| 126 | DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)), |
| 127 | DEVICE_KEY_NOT_FOUND); |
| 128 | get(DeviceKeyAdminService.class).removeKey(DeviceKeyId.deviceKeyId(id)); |
| 129 | return ok(codec(DeviceKey.class).encode(deviceKey, this)).build(); |
| 130 | } |
| 131 | } |