blob: 91793ca77f85a6c36d23e10150b03b84aba04d74 [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;
Ray Milkey86ee5e82018-04-02 15:33:07 -070021import static org.onlab.util.Tools.readTreeFromStream;
Phaneendra Manda6109f952015-10-29 16:59:30 +053022
23import java.io.IOException;
24import java.io.InputStream;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.PUT;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36
37import org.onosproject.rest.AbstractWebResource;
38import org.onosproject.vtnrsc.PortPairGroup;
39import org.onosproject.vtnrsc.PortPairGroupId;
40import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
Phaneendra Manda6109f952015-10-29 16:59:30 +053041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Phaneendra Mandad44649e82015-12-01 21:11:57 +053044import com.fasterxml.jackson.databind.JsonNode;
Phaneendra Manda6109f952015-10-29 16:59:30 +053045import com.fasterxml.jackson.databind.ObjectMapper;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053046import com.fasterxml.jackson.databind.node.ArrayNode;
Phaneendra Manda6109f952015-10-29 16:59:30 +053047import com.fasterxml.jackson.databind.node.ObjectNode;
48
49/**
50 * Query and program port pair group.
51 */
52
53@Path("port_pair_groups")
54public class PortPairGroupWebResource extends AbstractWebResource {
55
56 private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class);
Phaneendra Manda6109f952015-10-29 16:59:30 +053057 public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found";
58 public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists";
59 public static final String PORT_PAIR_GROUP_ID_NOT_EXIST = "Port pair group does not exist with identifier";
60
61 /**
62 * Get details of all port pair groups created.
63 *
64 * @return 200 OK
65 */
66 @GET
67 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080068 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda6109f952015-10-29 16:59:30 +053069 public Response getPortPairGroups() {
Phaneendra Mandad44649e82015-12-01 21:11:57 +053070 Iterable<PortPairGroup> portPairGroups = get(PortPairGroupService.class).getPortPairGroups();
71 ObjectNode result = mapper().createObjectNode();
72 ArrayNode portPairGroupEntry = result.putArray("port_pair_groups");
73 if (portPairGroups != null) {
74 for (final PortPairGroup portPairGroup : portPairGroups) {
75 portPairGroupEntry.add(codec(PortPairGroup.class).encode(portPairGroup, this));
76 }
77 }
78 return ok(result.toString()).build();
Phaneendra Manda6109f952015-10-29 16:59:30 +053079 }
80
81 /**
82 * Get details of a specified port pair group id.
83 *
84 * @param id port pair group id
85 * @return 200 OK, 404 if given identifier does not exist
86 */
87 @GET
88 @Path("{group_id}")
89 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080090 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda6109f952015-10-29 16:59:30 +053091 public Response getPortPairGroup(@PathParam("group_id") String id) {
Phaneendra Mandad44649e82015-12-01 21:11:57 +053092 PortPairGroup portPairGroup = nullIsNotFound(get(PortPairGroupService.class)
93 .getPortPairGroup(PortPairGroupId.of(id)),
Phaneendra Manda6109f952015-10-29 16:59:30 +053094 PORT_PAIR_GROUP_NOT_FOUND);
95
Phaneendra Mandad44649e82015-12-01 21:11:57 +053096 ObjectNode result = mapper().createObjectNode();
97 result.set("port_pair_group", codec(PortPairGroup.class).encode(portPairGroup, this));
98 return ok(result.toString()).build();
Phaneendra Manda6109f952015-10-29 16:59:30 +053099 }
100
101 /**
102 * Creates a new port pair group.
103 *
104 * @param stream port pair group from JSON
105 * @return status of the request - CREATED if the JSON is correct,
106 * BAD_REQUEST if the JSON is invalid
107 */
108 @POST
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response createPortPairGroup(InputStream stream) {
112
113 try {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530114 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700115 ObjectNode jsonTree = readTreeFromStream(mapper, stream);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530116 JsonNode port = jsonTree.get("port_pair_group");
Phaneendra Manda6109f952015-10-29 16:59:30 +0530117
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530118 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this);
119 Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).createPortPairGroup(portPairGroup),
Phaneendra Manda6109f952015-10-29 16:59:30 +0530120 PORT_PAIR_GROUP_NOT_FOUND);
121 return Response.status(OK).entity(issuccess.toString()).build();
122 } catch (IOException e) {
123 log.error("Exception while creating port pair group {}.", e.toString());
124 throw new IllegalArgumentException(e);
125 }
126 }
127
128 /**
129 * Update details of a specified port pair group id.
130 *
131 * @param id port pair group id
132 * @param stream port pair group from json
133 * @return 200 OK, 404 if given identifier does not exist
134 */
135 @PUT
136 @Path("{group_id}")
137 @Produces(MediaType.APPLICATION_JSON)
138 @Consumes(MediaType.APPLICATION_JSON)
139 public Response updatePortPairGroup(@PathParam("group_id") String id,
140 final InputStream stream) {
141 try {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530142 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700143 ObjectNode jsonTree = readTreeFromStream(mapper, stream);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530144 JsonNode port = jsonTree.get("port_pair_group");
145 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this);
146 Boolean isSuccess = nullIsNotFound(get(PortPairGroupService.class).updatePortPairGroup(portPairGroup),
147 PORT_PAIR_GROUP_NOT_FOUND);
Phaneendra Manda6109f952015-10-29 16:59:30 +0530148 return Response.status(OK).entity(isSuccess.toString()).build();
149 } catch (IOException e) {
150 log.error("Update port pair group failed because of exception {}.", e.toString());
151 throw new IllegalArgumentException(e);
152 }
153 }
154
155 /**
156 * Delete details of a specified port pair group id.
157 *
158 * @param id port pair group id
Jian Lic2a542b2016-05-10 11:48:19 -0700159 * @return 204 NO CONTENT
Phaneendra Manda6109f952015-10-29 16:59:30 +0530160 */
161 @Path("{group_id}")
162 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800163 @Produces(MediaType.APPLICATION_JSON)
164 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700165 public Response deletePortPairGroup(@PathParam("group_id") String id) {
Phaneendra Manda6109f952015-10-29 16:59:30 +0530166 log.debug("Deletes port pair group by identifier {}.", id);
Mahesh Poojary Huaweif5f1edd2015-11-05 11:28:01 +0530167 PortPairGroupId portPairGroupId = PortPairGroupId.of(id);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530168 Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).removePortPairGroup(portPairGroupId),
Phaneendra Manda6109f952015-10-29 16:59:30 +0530169 PORT_PAIR_GROUP_NOT_FOUND);
170 if (!issuccess) {
171 log.debug("Port pair group identifier {} does not exist", id);
172 }
Jian Lic2a542b2016-05-10 11:48:19 -0700173
174 return Response.noContent().build();
Phaneendra Manda6109f952015-10-29 16:59:30 +0530175 }
176}