Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [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 | package org.onosproject.vtnweb.resources; |
| 17 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 18 | import static javax.ws.rs.core.Response.Status.OK; |
| 19 | import static org.onlab.util.Tools.nullIsNotFound; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.Consumes; |
| 25 | import javax.ws.rs.DELETE; |
| 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.PUT; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 35 | import org.onosproject.rest.AbstractWebResource; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 36 | import org.onosproject.vtnrsc.FlowClassifier; |
| 37 | import org.onosproject.vtnrsc.FlowClassifierId; |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 38 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 41 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 42 | import com.fasterxml.jackson.databind.JsonNode; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 43 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 45 | |
| 46 | /** |
| 47 | * Query and program flow classifier. |
| 48 | */ |
| 49 | @Path("flow_classifiers") |
| 50 | public class FlowClassifierWebResource extends AbstractWebResource { |
| 51 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 52 | private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); |
| 53 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 54 | public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; |
| 55 | |
| 56 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 57 | * Get all flow classifiers created. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 58 | * |
Bharat saraswal | a9929a9 | 2015-11-19 00:33:55 +0530 | [diff] [blame] | 59 | * @return 200 OK |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 60 | */ |
| 61 | @GET |
| 62 | @Produces(MediaType.APPLICATION_JSON) |
| 63 | public Response getFlowClassifiers() { |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 64 | Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers(); |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 65 | ObjectNode result = mapper().createObjectNode(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 66 | ArrayNode flowClassifierEntry = result.putArray("flow_classifiers"); |
| 67 | if (flowClassifiers != null) { |
| 68 | for (final FlowClassifier flowClassifier : flowClassifiers) { |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 69 | flowClassifierEntry.add(codec(FlowClassifier.class).encode(flowClassifier, this)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 70 | } |
| 71 | } |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 72 | return ok(result.toString()).build(); |
| 73 | } |
| 74 | |
| 75 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 76 | * Get details of a flow classifier. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 77 | * |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 78 | * @param id |
| 79 | * flow classifier id |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 80 | * @return 200 OK , 404 if given identifier does not exist |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 81 | */ |
| 82 | @GET |
| 83 | @Path("{flow_id}") |
| 84 | @Produces(MediaType.APPLICATION_JSON) |
| 85 | public Response getFlowClassifier(@PathParam("flow_id") String id) { |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 86 | FlowClassifier flowClassifier = nullIsNotFound(get(FlowClassifierService.class) |
| 87 | .getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 88 | |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 89 | ObjectNode result = mapper().createObjectNode(); |
| 90 | result.set("flow_classifier", codec(FlowClassifier.class).encode(flowClassifier, this)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 91 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 92 | return ok(result.toString()).build(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates and stores a new flow classifier. |
| 97 | * |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 98 | * @param stream |
| 99 | * flow classifier from JSON |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 100 | * @return status of the request - CREATED if the JSON is correct, |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 101 | * BAD_REQUEST if the JSON is invalid |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 102 | */ |
| 103 | @POST |
| 104 | @Consumes(MediaType.APPLICATION_JSON) |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
| 106 | public Response createFlowClassifier(InputStream stream) { |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 107 | try { |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 108 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 109 | JsonNode flow = jsonTree.get("flow_classifier"); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 110 | |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 111 | FlowClassifier flowClassifier = codec(FlowClassifier.class).decode((ObjectNode) flow, this); |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 112 | Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier), |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 113 | FLOW_CLASSIFIER_NOT_FOUND); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 114 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 115 | } catch (IOException ex) { |
| 116 | log.error("Exception while creating flow classifier {}.", ex.toString()); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 117 | throw new IllegalArgumentException(ex); |
| 118 | } |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 122 | * Update details of a flow classifier. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 123 | * |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 124 | * @param id |
| 125 | * flow classifier id |
| 126 | * @param stream |
| 127 | * InputStream |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 128 | * @return 200 OK, 404 if given identifier does not exist |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 129 | */ |
| 130 | @PUT |
| 131 | @Path("{flow_id}") |
| 132 | @Produces(MediaType.APPLICATION_JSON) |
| 133 | @Consumes(MediaType.APPLICATION_JSON) |
| 134 | public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) { |
| 135 | try { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 136 | |
| 137 | JsonNode jsonTree = mapper().readTree(stream); |
| 138 | JsonNode flow = jsonTree.get("flow_classifier"); |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 139 | FlowClassifier flowClassifier = codec(FlowClassifier.class).decode((ObjectNode) flow, this); |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 140 | Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier), |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 141 | FLOW_CLASSIFIER_NOT_FOUND); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 142 | return Response.status(OK).entity(result.toString()).build(); |
| 143 | } catch (IOException e) { |
| 144 | log.error("Update flow classifier failed because of exception {}.", e.toString()); |
| 145 | throw new IllegalArgumentException(e); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 150 | * Delete details of a flow classifier. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 151 | * |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 152 | * @param id |
| 153 | * flow classifier id |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 154 | */ |
| 155 | @Path("{flow_id}") |
| 156 | @DELETE |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 157 | public void deleteFlowClassifier(@PathParam("flow_id") String id) { |
| 158 | log.debug("Deletes flow classifier by identifier {}.", id); |
| 159 | FlowClassifierId flowClassifierId = FlowClassifierId.of(id); |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 160 | Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId), |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 161 | FLOW_CLASSIFIER_NOT_FOUND); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 162 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 163 | } |
| 164 | } |