blob: ae227eddafa6a73b09321e50d1e7922354c76535 [file] [log] [blame]
DScano55a01032020-04-09 20:01:50 +02001/*
2 * Copyright 2020-present Open Networking Foundation
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 */
16package org.onosproject.vpls.rest;
17
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.net.intf.Interface;
22import org.onosproject.net.intf.InterfaceAdminService;
23import org.onosproject.rest.AbstractWebResource;
24import org.onosproject.vpls.api.Vpls;
25import org.onosproject.vpls.api.VplsData;
26
27import org.slf4j.Logger;
28
29import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
31import javax.ws.rs.GET;
32import javax.ws.rs.POST;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.Context;
37import javax.ws.rs.core.Response;
38import javax.ws.rs.core.UriInfo;
39import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.MediaType;
41import java.io.IOException;
42import java.io.InputStream;
43import java.util.Collection;
44import java.util.ArrayList;
45
46
47import static org.onlab.util.Tools.readTreeFromStream;
48import static org.onlab.util.Tools.nullIsNotFound;
49import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Query and program Vplss.
53 */
54@Path("vpls")
55public class VplsWebResource extends AbstractWebResource {
56 @Context
57 private UriInfo uriInfo;
58
59 private static final String VPLS_NOT_FOUND = "Vpls is not found for ";
60 private static final String VPLSS = "vplss";
61 private static final String VPLS = "vpls";
62 private static final String INTERFACES = "interfaces";
63
64 private final ObjectNode root = mapper().createObjectNode();
65 private final Logger log = getLogger(getClass());
66 /**
67 * Gets all Vplss. Returns array of all Vplss in the system.
68 *
69 * @return 200 OK with a collection of Vplss
70 * @onos.rsModel Vplss
71 */
72 @GET
73 @Produces(MediaType.APPLICATION_JSON)
74 public Response getVplss() {
75 ArrayNode vplssNode = root.putArray(VPLSS);
76 Vpls service = get(Vpls.class);
77 Collection<VplsData> vplsDatas = service.getAllVpls();
78 if (!vplsDatas.isEmpty()) {
79 for (VplsData entry : vplsDatas) {
80 vplssNode.add(codec(VplsData.class).encode(entry, this));
81 }
82 }
83
84 return ok(root).build();
85 }
86
87 /**
88 * Gets Vpls. Returns a Vpls by vplsName.
89 * @param vplsName vpls name
90 * @return 200 OK with a vpls, return 404 if no entry has been found
91 * @onos.rsModel Vpls
92 */
93 @GET
94 @Produces(MediaType.APPLICATION_JSON)
95 @Path("{vplsName}")
96 public Response getVpls(@PathParam("vplsName") String vplsName) {
97 ArrayNode vplsNode = root.putArray(VPLS);
98 Vpls service = get(Vpls.class);
99 final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
100 VPLS_NOT_FOUND + vplsName);
101 vplsNode.add(codec(VplsData.class).encode(vplsData, this));
102
103 return ok(root).build();
104 }
105 /**
106 * Creates new vpls. Creates and installs a new Vplps.<br>
107 *
108 * @param stream Vpls JSON
109 * @return status of the request - CREATED if the JSON is correct,
110 * BAD_REQUEST if the JSON is invalid
111 * @onos.rsModel VplsPost
112 */
113 @POST
114 @Consumes(MediaType.APPLICATION_JSON)
115 @Produces(MediaType.APPLICATION_JSON)
116 public Response createVpls(InputStream stream) {
117 Vpls service = get(Vpls.class);
118 InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
119 try {
120 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
121
122 VplsData vplsData = codec(VplsData.class).decode(jsonTree, this);
123 vplsData.interfaces().forEach(interf -> {
124 interfaceService.add(interf);
125 });
126 service.addInterfaces(vplsData, vplsData.interfaces());
127
128 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
129 .path(VPLS);
130 return Response
131 .created(locationBuilder.build())
132 .build();
133
134 } catch (IOException e) {
135 throw new IllegalArgumentException(e.getMessage());
136 }
137
138 }
139
140 /**
141 * Add new interfaces. Add new interfaces to a Vpls.<br>
142 *
143 * @param stream interfaces JSON
144 * @param vplsName Vpls name
145 * @return status of the request - CREATED if the JSON is correct,
146 * BAD_REQUEST if the JSON is invalid
147 * @onos.rsModel InterfacesPost
148 */
149 @POST
150 @Path("interfaces/{vplsName}")
151 @Consumes(MediaType.APPLICATION_JSON)
152 @Produces(MediaType.APPLICATION_JSON)
153 public Response addInterfaces(@PathParam("vplsName") String vplsName, InputStream stream) {
154 Vpls service = get(Vpls.class);
155 InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
156 final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
157 VPLS_NOT_FOUND + vplsName);
158 try {
159 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
160 Collection<Interface> interfaceList = new ArrayList<>();
161 jsonTree.forEach(interf -> {
162 Interface inter = codec(Interface.class).decode(jsonTree, this);
163 interfaceList.add(inter);
164 interfaceService.add(inter);
165 });
166 service.addInterfaces(vplsData, interfaceList);
167 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
168 .path(INTERFACES)
169 .path(vplsName);
170 return Response
171 .created(locationBuilder.build())
172 .build();
173
174 } catch (IOException e) {
175 throw new IllegalArgumentException(e.getMessage());
176 }
177
178 }
179
180 /**
181 * Removes the specified vpls.
182 *
183 * @param vplsName Vpls name
184 * @return 204 NO CONTENT
185 */
186 @DELETE
187 @Path("{vplsName}")
188 public Response deleteVpls(@PathParam("vplsName") String vplsName) {
189 Vpls service = get(Vpls.class);
190 final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
191 VPLS_NOT_FOUND + vplsName);
192 service.removeVpls(vplsData);
193 return Response.noContent().build();
194 }
195
196 /**
197 * Removes a specified interface.
198 *
199 * @param vplsName Vpls name
200 * @param interfaceName interface name
201 * @return 204 NO CONTENT
202 *
203 */
204 @DELETE
205 @Path("interface/{vplsName}/{interfaceName}")
206 public Response deleteInterface(@PathParam("vplsName") String vplsName,
207 @PathParam("interfaceName") String interfaceName) {
208 Vpls service = get(Vpls.class);
209 InterfaceAdminService interfaceService = get(InterfaceAdminService.class);
210 final VplsData vplsData = nullIsNotFound(service.getVpls(vplsName),
211 VPLS_NOT_FOUND + vplsName);
212 vplsData.interfaces().forEach(anInterface -> {
213 if (anInterface.name().equals(interfaceName)) {
214 interfaceService.remove(anInterface.connectPoint(), anInterface.name());
215 service.removeInterface(vplsData, anInterface);
216 }
217 });
218
219 return Response.noContent().build();
220 }
221
222}