blob: 81e294047cfa3bbe7fef6fe690b0bc6acd502e29 [file] [log] [blame]
SureshBR8fc55f32015-10-29 19:34:28 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
SureshBR8fc55f32015-10-29 19:34:28 +05303 *
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 */
16package org.onosproject.vtnweb.resources;
17
SureshBR8fc55f32015-10-29 19:34:28 +053018import static javax.ws.rs.core.Response.Status.OK;
19import static org.onlab.util.Tools.nullIsNotFound;
20
21import java.io.IOException;
22import java.io.InputStream;
23
24import javax.ws.rs.Consumes;
25import javax.ws.rs.DELETE;
26import javax.ws.rs.GET;
27import javax.ws.rs.POST;
28import javax.ws.rs.PUT;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34
35import org.onosproject.rest.AbstractWebResource;
36import org.onosproject.vtnrsc.PortChain;
37import org.onosproject.vtnrsc.PortChainId;
38import org.onosproject.vtnrsc.portchain.PortChainService;
SureshBR8fc55f32015-10-29 19:34:28 +053039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
SureshBR47a52922015-12-01 20:51:10 +053042import com.fasterxml.jackson.databind.JsonNode;
43import com.fasterxml.jackson.databind.node.ArrayNode;
SureshBR8fc55f32015-10-29 19:34:28 +053044import com.fasterxml.jackson.databind.node.ObjectNode;
45
46/**
47 * Query and program port chain.
48 */
49
50@Path("port_chains")
51public class PortChainWebResource extends AbstractWebResource {
52
53 private final Logger log = LoggerFactory.getLogger(PortChainWebResource.class);
SureshBR8fc55f32015-10-29 19:34:28 +053054 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)
65 public Response getPortChains() {
SureshBR47a52922015-12-01 20:51:10 +053066 Iterable<PortChain> portChains = get(PortChainService.class).getPortChains();
67 ObjectNode result = mapper().createObjectNode();
68 ArrayNode portChainEntry = result.putArray("port_chains");
69 if (portChains != null) {
70 for (final PortChain portChain : portChains) {
71 portChainEntry.add(codec(PortChain.class).encode(portChain, this));
72 }
73 }
74 return ok(result.toString()).build();
SureshBR8fc55f32015-10-29 19:34:28 +053075 }
76
77 /**
78 * Get details of a specified port chain id.
79 *
80 * @param id port chain id
81 * @return 200 OK, 404 if given identifier does not exist
82 */
83 @GET
84 @Path("{chain_id}")
85 @Produces(MediaType.APPLICATION_JSON)
86 public Response getPortPain(@PathParam("chain_id") String id) {
87
SureshBR47a52922015-12-01 20:51:10 +053088 PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)),
SureshBR8fc55f32015-10-29 19:34:28 +053089 PORT_CHAIN_NOT_FOUND);
SureshBR47a52922015-12-01 20:51:10 +053090 ObjectNode result = mapper().createObjectNode();
91 result.set("port_chain", codec(PortChain.class).encode(portChain, this));
92 return ok(result.toString()).build();
SureshBR8fc55f32015-10-29 19:34:28 +053093 }
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);
SureshBR47a52922015-12-01 20:51:10 +0530108 JsonNode port = jsonTree.get("port_chain");
109 PortChain portChain = codec(PortChain.class).decode((ObjectNode) port, this);
110 Boolean issuccess = nullIsNotFound(get(PortChainService.class).createPortChain(portChain),
111 PORT_CHAIN_NOT_FOUND);
SureshBR8fc55f32015-10-29 19:34:28 +0530112 return Response.status(OK).entity(issuccess.toString()).build();
113 } catch (IOException e) {
114 log.error("Exception while creating port chain {}.", e.toString());
115 throw new IllegalArgumentException(e);
116 }
117 }
118
119 /**
120 * Update details of a specified port chain id.
121 *
122 * @param id port chain id
123 * @param stream port chain json
124 * @return 200 OK, 404 if given identifier does not exist
125 */
126 @PUT
127 @Path("{chain_id}")
128 @Produces(MediaType.APPLICATION_JSON)
129 @Consumes(MediaType.APPLICATION_JSON)
130 public Response updatePortPain(@PathParam("chain_id") String id,
131 final InputStream stream) {
132 try {
133 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
SureshBR47a52922015-12-01 20:51:10 +0530134 JsonNode port = jsonTree.get("port_chain");
135 PortChain portChain = codec(PortChain.class).decode((ObjectNode) port, this);
136 Boolean result = nullIsNotFound(get(PortChainService.class).updatePortChain(portChain),
137 PORT_CHAIN_NOT_FOUND);
SureshBR8fc55f32015-10-29 19:34:28 +0530138 return Response.status(OK).entity(result.toString()).build();
139 } catch (IOException e) {
140 log.error("Update port chain failed because of exception {}.", e.toString());
141 throw new IllegalArgumentException(e);
142 }
143 }
144
145 /**
146 * Delete details of a specified port chain id.
147 *
148 * @param id port chain id
149 */
150 @Path("{chain_id}")
151 @DELETE
152 public void deletePortPain(@PathParam("chain_id") String id) {
153 log.debug("Deletes port chain by identifier {}.", id);
Mahesh Poojary Huaweia9feb6d2015-11-05 13:24:16 +0530154 PortChainId portChainId = PortChainId.of(id);
SureshBR8fc55f32015-10-29 19:34:28 +0530155
SureshBR47a52922015-12-01 20:51:10 +0530156 Boolean issuccess = nullIsNotFound(get(PortChainService.class).removePortChain(portChainId),
157 PORT_CHAIN_NOT_FOUND);
SureshBR8fc55f32015-10-29 19:34:28 +0530158 if (!issuccess) {
159 log.debug("Port Chain identifier {} does not exist", id);
160 }
161 }
162}