blob: 4e2c790992048f4a62b9be86877194fa4732d7f2 [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}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700243 @SuppressWarnings("unchecked")
Sahil Lele372d1f32015-07-31 15:01:41 -0700244 public Response delete(@PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700245 @PathParam("subject") String subject) {
246 NetworkConfigService service = get(NetworkConfigService.class);
247 Object s = service.getSubjectFactory(subjectKey).createSubject(subject);
248 service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
249 return Response.ok().build();
250 }
251
252 /**
253 * Clears network configuration for the specified subject and given
254 * configuration class.
255 *
256 * @param subjectKey subject class key
257 * @param subject subject key
258 * @param configKey configuration class key
259 * @return empty response
260 */
261 @DELETE
262 @Path("{subjectKey}/{subject}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700263 @SuppressWarnings("unchecked")
Sahil Lele372d1f32015-07-31 15:01:41 -0700264 public Response delete(@PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700265 @PathParam("subject") String subject,
266 @PathParam("configKey") String configKey) {
267 NetworkConfigService service = get(NetworkConfigService.class);
268 service.removeConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Jonathan Hart111b42b2015-07-14 13:28:05 -0700269 service.getConfigClass(subjectKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700270 return Response.ok().build();
271 }
272
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700273
274 /**
275 * Clears all network configurations.
276 *
277 * @return empty response
278 */
279 @DELETE
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700280 @SuppressWarnings("unchecked")
Sahil Lele372d1f32015-07-31 15:01:41 -0700281 public Response delete() {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700282 NetworkConfigService service = get(NetworkConfigService.class);
283 service.getSubjectClasses().forEach(subjectClass -> {
284 service.getSubjects(subjectClass).forEach(subject -> {
285 service.getConfigs(subject).forEach(config -> {
286 service.removeConfig(subject, config.getClass());
287 });
288 });
289 });
290 return Response.ok().build();
291 }
292
293
294 // TODO: this one below doesn't work correctly
295 /**
296 * Clears network configuration for the specified subject class.
297 *
298 * @param subjectKey subject class key
299 * @return empty response
300 */
301 @DELETE
302 @Path("{subjectKey}/")
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700303 @SuppressWarnings("unchecked")
Sahil Lele372d1f32015-07-31 15:01:41 -0700304 public Response delete(@PathParam("subjectKey") String subjectKey) {
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700305 NetworkConfigService service = get(NetworkConfigService.class);
306 service.getSubjects(service.getSubjectFactory(subjectKey).getClass()).forEach(subject -> {
307 service.getConfigs(subject).forEach(config -> {
308 service.removeConfig(subject, config.getClass());
309 });
310 });
311 return Response.ok().build();
312 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700313}