Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +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 | |
| 17 | package org.onosproject.vtnweb.resources; |
| 18 | |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 19 | import static javax.ws.rs.core.Response.Status.OK; |
| 20 | import static org.onlab.util.Tools.nullIsNotFound; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 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 | |
| 36 | import org.onosproject.rest.AbstractWebResource; |
| 37 | import org.onosproject.vtnrsc.PortPair; |
| 38 | import org.onosproject.vtnrsc.PortPairId; |
| 39 | import org.onosproject.vtnrsc.portpair.PortPairService; |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 43 | import com.fasterxml.jackson.databind.JsonNode; |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 45 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 46 | |
| 47 | /** |
| 48 | * Query and program port pair. |
| 49 | */ |
| 50 | @Path("port_pairs") |
| 51 | public class PortPairWebResource extends AbstractWebResource { |
| 52 | |
| 53 | private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 54 | public static final String PORT_PAIR_NOT_FOUND = "Port pair not found"; |
| 55 | public static final String PORT_PAIR_ID_EXIST = "Port pair exists"; |
| 56 | public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier"; |
| 57 | |
| 58 | /** |
| 59 | * Get details of all port pairs created. |
| 60 | * |
| 61 | * @return 200 OK |
| 62 | */ |
| 63 | @GET |
| 64 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 65 | @Consumes(MediaType.APPLICATION_JSON) |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 66 | public Response getPortPairs() { |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 67 | Iterable<PortPair> portPairs = get(PortPairService.class).getPortPairs(); |
| 68 | ObjectNode result = mapper().createObjectNode(); |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 69 | ArrayNode portPairEntry = result.putArray("port_pairs"); |
| 70 | if (portPairs != null) { |
| 71 | for (final PortPair portPair : portPairs) { |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 72 | portPairEntry.add(codec(PortPair.class).encode(portPair, this)); |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | return ok(result.toString()).build(); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get details of a specified port pair id. |
| 80 | * |
| 81 | * @param id port pair id |
| 82 | * @return 200 OK, 404 if given identifier does not exist |
| 83 | */ |
| 84 | @GET |
| 85 | @Path("{pair_id}") |
| 86 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 87 | @Consumes(MediaType.APPLICATION_JSON) |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 88 | public Response getPortPair(@PathParam("pair_id") String id) { |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 89 | PortPair portPair = nullIsNotFound(get(PortPairService.class).getPortPair(PortPairId.of(id)), |
| 90 | PORT_PAIR_NOT_FOUND); |
| 91 | ObjectNode result = mapper().createObjectNode(); |
| 92 | result.set("port_pair", codec(PortPair.class).encode(portPair, this)); |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 93 | return ok(result.toString()).build(); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Creates a new port pair. |
| 98 | * |
| 99 | * @param stream port pair from JSON |
| 100 | * @return status of the request - CREATED if the JSON is correct, |
| 101 | * BAD_REQUEST if the JSON is invalid |
| 102 | */ |
| 103 | @POST |
| 104 | @Consumes(MediaType.APPLICATION_JSON) |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
| 106 | public Response createPortPair(InputStream stream) { |
| 107 | try { |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 108 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 109 | JsonNode port = jsonTree.get("port_pair"); |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 110 | PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this); |
| 111 | Boolean isSuccess = nullIsNotFound(get(PortPairService.class).createPortPair(portPair), |
| 112 | PORT_PAIR_NOT_FOUND); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 113 | return Response.status(OK).entity(isSuccess.toString()).build(); |
| 114 | } catch (IOException e) { |
| 115 | log.error("Exception while creating port pair {}.", e.toString()); |
| 116 | throw new IllegalArgumentException(e); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Update details of a specified port pair id. |
| 122 | * |
| 123 | * @param id port pair id |
| 124 | * @param stream port pair from json |
| 125 | * @return 200 OK, 404 if the given identifier does not exist |
| 126 | */ |
| 127 | @PUT |
| 128 | @Path("{pair_id}") |
| 129 | @Produces(MediaType.APPLICATION_JSON) |
| 130 | @Consumes(MediaType.APPLICATION_JSON) |
| 131 | public Response updatePortPair(@PathParam("pair_id") String id, |
| 132 | final InputStream stream) { |
| 133 | try { |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 134 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Phaneendra Manda | 63d2470 | 2015-11-14 14:56:42 +0530 | [diff] [blame] | 135 | JsonNode port = jsonTree.get("port_pair"); |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 136 | PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this); |
| 137 | Boolean isSuccess = nullIsNotFound(get(PortPairService.class).updatePortPair(portPair), |
| 138 | PORT_PAIR_NOT_FOUND); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 139 | return Response.status(OK).entity(isSuccess.toString()).build(); |
| 140 | } catch (IOException e) { |
| 141 | log.error("Update port pair failed because of exception {}.", e.toString()); |
| 142 | throw new IllegalArgumentException(e); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Delete details of a specified port pair id. |
| 148 | * |
| 149 | * @param id port pair id |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 150 | * @return 204 NO CONTENT |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 151 | */ |
| 152 | @Path("{pair_id}") |
| 153 | @DELETE |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 154 | @Produces(MediaType.APPLICATION_JSON) |
| 155 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 156 | public Response deletePortPair(@PathParam("pair_id") String id) { |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 157 | |
Mahesh Poojary Huawei | d0b1d13 | 2015-11-05 11:00:18 +0530 | [diff] [blame] | 158 | PortPairId portPairId = PortPairId.of(id); |
Phaneendra Manda | 568734d | 2015-12-01 20:30:39 +0530 | [diff] [blame] | 159 | Boolean isSuccess = nullIsNotFound(get(PortPairService.class).removePortPair(portPairId), PORT_PAIR_NOT_FOUND); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 160 | if (!isSuccess) { |
| 161 | log.debug("Port pair identifier {} does not exist", id); |
| 162 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 163 | return Response.noContent().build(); |
Phaneendra Manda | 052d56f | 2015-10-29 16:52:46 +0530 | [diff] [blame] | 164 | } |
| 165 | } |