blob: 1bfa44f920e9f6f3103e647cb2d5752028bbf7b4 [file] [log] [blame]
SureshBR8fc55f32015-10-29 19:34:28 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyb784adb2018-04-02 15:33:07 -070020import static org.onlab.util.Tools.readTreeFromStream;
SureshBR8fc55f32015-10-29 19:34:28 +053021
22import java.io.IOException;
23import java.io.InputStream;
24
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.PUT;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35
36import org.onosproject.rest.AbstractWebResource;
37import org.onosproject.vtnrsc.PortChain;
38import org.onosproject.vtnrsc.PortChainId;
39import org.onosproject.vtnrsc.portchain.PortChainService;
SureshBR8fc55f32015-10-29 19:34:28 +053040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
SureshBR47a52922015-12-01 20:51:10 +053043import com.fasterxml.jackson.databind.JsonNode;
44import com.fasterxml.jackson.databind.node.ArrayNode;
SureshBR8fc55f32015-10-29 19:34:28 +053045import com.fasterxml.jackson.databind.node.ObjectNode;
46
47/**
48 * Query and program port chain.
49 */
50
51@Path("port_chains")
52public class PortChainWebResource extends AbstractWebResource {
53
54 private final Logger log = LoggerFactory.getLogger(PortChainWebResource.class);
SureshBR8fc55f32015-10-29 19:34:28 +053055 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 wenbind0b119f2016-05-11 18:03:41 +080066 @Consumes(MediaType.APPLICATION_JSON)
SureshBR8fc55f32015-10-29 19:34:28 +053067 public Response getPortChains() {
SureshBR47a52922015-12-01 20:51:10 +053068 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();
SureshBR8fc55f32015-10-29 19:34:28 +053077 }
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 wenbind0b119f2016-05-11 18:03:41 +080088 @Consumes(MediaType.APPLICATION_JSON)
SureshBR8fc55f32015-10-29 19:34:28 +053089 public Response getPortPain(@PathParam("chain_id") String id) {
90
SureshBR47a52922015-12-01 20:51:10 +053091 PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)),
SureshBR8fc55f32015-10-29 19:34:28 +053092 PORT_CHAIN_NOT_FOUND);
SureshBR47a52922015-12-01 20:51:10 +053093 ObjectNode result = mapper().createObjectNode();
94 result.set("port_chain", codec(PortChain.class).encode(portChain, this));
95 return ok(result.toString()).build();
SureshBR8fc55f32015-10-29 19:34:28 +053096 }
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 Milkeyb784adb2018-04-02 15:33:07 -0700110 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
SureshBR47a52922015-12-01 20:51:10 +0530111 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);
SureshBR8fc55f32015-10-29 19:34:28 +0530115 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 Milkeyb784adb2018-04-02 15:33:07 -0700136 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
SureshBR47a52922015-12-01 20:51:10 +0530137 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);
SureshBR8fc55f32015-10-29 19:34:28 +0530141 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 Lic2a542b2016-05-10 11:48:19 -0700152 * @return 204 NO CONTENT
SureshBR8fc55f32015-10-29 19:34:28 +0530153 */
154 @Path("{chain_id}")
155 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800156 @Produces(MediaType.APPLICATION_JSON)
157 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700158 public Response deletePortPain(@PathParam("chain_id") String id) {
SureshBR8fc55f32015-10-29 19:34:28 +0530159 log.debug("Deletes port chain by identifier {}.", id);
Mahesh Poojary Huaweia9feb6d2015-11-05 13:24:16 +0530160 PortChainId portChainId = PortChainId.of(id);
SureshBR8fc55f32015-10-29 19:34:28 +0530161
SureshBR47a52922015-12-01 20:51:10 +0530162 Boolean issuccess = nullIsNotFound(get(PortChainService.class).removePortChain(portChainId),
163 PORT_CHAIN_NOT_FOUND);
SureshBR8fc55f32015-10-29 19:34:28 +0530164 if (!issuccess) {
165 log.debug("Port Chain identifier {} does not exist", id);
166 }
Jian Lic2a542b2016-05-10 11:48:19 -0700167 return Response.noContent().build();
SureshBR8fc55f32015-10-29 19:34:28 +0530168 }
169}