Committed by
Ray Milkey
[ONOS-3113] Web Resource for Flow classifier in SFC
Change-Id: Id5e3a85883e8fb682bb43c45400860a3b07d05fe
Showing
1 changed file
with
192 additions
and
0 deletions
apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/FlowClassifierWebResource.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2015 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 | +package org.onosproject.vtnweb.resources; | ||
| 17 | + | ||
| 18 | +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
| 19 | +import static org.onlab.util.Tools.nullIsNotFound; | ||
| 20 | +import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
| 21 | + | ||
| 22 | +import java.io.IOException; | ||
| 23 | +import java.io.InputStream; | ||
| 24 | +import java.net.URI; | ||
| 25 | +import java.net.URISyntaxException; | ||
| 26 | +import java.util.UUID; | ||
| 27 | + | ||
| 28 | +import javax.ws.rs.Consumes; | ||
| 29 | +import javax.ws.rs.DELETE; | ||
| 30 | +import javax.ws.rs.GET; | ||
| 31 | +import javax.ws.rs.POST; | ||
| 32 | +import javax.ws.rs.PUT; | ||
| 33 | +import javax.ws.rs.Path; | ||
| 34 | +import javax.ws.rs.PathParam; | ||
| 35 | +import javax.ws.rs.Produces; | ||
| 36 | +import javax.ws.rs.core.MediaType; | ||
| 37 | +import javax.ws.rs.core.Response; | ||
| 38 | + | ||
| 39 | +import org.onosproject.vtnrsc.FlowClassifier; | ||
| 40 | +import org.onosproject.vtnrsc.FlowClassifierId; | ||
| 41 | +import org.onosproject.rest.AbstractWebResource; | ||
| 42 | +import org.onosproject.vtnrsc.flowClassifier.FlowClassifierService; | ||
| 43 | +import org.onosproject.vtnrsc.web.FlowClassifierCodec; | ||
| 44 | + | ||
| 45 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 46 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 47 | + | ||
| 48 | +/** | ||
| 49 | + * Query and program flow classifier. | ||
| 50 | + */ | ||
| 51 | +@Path("flow_classifiers") | ||
| 52 | +public class FlowClassifierWebResource extends AbstractWebResource { | ||
| 53 | + | ||
| 54 | + final FlowClassifierService service = get(FlowClassifierService.class); | ||
| 55 | + final ObjectNode root = mapper().createObjectNode(); | ||
| 56 | + public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * Get all flow classifiers created. Returns list of all flow classifiers | ||
| 60 | + * created. | ||
| 61 | + * | ||
| 62 | + * @return 200 OK | ||
| 63 | + */ | ||
| 64 | + @GET | ||
| 65 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 66 | + public Response getFlowClassifiers() { | ||
| 67 | + Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); | ||
| 68 | + ObjectNode result = new ObjectMapper().createObjectNode(); | ||
| 69 | + result.set("flow_classifiers", new FlowClassifierCodec().encode(flowClassifiers, this)); | ||
| 70 | + return ok(result.toString()).build(); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Get details of a flow classifier. Returns details of a specified flow | ||
| 75 | + * classifier id. | ||
| 76 | + * | ||
| 77 | + * @param id flow classifier id | ||
| 78 | + * @return 200 OK | ||
| 79 | + */ | ||
| 80 | + @GET | ||
| 81 | + @Path("{flow_id}") | ||
| 82 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 83 | + public Response getFlowClassifier(@PathParam("flow_id") String id) { | ||
| 84 | + | ||
| 85 | + if (!service.hasFlowClassifier(FlowClassifierId.flowClassifierId(UUID.fromString(id)))) { | ||
| 86 | + return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); | ||
| 87 | + } | ||
| 88 | + FlowClassifier flowClassifier = nullIsNotFound( | ||
| 89 | + service.getFlowClassifier(FlowClassifierId.flowClassifierId(UUID.fromString(id))), | ||
| 90 | + FLOW_CLASSIFIER_NOT_FOUND); | ||
| 91 | + | ||
| 92 | + ObjectNode result = new ObjectMapper().createObjectNode(); | ||
| 93 | + result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); | ||
| 94 | + return ok(result.toString()).build(); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * Creates and stores a new flow classifier. | ||
| 99 | + * | ||
| 100 | + * @param flowClassifierId flow classifier identifier | ||
| 101 | + * @param stream flow classifier from JSON | ||
| 102 | + * @return status of the request - CREATED if the JSON is correct, | ||
| 103 | + * BAD_REQUEST if the JSON is invalid | ||
| 104 | + */ | ||
| 105 | + @POST | ||
| 106 | + @Path("{flow_id}") | ||
| 107 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 108 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 109 | + public Response createFlowClassifier(@PathParam("flow_id") String flowClassifierId, InputStream stream) { | ||
| 110 | + URI location; | ||
| 111 | + try { | ||
| 112 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
| 113 | + | ||
| 114 | + FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); | ||
| 115 | + service.createFlowClassifier(flowClassifier); | ||
| 116 | + location = new URI(flowClassifierId); | ||
| 117 | + } catch (IOException | URISyntaxException ex) { | ||
| 118 | + throw new IllegalArgumentException(ex); | ||
| 119 | + } | ||
| 120 | + return Response.created(location).build(); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + /** | ||
| 124 | + * Creates and stores a new flow classifier. | ||
| 125 | + * | ||
| 126 | + * @param stream flow classifier from JSON | ||
| 127 | + * @return status of the request - CREATED if the JSON is correct, | ||
| 128 | + * BAD_REQUEST if the JSON is invalid | ||
| 129 | + */ | ||
| 130 | + @POST | ||
| 131 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 132 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 133 | + public Response createFlowClassifier(InputStream stream) { | ||
| 134 | + URI location; | ||
| 135 | + try { | ||
| 136 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
| 137 | + | ||
| 138 | + FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); | ||
| 139 | + service.createFlowClassifier(flowClassifier); | ||
| 140 | + location = new URI(flowClassifier.flowClassifierId().toString()); | ||
| 141 | + } catch (IOException | URISyntaxException ex) { | ||
| 142 | + throw new IllegalArgumentException(ex); | ||
| 143 | + } | ||
| 144 | + return Response.created(location).build(); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * Update details of a flow classifier. Update details of a specified flow | ||
| 149 | + * classifier id. | ||
| 150 | + * | ||
| 151 | + * @param id flow classifier id | ||
| 152 | + * @param stream InputStream | ||
| 153 | + * @return 200 OK | ||
| 154 | + */ | ||
| 155 | + @PUT | ||
| 156 | + @Path("{flow_id}") | ||
| 157 | + @Produces(MediaType.APPLICATION_JSON) | ||
| 158 | + @Consumes(MediaType.APPLICATION_JSON) | ||
| 159 | + public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) { | ||
| 160 | + try { | ||
| 161 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
| 162 | + FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); | ||
| 163 | + Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); | ||
| 164 | + if (!result) { | ||
| 165 | + return Response.status(204).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); | ||
| 166 | + } | ||
| 167 | + return Response.status(203).entity(result.toString()).build(); | ||
| 168 | + } catch (Exception e) { | ||
| 169 | + return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /** | ||
| 174 | + * Delete details of a flow classifier. Delete details of a specified flow | ||
| 175 | + * classifier id. | ||
| 176 | + * | ||
| 177 | + * @param id flow classifier id | ||
| 178 | + * @return 200 OK | ||
| 179 | + * @throws IOException when input doesn't match. | ||
| 180 | + */ | ||
| 181 | + @Path("{flow_id}") | ||
| 182 | + @DELETE | ||
| 183 | + public Response deleteFlowClassifier(@PathParam("flow_id") String id) throws IOException { | ||
| 184 | + try { | ||
| 185 | + FlowClassifierId flowClassifierId = FlowClassifierId.flowClassifierId(UUID.fromString(id)); | ||
| 186 | + service.removeFlowClassifier(flowClassifierId); | ||
| 187 | + return Response.status(201).entity("SUCCESS").build(); | ||
| 188 | + } catch (Exception e) { | ||
| 189 | + return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | +} |
-
Please register or login to post a comment