blob: 2dc778bab9fcc0d122656302af9d18317be0add2 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
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 */
16package org.onosproject.rest.resources;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.incubator.net.config.NetworkConfigService;
20import org.onosproject.incubator.net.config.SubjectFactory;
21import org.onosproject.rest.AbstractWebResource;
22
23import javax.ws.rs.Consumes;
24import javax.ws.rs.DELETE;
25import javax.ws.rs.GET;
26import javax.ws.rs.POST;
27import javax.ws.rs.Path;
28import javax.ws.rs.PathParam;
29import javax.ws.rs.Produces;
30import javax.ws.rs.core.MediaType;
31import javax.ws.rs.core.Response;
32import java.io.IOException;
33import java.io.InputStream;
34
35/**
36 * REST resource for injecting and retrieving common network configuration.
37 */
38@Path("network/configuration")
39public class NetworkConfigWebResource extends AbstractWebResource {
40
41 /**
42 * Returns entire network configuration base.
43 *
44 * @return network configuration JSON
45 */
46 @GET
47 @Produces(MediaType.APPLICATION_JSON)
48 @SuppressWarnings("unchecked")
49 public Response download() {
50 NetworkConfigService service = get(NetworkConfigService.class);
51 ObjectNode root = mapper().createObjectNode();
52 service.getSubjectClasses().forEach(sc ->
53 produceJson(service, newObject(root, service.getSubjectFactory(sc).subjectKey()), sc));
54 return ok(root).build();
55 }
56
57 /**
58 * Returns the network configuration for the specified subject class.
59 *
60 * @param subjectKey subject class key
61 * @return network configuration JSON
62 */
63 @GET
64 @Path("{subjectKey}")
65 @Produces(MediaType.APPLICATION_JSON)
66 @SuppressWarnings("unchecked")
67 public Response download(@PathParam("subjectKey") String subjectKey) {
68 NetworkConfigService service = get(NetworkConfigService.class);
69 ObjectNode root = mapper().createObjectNode();
70 produceJson(service, root, service.getSubjectFactory(subjectKey).subjectClass());
71 return ok(root).build();
72 }
73
74 /**
75 * Returns the network configuration for the specified subject.
76 *
77 * @param subjectKey subject class key
78 * @param subject subject key
79 * @return network configuration JSON
80 */
81 @GET
82 @Path("{subjectKey}/{subject}")
83 @Produces(MediaType.APPLICATION_JSON)
84 @SuppressWarnings("unchecked")
85 public Response download(@PathParam("subjectKey") String subjectKey,
86 @PathParam("subject") String subject) {
87 NetworkConfigService service = get(NetworkConfigService.class);
88 ObjectNode root = mapper().createObjectNode();
89 produceSubjectJson(service, root,
Sahil Lele3a0cdd52015-07-21 14:16:31 -070090 service.getSubjectFactory(subjectKey).createSubject(subject));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091 return ok(root).build();
92 }
93
94 /**
95 * Returns the network configuration for the specified subject and given
96 * configuration class.
97 *
98 * @param subjectKey subject class key
99 * @param subject subject key
100 * @param configKey configuration class key
101 * @return network configuration JSON
102 */
103 @GET
104 @Path("{subjectKey}/{subject}/{configKey}")
105 @Produces(MediaType.APPLICATION_JSON)
106 @SuppressWarnings("unchecked")
107 public Response download(@PathParam("subjectKey") String subjectKey,
108 @PathParam("subject") String subject,
109 @PathParam("configKey") String configKey) {
110 NetworkConfigService service = get(NetworkConfigService.class);
111 return ok(service.getConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Jonathan Hart111b42b2015-07-14 13:28:05 -0700112 service.getConfigClass(subjectKey, configKey)).node()).build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700113 }
114
115 @SuppressWarnings("unchecked")
116 private void produceJson(NetworkConfigService service, ObjectNode node,
117 Class subjectClass) {
118 service.getSubjects(subjectClass).forEach(s ->
119 produceSubjectJson(service, newObject(node, s.toString()), s));
120 }
121
122 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
123 Object subject) {
124 service.getConfigs(subject).forEach(c -> node.set(c.key(), c.node()));
125 }
126
127
128 /**
129 * Uploads network configuration in bulk.
130 *
131 * @param request network configuration JSON rooted at the top node
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700132 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700133 * @return empty response
134 */
135 @POST
136 @Consumes(MediaType.APPLICATION_JSON)
137 @SuppressWarnings("unchecked")
138 public Response upload(InputStream request) throws IOException {
139 NetworkConfigService service = get(NetworkConfigService.class);
140 ObjectNode root = (ObjectNode) mapper().readTree(request);
141 root.fieldNames()
142 .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700143 service.getSubjectFactory(sk)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700144 return Response.ok().build();
145 }
146
147 /**
148 * Uploads network configuration for the specified subject class.
149 *
150 * @param subjectKey subject class key
151 * @param request network configuration JSON rooted at the top node
152 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700153 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700154 */
155 @POST
156 @Path("{subjectKey}")
157 @Consumes(MediaType.APPLICATION_JSON)
158 @SuppressWarnings("unchecked")
159 public Response upload(@PathParam("subjectKey") String subjectKey,
160 InputStream request) throws IOException {
161 NetworkConfigService service = get(NetworkConfigService.class);
162 ObjectNode root = (ObjectNode) mapper().readTree(request);
163 consumeJson(service, root, service.getSubjectFactory(subjectKey));
164 return Response.ok().build();
165 }
166
167 /**
168 * Uploads network configuration for the specified subject.
169 *
170 * @param subjectKey subject class key
171 * @param subject subject key
172 * @param request network configuration JSON rooted at the top node
173 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700174 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700175 */
176 @POST
177 @Path("{subjectKey}/{subject}")
178 @Consumes(MediaType.APPLICATION_JSON)
179 @SuppressWarnings("unchecked")
180 public Response upload(@PathParam("subjectKey") String subjectKey,
181 @PathParam("subject") String subject,
182 InputStream request) throws IOException {
183 NetworkConfigService service = get(NetworkConfigService.class);
184 ObjectNode root = (ObjectNode) mapper().readTree(request);
185 consumeSubjectJson(service, root,
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700186 service.getSubjectFactory(subjectKey).createSubject(subject),
187 subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700188 return Response.ok().build();
189 }
190
191 /**
192 * Uploads network configuration for the specified subject and given
193 * configuration class.
194 *
195 * @param subjectKey subject class key
196 * @param subject subject key
197 * @param configKey configuration class key
198 * @param request network configuration JSON rooted at the top node
199 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700200 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700201 */
202 @POST
203 @Path("{subjectKey}/{subject}/{configKey}")
204 @Consumes(MediaType.APPLICATION_JSON)
205 @SuppressWarnings("unchecked")
206 public Response upload(@PathParam("subjectKey") String subjectKey,
207 @PathParam("subject") String subject,
208 @PathParam("configKey") String configKey,
209 InputStream request) throws IOException {
210 NetworkConfigService service = get(NetworkConfigService.class);
211 ObjectNode root = (ObjectNode) mapper().readTree(request);
212 service.applyConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700213 service.getConfigClass(subjectKey, configKey), root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700214 return Response.ok().build();
215 }
216
217 private void consumeJson(NetworkConfigService service, ObjectNode classNode,
218 SubjectFactory subjectFactory) {
219 classNode.fieldNames().forEachRemaining(s ->
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700220 consumeSubjectJson(service, (ObjectNode) classNode.path(s),
221 subjectFactory.createSubject(s),
222 subjectFactory.subjectKey()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700223 }
224
225 private void consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700226 ObjectNode subjectNode, Object subject,
227 String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700228 subjectNode.fieldNames().forEachRemaining(c ->
Jonathan Hart111b42b2015-07-14 13:28:05 -0700229 service.applyConfig(subject, service.getConfigClass(subjectKey, c),
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700230 (ObjectNode) subjectNode.path(c)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700231 }
232
233
234 /**
235 * Clears network configuration for the specified subject.
236 *
237 * @param subjectKey subject class key
238 * @param subject subject key
239 * @return empty response
240 */
241 @DELETE
242 @Path("{subjectKey}/{subject}")
243 @Consumes(MediaType.APPLICATION_JSON)
244 @SuppressWarnings("unchecked")
245 public Response upload(@PathParam("subjectKey") String subjectKey,
246 @PathParam("subject") String subject) {
247 NetworkConfigService service = get(NetworkConfigService.class);
248 Object s = service.getSubjectFactory(subjectKey).createSubject(subject);
249 service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
250 return Response.ok().build();
251 }
252
253 /**
254 * Clears network configuration for the specified subject and given
255 * configuration class.
256 *
257 * @param subjectKey subject class key
258 * @param subject subject key
259 * @param configKey configuration class key
260 * @return empty response
261 */
262 @DELETE
263 @Path("{subjectKey}/{subject}/{configKey}")
264 @Consumes(MediaType.APPLICATION_JSON)
265 @SuppressWarnings("unchecked")
266 public Response upload(@PathParam("subjectKey") String subjectKey,
267 @PathParam("subject") String subject,
268 @PathParam("configKey") String configKey) {
269 NetworkConfigService service = get(NetworkConfigService.class);
270 service.removeConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Jonathan Hart111b42b2015-07-14 13:28:05 -0700271 service.getConfigClass(subjectKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700272 return Response.ok().build();
273 }
274
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700275
276 /**
277 * Clears all network configurations.
278 *
279 * @return empty response
280 */
281 @DELETE
282 @Consumes(MediaType.APPLICATION_JSON)
283 @SuppressWarnings("unchecked")
284 public Response upload() {
285 NetworkConfigService service = get(NetworkConfigService.class);
286 service.getSubjectClasses().forEach(subjectClass -> {
287 service.getSubjects(subjectClass).forEach(subject -> {
288 service.getConfigs(subject).forEach(config -> {
289 service.removeConfig(subject, config.getClass());
290 });
291 });
292 });
293 return Response.ok().build();
294 }
295
296
297 // TODO: this one below doesn't work correctly
298 /**
299 * Clears network configuration for the specified subject class.
300 *
301 * @param subjectKey subject class key
302 * @return empty response
303 */
304 @DELETE
305 @Path("{subjectKey}/")
306 @Consumes(MediaType.APPLICATION_JSON)
307 @SuppressWarnings("unchecked")
308 public Response upload(@PathParam("subjectKey") String subjectKey) {
309 NetworkConfigService service = get(NetworkConfigService.class);
310 service.getSubjects(service.getSubjectFactory(subjectKey).getClass()).forEach(subject -> {
311 service.getConfigs(subject).forEach(config -> {
312 service.removeConfig(subject, config.getClass());
313 });
314 });
315 return Response.ok().build();
316 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700317}