blob: 0a6f6e7cfa240db339fe2247d6bb3152423b088e [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;
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)
Wu wenbind0b119f2016-05-11 18:03:41 +080065 @Consumes(MediaType.APPLICATION_JSON)
SureshBR8fc55f32015-10-29 19:34:28 +053066 public Response getPortChains() {
SureshBR47a52922015-12-01 20:51:10 +053067 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();
SureshBR8fc55f32015-10-29 19:34:28 +053076 }
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 wenbind0b119f2016-05-11 18:03:41 +080087 @Consumes(MediaType.APPLICATION_JSON)
SureshBR8fc55f32015-10-29 19:34:28 +053088 public Response getPortPain(@PathParam("chain_id") String id) {
89
SureshBR47a52922015-12-01 20:51:10 +053090 PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)),
SureshBR8fc55f32015-10-29 19:34:28 +053091 PORT_CHAIN_NOT_FOUND);
SureshBR47a52922015-12-01 20:51:10 +053092 ObjectNode result = mapper().createObjectNode();
93 result.set("port_chain", codec(PortChain.class).encode(portChain, this));
94 return ok(result.toString()).build();
SureshBR8fc55f32015-10-29 19:34:28 +053095 }
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);
SureshBR47a52922015-12-01 20:51:10 +0530110 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);
SureshBR8fc55f32015-10-29 19:34:28 +0530114 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);
SureshBR47a52922015-12-01 20:51:10 +0530136 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);
SureshBR8fc55f32015-10-29 19:34:28 +0530140 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 Lic2a542b2016-05-10 11:48:19 -0700151 * @return 204 NO CONTENT
SureshBR8fc55f32015-10-29 19:34:28 +0530152 */
153 @Path("{chain_id}")
154 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800155 @Produces(MediaType.APPLICATION_JSON)
156 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700157 public Response deletePortPain(@PathParam("chain_id") String id) {
SureshBR8fc55f32015-10-29 19:34:28 +0530158 log.debug("Deletes port chain by identifier {}.", id);
Mahesh Poojary Huaweia9feb6d2015-11-05 13:24:16 +0530159 PortChainId portChainId = PortChainId.of(id);
SureshBR8fc55f32015-10-29 19:34:28 +0530160
SureshBR47a52922015-12-01 20:51:10 +0530161 Boolean issuccess = nullIsNotFound(get(PortChainService.class).removePortChain(portChainId),
162 PORT_CHAIN_NOT_FOUND);
SureshBR8fc55f32015-10-29 19:34:28 +0530163 if (!issuccess) {
164 log.debug("Port Chain identifier {} does not exist", id);
165 }
Jian Lic2a542b2016-05-10 11:48:19 -0700166 return Response.noContent().build();
SureshBR8fc55f32015-10-29 19:34:28 +0530167 }
168}