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