SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +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 | |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +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; |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 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.PortChain; |
| 38 | import org.onosproject.vtnrsc.PortChainId; |
| 39 | import org.onosproject.vtnrsc.portchain.PortChainService; |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 43 | import com.fasterxml.jackson.databind.JsonNode; |
| 44 | import com.fasterxml.jackson.databind.node.ArrayNode; |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 45 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 46 | |
| 47 | /** |
| 48 | * Query and program port chain. |
| 49 | */ |
| 50 | |
| 51 | @Path("port_chains") |
| 52 | public class PortChainWebResource extends AbstractWebResource { |
| 53 | |
| 54 | private final Logger log = LoggerFactory.getLogger(PortChainWebResource.class); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 55 | public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found"; |
| 56 | public static final String PORT_CHAIN_ID_EXIST = "Port chain exists"; |
| 57 | public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier"; |
| 58 | |
| 59 | /** |
| 60 | * Get details of all port chains created. |
| 61 | * |
| 62 | * @return 200 OK |
| 63 | */ |
| 64 | @GET |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 66 | @Consumes(MediaType.APPLICATION_JSON) |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 67 | public Response getPortChains() { |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 68 | Iterable<PortChain> portChains = get(PortChainService.class).getPortChains(); |
| 69 | ObjectNode result = mapper().createObjectNode(); |
| 70 | ArrayNode portChainEntry = result.putArray("port_chains"); |
| 71 | if (portChains != null) { |
| 72 | for (final PortChain portChain : portChains) { |
| 73 | portChainEntry.add(codec(PortChain.class).encode(portChain, this)); |
| 74 | } |
| 75 | } |
| 76 | return ok(result.toString()).build(); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get details of a specified port chain id. |
| 81 | * |
| 82 | * @param id port chain id |
| 83 | * @return 200 OK, 404 if given identifier does not exist |
| 84 | */ |
| 85 | @GET |
| 86 | @Path("{chain_id}") |
| 87 | @Produces(MediaType.APPLICATION_JSON) |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 88 | @Consumes(MediaType.APPLICATION_JSON) |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 89 | public Response getPortPain(@PathParam("chain_id") String id) { |
| 90 | |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 91 | PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)), |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 92 | PORT_CHAIN_NOT_FOUND); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 93 | ObjectNode result = mapper().createObjectNode(); |
| 94 | result.set("port_chain", codec(PortChain.class).encode(portChain, this)); |
| 95 | return ok(result.toString()).build(); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates a new port chain. |
| 100 | * |
| 101 | * @param stream port chain from JSON |
| 102 | * @return status of the request - CREATED if the JSON is correct, |
| 103 | * BAD_REQUEST if the JSON is invalid |
| 104 | */ |
| 105 | @POST |
| 106 | @Consumes(MediaType.APPLICATION_JSON) |
| 107 | @Produces(MediaType.APPLICATION_JSON) |
| 108 | public Response createPortChain(InputStream stream) { |
| 109 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 110 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 111 | JsonNode port = jsonTree.get("port_chain"); |
| 112 | PortChain portChain = codec(PortChain.class).decode((ObjectNode) port, this); |
| 113 | Boolean issuccess = nullIsNotFound(get(PortChainService.class).createPortChain(portChain), |
| 114 | PORT_CHAIN_NOT_FOUND); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 115 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 116 | } catch (IOException e) { |
| 117 | log.error("Exception while creating port chain {}.", e.toString()); |
| 118 | throw new IllegalArgumentException(e); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Update details of a specified port chain id. |
| 124 | * |
| 125 | * @param id port chain id |
| 126 | * @param stream port chain json |
| 127 | * @return 200 OK, 404 if given identifier does not exist |
| 128 | */ |
| 129 | @PUT |
| 130 | @Path("{chain_id}") |
| 131 | @Produces(MediaType.APPLICATION_JSON) |
| 132 | @Consumes(MediaType.APPLICATION_JSON) |
| 133 | public Response updatePortPain(@PathParam("chain_id") String id, |
| 134 | final InputStream stream) { |
| 135 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 136 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 137 | JsonNode port = jsonTree.get("port_chain"); |
| 138 | PortChain portChain = codec(PortChain.class).decode((ObjectNode) port, this); |
| 139 | Boolean result = nullIsNotFound(get(PortChainService.class).updatePortChain(portChain), |
| 140 | PORT_CHAIN_NOT_FOUND); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 141 | return Response.status(OK).entity(result.toString()).build(); |
| 142 | } catch (IOException e) { |
| 143 | log.error("Update port chain failed because of exception {}.", e.toString()); |
| 144 | throw new IllegalArgumentException(e); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Delete details of a specified port chain id. |
| 150 | * |
| 151 | * @param id port chain id |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 152 | * @return 204 NO CONTENT |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 153 | */ |
| 154 | @Path("{chain_id}") |
| 155 | @DELETE |
Wu wenbin | d0b119f | 2016-05-11 18:03:41 +0800 | [diff] [blame] | 156 | @Produces(MediaType.APPLICATION_JSON) |
| 157 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 158 | public Response deletePortPain(@PathParam("chain_id") String id) { |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 159 | log.debug("Deletes port chain by identifier {}.", id); |
Mahesh Poojary Huawei | a9feb6d | 2015-11-05 13:24:16 +0530 | [diff] [blame] | 160 | PortChainId portChainId = PortChainId.of(id); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 161 | |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 162 | Boolean issuccess = nullIsNotFound(get(PortChainService.class).removePortChain(portChainId), |
| 163 | PORT_CHAIN_NOT_FOUND); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 164 | if (!issuccess) { |
| 165 | log.debug("Port Chain identifier {} does not exist", id); |
| 166 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 167 | return Response.noContent().build(); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 168 | } |
| 169 | } |