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