blob: 68fc6bd81708d19da0a6ddc3eecddf18c76d4ab7 [file] [log] [blame]
Phaneendra Manda6109f952015-10-29 16:59:30 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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)
67 public Response getPortPairGroups() {
Phaneendra Mandad44649e82015-12-01 21:11:57 +053068 Iterable<PortPairGroup> portPairGroups = get(PortPairGroupService.class).getPortPairGroups();
69 ObjectNode result = mapper().createObjectNode();
70 ArrayNode portPairGroupEntry = result.putArray("port_pair_groups");
71 if (portPairGroups != null) {
72 for (final PortPairGroup portPairGroup : portPairGroups) {
73 portPairGroupEntry.add(codec(PortPairGroup.class).encode(portPairGroup, this));
74 }
75 }
76 return ok(result.toString()).build();
Phaneendra Manda6109f952015-10-29 16:59:30 +053077 }
78
79 /**
80 * Get details of a specified port pair group id.
81 *
82 * @param id port pair group id
83 * @return 200 OK, 404 if given identifier does not exist
84 */
85 @GET
86 @Path("{group_id}")
87 @Produces(MediaType.APPLICATION_JSON)
88 public Response getPortPairGroup(@PathParam("group_id") String id) {
Phaneendra Mandad44649e82015-12-01 21:11:57 +053089 PortPairGroup portPairGroup = nullIsNotFound(get(PortPairGroupService.class)
90 .getPortPairGroup(PortPairGroupId.of(id)),
Phaneendra Manda6109f952015-10-29 16:59:30 +053091 PORT_PAIR_GROUP_NOT_FOUND);
92
Phaneendra Mandad44649e82015-12-01 21:11:57 +053093 ObjectNode result = mapper().createObjectNode();
94 result.set("port_pair_group", codec(PortPairGroup.class).encode(portPairGroup, this));
95 return ok(result.toString()).build();
Phaneendra Manda6109f952015-10-29 16:59:30 +053096 }
97
98 /**
99 * Creates a new port pair group.
100 *
101 * @param stream port pair group 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 createPortPairGroup(InputStream stream) {
109
110 try {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530111 ObjectMapper mapper = new ObjectMapper();
112 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
113 JsonNode port = jsonTree.get("port_pair_group");
Phaneendra Manda6109f952015-10-29 16:59:30 +0530114
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530115 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this);
116 Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).createPortPairGroup(portPairGroup),
Phaneendra Manda6109f952015-10-29 16:59:30 +0530117 PORT_PAIR_GROUP_NOT_FOUND);
118 return Response.status(OK).entity(issuccess.toString()).build();
119 } catch (IOException e) {
120 log.error("Exception while creating port pair group {}.", e.toString());
121 throw new IllegalArgumentException(e);
122 }
123 }
124
125 /**
126 * Update details of a specified port pair group id.
127 *
128 * @param id port pair group id
129 * @param stream port pair group from json
130 * @return 200 OK, 404 if given identifier does not exist
131 */
132 @PUT
133 @Path("{group_id}")
134 @Produces(MediaType.APPLICATION_JSON)
135 @Consumes(MediaType.APPLICATION_JSON)
136 public Response updatePortPairGroup(@PathParam("group_id") String id,
137 final InputStream stream) {
138 try {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530139 ObjectMapper mapper = new ObjectMapper();
140 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
141 JsonNode port = jsonTree.get("port_pair_group");
142 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this);
143 Boolean isSuccess = nullIsNotFound(get(PortPairGroupService.class).updatePortPairGroup(portPairGroup),
144 PORT_PAIR_GROUP_NOT_FOUND);
Phaneendra Manda6109f952015-10-29 16:59:30 +0530145 return Response.status(OK).entity(isSuccess.toString()).build();
146 } catch (IOException e) {
147 log.error("Update port pair group failed because of exception {}.", e.toString());
148 throw new IllegalArgumentException(e);
149 }
150 }
151
152 /**
153 * Delete details of a specified port pair group id.
154 *
155 * @param id port pair group id
Jian Lic2a542b2016-05-10 11:48:19 -0700156 * @return 204 NO CONTENT
Phaneendra Manda6109f952015-10-29 16:59:30 +0530157 */
158 @Path("{group_id}")
159 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700160 public Response deletePortPairGroup(@PathParam("group_id") String id) {
Phaneendra Manda6109f952015-10-29 16:59:30 +0530161 log.debug("Deletes port pair group by identifier {}.", id);
Mahesh Poojary Huaweif5f1edd2015-11-05 11:28:01 +0530162 PortPairGroupId portPairGroupId = PortPairGroupId.of(id);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530163 Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).removePortPairGroup(portPairGroupId),
Phaneendra Manda6109f952015-10-29 16:59:30 +0530164 PORT_PAIR_GROUP_NOT_FOUND);
165 if (!issuccess) {
166 log.debug("Port pair group identifier {} does not exist", id);
167 }
Jian Lic2a542b2016-05-10 11:48:19 -0700168
169 return Response.noContent().build();
Phaneendra Manda6109f952015-10-29 16:59:30 +0530170 }
171}