2  * Copyright 2014-2015 Open Networking Laboratory
 
   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
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  16 package org.onosproject.vtnweb.resources;
 
  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;
 
  22 import java.io.IOException;
 
  23 import java.io.InputStream;
 
  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;
 
  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;
 
  44 import com.fasterxml.jackson.databind.ObjectMapper;
 
  45 import com.fasterxml.jackson.databind.node.ObjectNode;
 
  48  * Query and program port chain.
 
  52 public class PortChainWebResource extends AbstractWebResource {
 
  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";
 
  61      * Get details of all port chains created.
 
  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();
 
  75      * Get details of a specified port chain id.
 
  77      * @param id port chain id
 
  78      * @return 200 OK, 404 if given identifier does not exist
 
  82     @Produces(MediaType.APPLICATION_JSON)
 
  83     public Response getPortPain(@PathParam("chain_id") String id) {
 
  85         if (!service.exists(PortChainId.of(id))) {
 
  86             return Response.status(NOT_FOUND).entity(PORT_CHAIN_NOT_FOUND).build();
 
  88         PortChain portChain = nullIsNotFound(service.getPortChain(PortChainId.of(id)),
 
  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();
 
  96      * Creates a new port chain.
 
  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
 
 103     @Consumes(MediaType.APPLICATION_JSON)
 
 104     @Produces(MediaType.APPLICATION_JSON)
 
 105     public Response createPortChain(InputStream stream) {
 
 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);
 
 118      * Update details of a specified port chain id.
 
 120      * @param id port chain id
 
 121      * @param stream port chain json
 
 122      * @return 200 OK, 404 if given identifier does not exist
 
 126     @Produces(MediaType.APPLICATION_JSON)
 
 127     @Consumes(MediaType.APPLICATION_JSON)
 
 128     public Response updatePortPain(@PathParam("chain_id") String id,
 
 129                                    final InputStream stream) {
 
 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);
 
 142      * Delete details of a specified port chain id.
 
 144      * @param id port chain id
 
 148     public void deletePortPain(@PathParam("chain_id") String id) {
 
 149         log.debug("Deletes port chain by identifier {}.", id);
 
 150         PortChainId portChainId = PortChainId.of(id);
 
 152         Boolean issuccess = nullIsNotFound(service.removePortChain(portChainId), PORT_CHAIN_NOT_FOUND);
 
 154             log.debug("Port Chain identifier {} does not exist", id);