Committed by
Ray Milkey
[ONOS-3111] Web resource for PortPairGroup in SFC
Change-Id: Ieb91e3b456642e3d08f4bd651e9734278de2b773
Showing
1 changed file
with
163 additions
and
0 deletions
apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairGroupWebResource.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
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 | + | ||
17 | +package org.onosproject.vtnweb.resources; | ||
18 | + | ||
19 | +import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
20 | +import static javax.ws.rs.core.Response.Status.OK; | ||
21 | +import static org.onlab.util.Tools.nullIsNotFound; | ||
22 | + | ||
23 | +import java.io.IOException; | ||
24 | +import java.io.InputStream; | ||
25 | + | ||
26 | +import javax.ws.rs.Consumes; | ||
27 | +import javax.ws.rs.DELETE; | ||
28 | +import javax.ws.rs.GET; | ||
29 | +import javax.ws.rs.POST; | ||
30 | +import javax.ws.rs.PUT; | ||
31 | +import javax.ws.rs.Path; | ||
32 | +import javax.ws.rs.PathParam; | ||
33 | +import javax.ws.rs.Produces; | ||
34 | +import javax.ws.rs.core.MediaType; | ||
35 | +import javax.ws.rs.core.Response; | ||
36 | + | ||
37 | +import org.onosproject.rest.AbstractWebResource; | ||
38 | +import org.onosproject.vtnrsc.PortPairGroup; | ||
39 | +import org.onosproject.vtnrsc.PortPairGroupId; | ||
40 | +import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; | ||
41 | +import org.onosproject.vtnweb.web.PortPairGroupCodec; | ||
42 | +import org.slf4j.Logger; | ||
43 | +import org.slf4j.LoggerFactory; | ||
44 | + | ||
45 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
46 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
47 | + | ||
48 | +/** | ||
49 | + * Query and program port pair group. | ||
50 | + */ | ||
51 | + | ||
52 | +@Path("port_pair_groups") | ||
53 | +public class PortPairGroupWebResource extends AbstractWebResource { | ||
54 | + | ||
55 | + private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class); | ||
56 | + private final PortPairGroupService service = get(PortPairGroupService.class); | ||
57 | + 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) | ||
68 | + public Response getPortPairGroups() { | ||
69 | + Iterable<PortPairGroup> portPairGroups = service.getPortPairGroups(); | ||
70 | + ObjectNode result = new ObjectMapper().createObjectNode(); | ||
71 | + result.set("port_pair_groups", new PortPairGroupCodec().encode(portPairGroups, this)); | ||
72 | + return ok(result).build(); | ||
73 | + } | ||
74 | + | ||
75 | + /** | ||
76 | + * Get details of a specified port pair group id. | ||
77 | + * | ||
78 | + * @param id port pair group id | ||
79 | + * @return 200 OK, 404 if given identifier does not exist | ||
80 | + */ | ||
81 | + @GET | ||
82 | + @Path("{group_id}") | ||
83 | + @Produces(MediaType.APPLICATION_JSON) | ||
84 | + public Response getPortPairGroup(@PathParam("group_id") String id) { | ||
85 | + | ||
86 | + if (!service.exists(PortPairGroupId.portPairGroupId(id))) { | ||
87 | + return Response.status(NOT_FOUND) | ||
88 | + .entity(PORT_PAIR_GROUP_NOT_FOUND).build(); | ||
89 | + } | ||
90 | + PortPairGroup portPairGroup = nullIsNotFound(service.getPortPairGroup(PortPairGroupId.portPairGroupId(id)), | ||
91 | + PORT_PAIR_GROUP_NOT_FOUND); | ||
92 | + | ||
93 | + ObjectNode result = new ObjectMapper().createObjectNode(); | ||
94 | + result.set("port_pair_group", new PortPairGroupCodec().encode(portPairGroup, this)); | ||
95 | + return ok(result).build(); | ||
96 | + } | ||
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 { | ||
111 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
112 | + | ||
113 | + PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this); | ||
114 | + Boolean issuccess = nullIsNotFound(service.createPortPairGroup(portPairGroup), | ||
115 | + PORT_PAIR_GROUP_NOT_FOUND); | ||
116 | + return Response.status(OK).entity(issuccess.toString()).build(); | ||
117 | + } catch (IOException e) { | ||
118 | + log.error("Exception while creating port pair group {}.", e.toString()); | ||
119 | + throw new IllegalArgumentException(e); | ||
120 | + } | ||
121 | + } | ||
122 | + | ||
123 | + /** | ||
124 | + * Update details of a specified port pair group id. | ||
125 | + * | ||
126 | + * @param id port pair group id | ||
127 | + * @param stream port pair group from json | ||
128 | + * @return 200 OK, 404 if given identifier does not exist | ||
129 | + */ | ||
130 | + @PUT | ||
131 | + @Path("{group_id}") | ||
132 | + @Produces(MediaType.APPLICATION_JSON) | ||
133 | + @Consumes(MediaType.APPLICATION_JSON) | ||
134 | + public Response updatePortPairGroup(@PathParam("group_id") String id, | ||
135 | + final InputStream stream) { | ||
136 | + try { | ||
137 | + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
138 | + PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this); | ||
139 | + Boolean isSuccess = nullIsNotFound(service.updatePortPairGroup(portPairGroup), PORT_PAIR_GROUP_NOT_FOUND); | ||
140 | + return Response.status(OK).entity(isSuccess.toString()).build(); | ||
141 | + } catch (IOException e) { | ||
142 | + log.error("Update port pair group failed because of exception {}.", e.toString()); | ||
143 | + throw new IllegalArgumentException(e); | ||
144 | + } | ||
145 | + } | ||
146 | + | ||
147 | + /** | ||
148 | + * Delete details of a specified port pair group id. | ||
149 | + * | ||
150 | + * @param id port pair group id | ||
151 | + */ | ||
152 | + @Path("{group_id}") | ||
153 | + @DELETE | ||
154 | + public void deletePortPairGroup(@PathParam("group_id") String id) { | ||
155 | + log.debug("Deletes port pair group by identifier {}.", id); | ||
156 | + PortPairGroupId portPairGroupId = PortPairGroupId.portPairGroupId(id); | ||
157 | + Boolean issuccess = nullIsNotFound(service.removePortPairGroup(portPairGroupId), | ||
158 | + PORT_PAIR_GROUP_NOT_FOUND); | ||
159 | + if (!issuccess) { | ||
160 | + log.debug("Port pair group identifier {} does not exist", id); | ||
161 | + } | ||
162 | + } | ||
163 | +} |
-
Please register or login to post a comment