SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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 | |
| 18 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
| 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.PortChain; |
| 38 | import org.onosproject.vtnrsc.PortChainId; |
| 39 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 40 | import org.onosproject.vtnweb.web.PortChainCodec; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 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); |
| 55 | private final PortChainService service = get(PortChainService.class); |
| 56 | public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found"; |
| 57 | public static final String PORT_CHAIN_ID_EXIST = "Port chain exists"; |
| 58 | public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier"; |
| 59 | |
| 60 | /** |
| 61 | * Get details of all port chains created. |
| 62 | * |
| 63 | * @return 200 OK |
| 64 | */ |
| 65 | @GET |
| 66 | @Produces(MediaType.APPLICATION_JSON) |
| 67 | public Response getPortChains() { |
| 68 | Iterable<PortChain> portChains = service.getPortChains(); |
| 69 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 70 | result.set("port_chains", new PortChainCodec().encode(portChains, this)); |
| 71 | return ok(result).build(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get details of a specified port chain id. |
| 76 | * |
| 77 | * @param id port chain id |
| 78 | * @return 200 OK, 404 if given identifier does not exist |
| 79 | */ |
| 80 | @GET |
| 81 | @Path("{chain_id}") |
| 82 | @Produces(MediaType.APPLICATION_JSON) |
| 83 | public Response getPortPain(@PathParam("chain_id") String id) { |
| 84 | |
Mahesh Poojary Huawei | a9feb6d | 2015-11-05 13:24:16 +0530 | [diff] [blame^] | 85 | if (!service.exists(PortChainId.of(id))) { |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 86 | return Response.status(NOT_FOUND).entity(PORT_CHAIN_NOT_FOUND).build(); |
| 87 | } |
Mahesh Poojary Huawei | a9feb6d | 2015-11-05 13:24:16 +0530 | [diff] [blame^] | 88 | PortChain portChain = nullIsNotFound(service.getPortChain(PortChainId.of(id)), |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 89 | PORT_CHAIN_NOT_FOUND); |
| 90 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 91 | result.set("port_chain", new PortChainCodec().encode(portChain, this)); |
| 92 | return ok(result).build(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a new port chain. |
| 97 | * |
| 98 | * @param stream port chain from JSON |
| 99 | * @return status of the request - CREATED if the JSON is correct, |
| 100 | * BAD_REQUEST if the JSON is invalid |
| 101 | */ |
| 102 | @POST |
| 103 | @Consumes(MediaType.APPLICATION_JSON) |
| 104 | @Produces(MediaType.APPLICATION_JSON) |
| 105 | public Response createPortChain(InputStream stream) { |
| 106 | try { |
| 107 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 108 | PortChain portChain = codec(PortChain.class).decode(jsonTree, this); |
| 109 | Boolean issuccess = nullIsNotFound(service.createPortChain(portChain), PORT_CHAIN_NOT_FOUND); |
| 110 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 111 | } catch (IOException e) { |
| 112 | log.error("Exception while creating port chain {}.", e.toString()); |
| 113 | throw new IllegalArgumentException(e); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Update details of a specified port chain id. |
| 119 | * |
| 120 | * @param id port chain id |
| 121 | * @param stream port chain json |
| 122 | * @return 200 OK, 404 if given identifier does not exist |
| 123 | */ |
| 124 | @PUT |
| 125 | @Path("{chain_id}") |
| 126 | @Produces(MediaType.APPLICATION_JSON) |
| 127 | @Consumes(MediaType.APPLICATION_JSON) |
| 128 | public Response updatePortPain(@PathParam("chain_id") String id, |
| 129 | final InputStream stream) { |
| 130 | try { |
| 131 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 132 | PortChain portChain = codec(PortChain.class).decode(jsonTree, this); |
| 133 | Boolean result = nullIsNotFound(service.updatePortChain(portChain), PORT_CHAIN_NOT_FOUND); |
| 134 | return Response.status(OK).entity(result.toString()).build(); |
| 135 | } catch (IOException e) { |
| 136 | log.error("Update port chain failed because of exception {}.", e.toString()); |
| 137 | throw new IllegalArgumentException(e); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Delete details of a specified port chain id. |
| 143 | * |
| 144 | * @param id port chain id |
| 145 | */ |
| 146 | @Path("{chain_id}") |
| 147 | @DELETE |
| 148 | public void deletePortPain(@PathParam("chain_id") String id) { |
| 149 | log.debug("Deletes port chain by identifier {}.", id); |
Mahesh Poojary Huawei | a9feb6d | 2015-11-05 13:24:16 +0530 | [diff] [blame^] | 150 | PortChainId portChainId = PortChainId.of(id); |
SureshBR | 8fc55f3 | 2015-10-29 19:34:28 +0530 | [diff] [blame] | 151 | |
| 152 | Boolean issuccess = nullIsNotFound(service.removePortChain(portChainId), PORT_CHAIN_NOT_FOUND); |
| 153 | if (!issuccess) { |
| 154 | log.debug("Port Chain identifier {} does not exist", id); |
| 155 | } |
| 156 | } |
| 157 | } |