blob: 42b95637cd1e8f09580c010bf1847f726713607c [file] [log] [blame]
Phaneendra Manda6109f952015-10-29 16:59:30 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Manda6109f952015-10-29 16:59:30 +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 */
16
17package org.onosproject.vtnweb.resources;
18
Phaneendra Manda6109f952015-10-29 16:59:30 +053019import static javax.ws.rs.core.Response.Status.OK;
20import static org.onlab.util.Tools.nullIsNotFound;
21
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.PortPairGroup;
38import org.onosproject.vtnrsc.PortPairGroupId;
39import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
Phaneendra Manda6109f952015-10-29 16:59:30 +053040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
Phaneendra Mandad44649e82015-12-01 21:11:57 +053043import com.fasterxml.jackson.databind.JsonNode;
Phaneendra Manda6109f952015-10-29 16:59:30 +053044import com.fasterxml.jackson.databind.ObjectMapper;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053045import com.fasterxml.jackson.databind.node.ArrayNode;
Phaneendra Manda6109f952015-10-29 16:59:30 +053046import com.fasterxml.jackson.databind.node.ObjectNode;
47
48/**
49 * Query and program port pair group.
50 */
51
52@Path("port_pair_groups")
53public class PortPairGroupWebResource extends AbstractWebResource {
54
55 private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class);
Phaneendra Manda6109f952015-10-29 16:59:30 +053056 public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found";
57 public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists";
58 public static final String PORT_PAIR_GROUP_ID_NOT_EXIST = "Port pair group does not exist with identifier";
59
60 /**
61 * Get details of all port pair groups created.
62 *
63 * @return 200 OK
64 */
65 @GET
66 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080067 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda6109f952015-10-29 16:59:30 +053068 public Response getPortPairGroups() {
Phaneendra Mandad44649e82015-12-01 21:11:57 +053069 Iterable<PortPairGroup> portPairGroups = get(PortPairGroupService.class).getPortPairGroups();
70 ObjectNode result = mapper().createObjectNode();
71 ArrayNode portPairGroupEntry = result.putArray("port_pair_groups");
72 if (portPairGroups != null) {
73 for (final PortPairGroup portPairGroup : portPairGroups) {
74 portPairGroupEntry.add(codec(PortPairGroup.class).encode(portPairGroup, this));
75 }
76 }
77 return ok(result.toString()).build();
Phaneendra Manda6109f952015-10-29 16:59:30 +053078 }
79
80 /**
81 * Get details of a specified port pair group id.
82 *
83 * @param id port pair group id
84 * @return 200 OK, 404 if given identifier does not exist
85 */
86 @GET
87 @Path("{group_id}")
88 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080089 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda6109f952015-10-29 16:59:30 +053090 public Response getPortPairGroup(@PathParam("group_id") String id) {
Phaneendra Mandad44649e82015-12-01 21:11:57 +053091 PortPairGroup portPairGroup = nullIsNotFound(get(PortPairGroupService.class)
92 .getPortPairGroup(PortPairGroupId.of(id)),
Phaneendra Manda6109f952015-10-29 16:59:30 +053093 PORT_PAIR_GROUP_NOT_FOUND);
94
Phaneendra Mandad44649e82015-12-01 21:11:57 +053095 ObjectNode result = mapper().createObjectNode();
96 result.set("port_pair_group", codec(PortPairGroup.class).encode(portPairGroup, this));
97 return ok(result.toString()).build();
Phaneendra Manda6109f952015-10-29 16:59:30 +053098 }
99
100 /**
101 * Creates a new port pair group.
102 *
103 * @param stream port pair group from JSON
104 * @return status of the request - CREATED if the JSON is correct,
105 * BAD_REQUEST if the JSON is invalid
106 */
107 @POST
108 @Consumes(MediaType.APPLICATION_JSON)
109 @Produces(MediaType.APPLICATION_JSON)
110 public Response createPortPairGroup(InputStream stream) {
111
112 try {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530113 ObjectMapper mapper = new ObjectMapper();
114 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
115 JsonNode port = jsonTree.get("port_pair_group");
Phaneendra Manda6109f952015-10-29 16:59:30 +0530116
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530117 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this);
118 Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).createPortPairGroup(portPairGroup),
Phaneendra Manda6109f952015-10-29 16:59:30 +0530119 PORT_PAIR_GROUP_NOT_FOUND);
120 return Response.status(OK).entity(issuccess.toString()).build();
121 } catch (IOException e) {
122 log.error("Exception while creating port pair group {}.", e.toString());
123 throw new IllegalArgumentException(e);
124 }
125 }
126
127 /**
128 * Update details of a specified port pair group id.
129 *
130 * @param id port pair group id
131 * @param stream port pair group from json
132 * @return 200 OK, 404 if given identifier does not exist
133 */
134 @PUT
135 @Path("{group_id}")
136 @Produces(MediaType.APPLICATION_JSON)
137 @Consumes(MediaType.APPLICATION_JSON)
138 public Response updatePortPairGroup(@PathParam("group_id") String id,
139 final InputStream stream) {
140 try {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530141 ObjectMapper mapper = new ObjectMapper();
142 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
143 JsonNode port = jsonTree.get("port_pair_group");
144 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this);
145 Boolean isSuccess = nullIsNotFound(get(PortPairGroupService.class).updatePortPairGroup(portPairGroup),
146 PORT_PAIR_GROUP_NOT_FOUND);
Phaneendra Manda6109f952015-10-29 16:59:30 +0530147 return Response.status(OK).entity(isSuccess.toString()).build();
148 } catch (IOException e) {
149 log.error("Update port pair group failed because of exception {}.", e.toString());
150 throw new IllegalArgumentException(e);
151 }
152 }
153
154 /**
155 * Delete details of a specified port pair group id.
156 *
157 * @param id port pair group id
Jian Lic2a542b2016-05-10 11:48:19 -0700158 * @return 204 NO CONTENT
Phaneendra Manda6109f952015-10-29 16:59:30 +0530159 */
160 @Path("{group_id}")
161 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800162 @Produces(MediaType.APPLICATION_JSON)
163 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700164 public Response deletePortPairGroup(@PathParam("group_id") String id) {
Phaneendra Manda6109f952015-10-29 16:59:30 +0530165 log.debug("Deletes port pair group by identifier {}.", id);
Mahesh Poojary Huaweif5f1edd2015-11-05 11:28:01 +0530166 PortPairGroupId portPairGroupId = PortPairGroupId.of(id);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530167 Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).removePortPairGroup(portPairGroupId),
Phaneendra Manda6109f952015-10-29 16:59:30 +0530168 PORT_PAIR_GROUP_NOT_FOUND);
169 if (!issuccess) {
170 log.debug("Port pair group identifier {} does not exist", id);
171 }
Jian Lic2a542b2016-05-10 11:48:19 -0700172
173 return Response.noContent().build();
Phaneendra Manda6109f952015-10-29 16:59:30 +0530174 }
175}