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 | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 18 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 19 | import static javax.ws.rs.core.Response.Status.OK; |
| 20 | import static org.onlab.util.Tools.nullIsNotFound; |
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; |
samueljcc | e3d5b6a | 2015-10-27 15:52:52 -0700 | [diff] [blame] | 40 | import org.onosproject.vtnweb.web.FlowClassifierCodec; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 43 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.JsonNode; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 45 | import com.fasterxml.jackson.databind.ObjectMapper; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 46 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 47 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 48 | |
| 49 | /** |
| 50 | * Query and program flow classifier. |
| 51 | */ |
| 52 | @Path("flow_classifiers") |
| 53 | public class FlowClassifierWebResource extends AbstractWebResource { |
| 54 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 55 | private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); |
| 56 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 57 | final FlowClassifierService service = get(FlowClassifierService.class); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 58 | public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; |
| 59 | |
| 60 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 61 | * Get all flow classifiers created. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 62 | * |
Bharat saraswal | a9929a9 | 2015-11-19 00:33:55 +0530 | [diff] [blame^] | 63 | * @return 200 OK |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 64 | */ |
| 65 | @GET |
| 66 | @Produces(MediaType.APPLICATION_JSON) |
| 67 | public Response getFlowClassifiers() { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 68 | final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 69 | ObjectNode result = new ObjectMapper().createObjectNode(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 70 | ArrayNode flowClassifierEntry = result.putArray("flow_classifiers"); |
| 71 | if (flowClassifiers != null) { |
| 72 | for (final FlowClassifier flowClassifier : flowClassifiers) { |
| 73 | flowClassifierEntry.add(new FlowClassifierCodec().encode(flowClassifier, this)); |
| 74 | } |
| 75 | } |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 76 | return ok(result.toString()).build(); |
| 77 | } |
| 78 | |
| 79 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 80 | * Get details of a flow classifier. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 81 | * |
| 82 | * @param id flow classifier id |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 83 | * @return 200 OK , 404 if given identifier does not exist |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 84 | */ |
| 85 | @GET |
| 86 | @Path("{flow_id}") |
| 87 | @Produces(MediaType.APPLICATION_JSON) |
| 88 | public Response getFlowClassifier(@PathParam("flow_id") String id) { |
| 89 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 90 | if (!service.hasFlowClassifier(FlowClassifierId.of(id))) { |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 91 | return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); |
| 92 | } |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 93 | FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)), |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 94 | FLOW_CLASSIFIER_NOT_FOUND); |
| 95 | |
| 96 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 97 | result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 98 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 99 | return ok(result.toString()).build(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Creates and stores a new flow classifier. |
| 104 | * |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 105 | * @param stream flow classifier from JSON |
| 106 | * @return status of the request - CREATED if the JSON is correct, |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 107 | * BAD_REQUEST if the JSON is invalid |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 108 | */ |
| 109 | @POST |
| 110 | @Consumes(MediaType.APPLICATION_JSON) |
| 111 | @Produces(MediaType.APPLICATION_JSON) |
| 112 | public Response createFlowClassifier(InputStream stream) { |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 113 | try { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 114 | ObjectMapper mapper = new ObjectMapper(); |
| 115 | ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); |
| 116 | JsonNode flow = jsonTree.get("flow_classifier"); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 117 | |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 118 | FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); |
| 119 | Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); |
| 120 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 121 | } catch (IOException ex) { |
| 122 | log.error("Exception while creating flow classifier {}.", ex.toString()); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 123 | throw new IllegalArgumentException(ex); |
| 124 | } |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 128 | * Update details of a flow classifier. |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 129 | * |
| 130 | * @param id flow classifier id |
| 131 | * @param stream InputStream |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 132 | * @return 200 OK, 404 if given identifier does not exist |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 133 | */ |
| 134 | @PUT |
| 135 | @Path("{flow_id}") |
| 136 | @Produces(MediaType.APPLICATION_JSON) |
| 137 | @Consumes(MediaType.APPLICATION_JSON) |
| 138 | public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) { |
| 139 | try { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 140 | |
| 141 | JsonNode jsonTree = mapper().readTree(stream); |
| 142 | JsonNode flow = jsonTree.get("flow_classifier"); |
| 143 | FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 144 | Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), 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 | * |
| 155 | * @param id flow classifier id |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 156 | */ |
| 157 | @Path("{flow_id}") |
| 158 | @DELETE |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 159 | public void deleteFlowClassifier(@PathParam("flow_id") String id) { |
| 160 | log.debug("Deletes flow classifier by identifier {}.", id); |
| 161 | FlowClassifierId flowClassifierId = FlowClassifierId.of(id); |
| 162 | Boolean issuccess = nullIsNotFound(service.removeFlowClassifier(flowClassifierId), FLOW_CLASSIFIER_NOT_FOUND); |
| 163 | |
Bharat saraswal | d25a30a | 2015-10-26 18:35:47 +0530 | [diff] [blame] | 164 | } |
| 165 | } |