Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 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 | |
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; |
samueljcc | e3d5b6a | 2015-10-27 15:52:52 -0700 | [diff] [blame] | 39 | import org.onosproject.vtnweb.web.FlowClassifierCodec; |
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 | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.ObjectMapper; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 45 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 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 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 54 | private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); |
| 55 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 56 | public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; |
| 57 | |
| 58 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 59 | * Get all flow classifiers created. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 60 | * |
Bharat saraswal | a9929a9 | 2015-11-19 00:33:55 +0530 | [diff] [blame] | 61 | * @return 200 OK |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 62 | */ |
| 63 | @GET |
| 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(); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 67 | ObjectNode result = new ObjectMapper().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) { |
| 71 | flowClassifierEntry.add(new FlowClassifierCodec().encode(flowClassifier, this)); |
| 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}") |
| 86 | @Produces(MediaType.APPLICATION_JSON) |
| 87 | public Response getFlowClassifier(@PathParam("flow_id") String id) { |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame^] | 88 | FlowClassifier flowClassifier = nullIsNotFound( |
| 89 | get(FlowClassifierService.class).getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 90 | |
| 91 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 92 | result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 93 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 94 | return ok(result.toString()).build(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Creates and stores a new flow classifier. |
| 99 | * |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame^] | 100 | * @param stream |
| 101 | * flow classifier from JSON |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 102 | * @return status of the request - CREATED if the JSON is correct, |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame^] | 103 | * BAD_REQUEST if the JSON is invalid |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 104 | */ |
| 105 | @POST |
| 106 | @Consumes(MediaType.APPLICATION_JSON) |
| 107 | @Produces(MediaType.APPLICATION_JSON) |
| 108 | public Response createFlowClassifier(InputStream stream) { |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 109 | try { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 110 | ObjectMapper mapper = new ObjectMapper(); |
| 111 | ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); |
| 112 | JsonNode flow = jsonTree.get("flow_classifier"); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 113 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 114 | FlowClassifier flowClassifier = new FlowClassifierCodec().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), |
| 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 | |
| 140 | JsonNode jsonTree = mapper().readTree(stream); |
| 141 | JsonNode flow = jsonTree.get("flow_classifier"); |
| 142 | FlowClassifier flowClassifier = new FlowClassifierCodec().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), |
| 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 | * |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame^] | 155 | * @param id |
| 156 | * flow classifier id |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 157 | */ |
| 158 | @Path("{flow_id}") |
| 159 | @DELETE |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 160 | public void deleteFlowClassifier(@PathParam("flow_id") String id) { |
| 161 | log.debug("Deletes flow classifier by identifier {}.", id); |
| 162 | FlowClassifierId flowClassifierId = FlowClassifierId.of(id); |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame^] | 163 | Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId), |
| 164 | FLOW_CLASSIFIER_NOT_FOUND); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 165 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 166 | } |
| 167 | } |