Committed by
Gerrit Code Review
ONOS-3658 - Adding REST APIs for query and management of device keys.
Change-Id: I3c70660a15d9085409e43d2d14120d29fd5020be
Showing
6 changed files
with
226 additions
and
1 deletions
| ... | @@ -53,6 +53,7 @@ import org.onosproject.net.intent.Constraint; | ... | @@ -53,6 +53,7 @@ import org.onosproject.net.intent.Constraint; |
| 53 | import org.onosproject.net.intent.HostToHostIntent; | 53 | import org.onosproject.net.intent.HostToHostIntent; |
| 54 | import org.onosproject.net.intent.Intent; | 54 | import org.onosproject.net.intent.Intent; |
| 55 | import org.onosproject.net.intent.PointToPointIntent; | 55 | import org.onosproject.net.intent.PointToPointIntent; |
| 56 | +import org.onosproject.net.key.DeviceKey; | ||
| 56 | import org.onosproject.net.mcast.McastRoute; | 57 | import org.onosproject.net.mcast.McastRoute; |
| 57 | import org.onosproject.net.meter.Band; | 58 | import org.onosproject.net.meter.Band; |
| 58 | import org.onosproject.net.meter.Meter; | 59 | import org.onosproject.net.meter.Meter; |
| ... | @@ -119,6 +120,7 @@ public class CodecManager implements CodecService { | ... | @@ -119,6 +120,7 @@ public class CodecManager implements CodecService { |
| 119 | registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec()); | 120 | registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec()); |
| 120 | registerCodec(NextObjective.class, new NextObjectiveCodec()); | 121 | registerCodec(NextObjective.class, new NextObjectiveCodec()); |
| 121 | registerCodec(McastRoute.class, new McastRouteCodec()); | 122 | registerCodec(McastRoute.class, new McastRouteCodec()); |
| 123 | + registerCodec(DeviceKey.class, new DeviceKeyCodec()); | ||
| 122 | log.info("Started"); | 124 | log.info("Started"); |
| 123 | } | 125 | } |
| 124 | 126 | ... | ... |
| 1 | +/* | ||
| 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 | + | ||
| 17 | +package org.onosproject.codec.impl; | ||
| 18 | + | ||
| 19 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 20 | +import org.onosproject.codec.CodecContext; | ||
| 21 | +import org.onosproject.net.key.DeviceKey; | ||
| 22 | +import org.onosproject.net.key.DeviceKeyId; | ||
| 23 | +import org.onosproject.net.key.DeviceKeyService; | ||
| 24 | +import org.slf4j.Logger; | ||
| 25 | + | ||
| 26 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 27 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * Device key JSON codec. | ||
| 31 | + */ | ||
| 32 | +public class DeviceKeyCodec extends AnnotatedCodec<DeviceKey> { | ||
| 33 | + | ||
| 34 | + private final Logger log = getLogger(getClass()); | ||
| 35 | + | ||
| 36 | + // JSON fieldNames | ||
| 37 | + private static final String ID = "id"; | ||
| 38 | + private static final String TYPE = "type"; | ||
| 39 | + private static final String LABEL = "label"; | ||
| 40 | + private static final String COMMUNITY_NAME = "community_name"; | ||
| 41 | + private static final String USERNAME = "username"; | ||
| 42 | + private static final String PASSWORD = "password"; | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + @Override | ||
| 46 | + public ObjectNode encode(DeviceKey deviceKey, CodecContext context) { | ||
| 47 | + checkNotNull(deviceKey, "Device key cannot be null"); | ||
| 48 | + DeviceKeyService service = context.getService(DeviceKeyService.class); | ||
| 49 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 50 | + .put(ID, deviceKey.deviceKeyId().id().toString()) | ||
| 51 | + .put(TYPE, deviceKey.type().toString()) | ||
| 52 | + .put(LABEL, deviceKey.label().toString()); | ||
| 53 | + | ||
| 54 | + if (deviceKey.type().equals(DeviceKey.Type.COMMUNITY_NAME)) { | ||
| 55 | + result.put(COMMUNITY_NAME, deviceKey.asCommunityName().name()); | ||
| 56 | + } else if (deviceKey.type().equals(DeviceKey.Type.USERNAME_PASSWORD)) { | ||
| 57 | + result.put(USERNAME, deviceKey.asUsernamePassword().username().toString()); | ||
| 58 | + result.put(PASSWORD, deviceKey.asUsernamePassword().password().toString()); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + return annotate(result, deviceKey, context); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public DeviceKey decode(ObjectNode json, CodecContext context) { | ||
| 66 | + if (json == null || !json.isObject()) { | ||
| 67 | + return null; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + DeviceKeyId id = DeviceKeyId.deviceKeyId(json.get(ID).asText()); | ||
| 71 | + | ||
| 72 | + DeviceKey.Type type = DeviceKey.Type.valueOf(json.get(TYPE).asText()); | ||
| 73 | + String label = json.get(LABEL).asText(); | ||
| 74 | + | ||
| 75 | + if (type.equals(DeviceKey.Type.COMMUNITY_NAME)) { | ||
| 76 | + String communityName = json.get(COMMUNITY_NAME).asText(); | ||
| 77 | + return DeviceKey.createDeviceKeyUsingCommunityName(id, label, communityName); | ||
| 78 | + } else if (type.equals(DeviceKey.Type.USERNAME_PASSWORD)) { | ||
| 79 | + String username = json.get(USERNAME).asText(); | ||
| 80 | + String password = json.get(PASSWORD).asText(); | ||
| 81 | + return DeviceKey.createDeviceKeyUsingUsernamePassword(id, label, username, password); | ||
| 82 | + } else { | ||
| 83 | + log.error("Unknown device key type: ", type); | ||
| 84 | + return null; | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | +} |
| ... | @@ -45,7 +45,8 @@ public class CoreWebApplication extends AbstractWebApplication { | ... | @@ -45,7 +45,8 @@ public class CoreWebApplication extends AbstractWebApplication { |
| 45 | StatisticsWebResource.class, | 45 | StatisticsWebResource.class, |
| 46 | MetricsWebResource.class, | 46 | MetricsWebResource.class, |
| 47 | FlowObjectiveWebResource.class, | 47 | FlowObjectiveWebResource.class, |
| 48 | - MulticastRouteWebResource.class | 48 | + MulticastRouteWebResource.class, |
| 49 | + DeviceKeyWebResource.class | ||
| 49 | ); | 50 | ); |
| 50 | } | 51 | } |
| 51 | } | 52 | } | ... | ... |
| 1 | +/* | ||
| 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 | + | ||
| 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 | ||
| 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 | ||
| 59 | + * @onos.rsModel DeviceKeysGet | ||
| 60 | + */ | ||
| 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 | + * | ||
| 71 | + * @param id device identifier | ||
| 72 | + * @return 200 OK | ||
| 73 | + * @onos.rsModel DeviceKeyGet | ||
| 74 | + */ | ||
| 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 | + /** | ||
| 84 | + * Adds a new device key from the JSON request. | ||
| 85 | + * | ||
| 86 | + * @param stream input JSON | ||
| 87 | + * @return status of the request - CREATED if the JSON is correct, | ||
| 88 | + * BAD_REQUEST if the JSON is invalid | ||
| 89 | + * @onos.rsModel IntentHost | ||
| 90 | + */ | ||
| 91 | + @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 | + * | ||
| 117 | + * @param id device identifier | ||
| 118 | + * @return 200 OK | ||
| 119 | + */ | ||
| 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 | +} |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment