blob: 9e2b6273fa0d018a3e589d1870548dcefb3ccd69 [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;
Ray Milkeya4122362015-08-18 15:19:08 -070019import org.onosproject.net.config.NetworkConfigService;
20import org.onosproject.net.config.SubjectFactory;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070021import 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/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070036 * Manage network configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070037 */
38@Path("network/configuration")
39public class NetworkConfigWebResource extends AbstractWebResource {
40
41 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070042 * Get entire network configuration base.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070043 *
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 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 * Get all network configuration for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070059 *
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 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070075 * Get all network configuration for a subject.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070076 *
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,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070090 service.getSubjectFactory(subjectKey).createSubject(subject));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091 return ok(root).build();
92 }
93
94 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070095 * Get specific network configuration for a subject.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070096 *
97 * @param subjectKey subject class key
98 * @param subject subject key
99 * @param configKey configuration class key
100 * @return network configuration JSON
101 */
102 @GET
103 @Path("{subjectKey}/{subject}/{configKey}")
104 @Produces(MediaType.APPLICATION_JSON)
105 @SuppressWarnings("unchecked")
106 public Response download(@PathParam("subjectKey") String subjectKey,
107 @PathParam("subject") String subject,
108 @PathParam("configKey") String configKey) {
109 NetworkConfigService service = get(NetworkConfigService.class);
110 return ok(service.getConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Jonathan Hart111b42b2015-07-14 13:28:05 -0700111 service.getConfigClass(subjectKey, configKey)).node()).build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700112 }
113
114 @SuppressWarnings("unchecked")
115 private void produceJson(NetworkConfigService service, ObjectNode node,
116 Class subjectClass) {
117 service.getSubjects(subjectClass).forEach(s ->
118 produceSubjectJson(service, newObject(node, s.toString()), s));
119 }
120
121 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
122 Object subject) {
123 service.getConfigs(subject).forEach(c -> node.set(c.key(), c.node()));
124 }
125
126
127 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700128 * Upload bulk network configuration.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700129 *
130 * @param request network configuration JSON rooted at the top node
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700131 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700132 * @return empty response
133 */
134 @POST
135 @Consumes(MediaType.APPLICATION_JSON)
136 @SuppressWarnings("unchecked")
137 public Response upload(InputStream request) throws IOException {
138 NetworkConfigService service = get(NetworkConfigService.class);
139 ObjectNode root = (ObjectNode) mapper().readTree(request);
140 root.fieldNames()
141 .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk),
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700142 service.getSubjectFactory(sk)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700143 return Response.ok().build();
144 }
145
146 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700147 * Upload multiple network configurations for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700148 *
149 * @param subjectKey subject class key
150 * @param request network configuration JSON rooted at the top node
151 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700152 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700153 */
154 @POST
155 @Path("{subjectKey}")
156 @Consumes(MediaType.APPLICATION_JSON)
157 @SuppressWarnings("unchecked")
158 public Response upload(@PathParam("subjectKey") String subjectKey,
159 InputStream request) throws IOException {
160 NetworkConfigService service = get(NetworkConfigService.class);
161 ObjectNode root = (ObjectNode) mapper().readTree(request);
162 consumeJson(service, root, service.getSubjectFactory(subjectKey));
163 return Response.ok().build();
164 }
165
166 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700167 * Upload mutliple network configurations for a subject.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700168 *
169 * @param subjectKey subject class key
170 * @param subject subject key
171 * @param request network configuration JSON rooted at the top node
172 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700173 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700174 */
175 @POST
176 @Path("{subjectKey}/{subject}")
177 @Consumes(MediaType.APPLICATION_JSON)
178 @SuppressWarnings("unchecked")
179 public Response upload(@PathParam("subjectKey") String subjectKey,
180 @PathParam("subject") String subject,
181 InputStream request) throws IOException {
182 NetworkConfigService service = get(NetworkConfigService.class);
183 ObjectNode root = (ObjectNode) mapper().readTree(request);
184 consumeSubjectJson(service, root,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700185 service.getSubjectFactory(subjectKey).createSubject(subject),
186 subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700187 return Response.ok().build();
188 }
189
190 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700191 * Upload specific network configuration for a subject.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700192 *
193 * @param subjectKey subject class key
194 * @param subject subject key
195 * @param configKey configuration class key
196 * @param request network configuration JSON rooted at the top node
197 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700198 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700199 */
200 @POST
201 @Path("{subjectKey}/{subject}/{configKey}")
202 @Consumes(MediaType.APPLICATION_JSON)
203 @SuppressWarnings("unchecked")
204 public Response upload(@PathParam("subjectKey") String subjectKey,
205 @PathParam("subject") String subject,
206 @PathParam("configKey") String configKey,
207 InputStream request) throws IOException {
208 NetworkConfigService service = get(NetworkConfigService.class);
209 ObjectNode root = (ObjectNode) mapper().readTree(request);
210 service.applyConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700211 service.getConfigClass(subjectKey, configKey), root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700212 return Response.ok().build();
213 }
214
215 private void consumeJson(NetworkConfigService service, ObjectNode classNode,
216 SubjectFactory subjectFactory) {
217 classNode.fieldNames().forEachRemaining(s ->
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700218 consumeSubjectJson(service, (ObjectNode) classNode.path(s),
219 subjectFactory.createSubject(s),
220 subjectFactory.subjectKey()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700221 }
222
223 private void consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700224 ObjectNode subjectNode, Object subject,
225 String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700226 subjectNode.fieldNames().forEachRemaining(c ->
Jonathan Hart111b42b2015-07-14 13:28:05 -0700227 service.applyConfig(subject, service.getConfigClass(subjectKey, c),
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700228 (ObjectNode) subjectNode.path(c)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700229 }
230
231
232 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700233 * Clear entire network configuration base.
234 *
235 * @return empty response
236 */
237 @DELETE
238 @SuppressWarnings("unchecked")
239 public Response delete() {
240 NetworkConfigService service = get(NetworkConfigService.class);
241 service.getSubjectClasses()
242 .forEach(subjectClass -> service.getSubjects(subjectClass)
243 .forEach(subject -> service.getConfigs(subject)
244 .forEach(config -> service
245 .removeConfig(subject, config.getClass()))));
246 return Response.ok().build();
247 }
248
249 /**
250 * Clear all network configurations for a subject class.
251 *
252 * @param subjectKey subject class key
253 * @return empty response
254 */
255 @DELETE
256 @Path("{subjectKey}")
257 @SuppressWarnings("unchecked")
258 public Response delete(@PathParam("subjectKey") String subjectKey) {
259 NetworkConfigService service = get(NetworkConfigService.class);
260 service.getSubjects(service.getSubjectFactory(subjectKey).getClass())
261 .forEach(subject -> service.getConfigs(subject)
262 .forEach(config -> service
263 .removeConfig(subject, config.getClass())));
264 return Response.ok().build();
265 }
266
267 /**
268 * Clear all network configurations for a subject.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700269 *
270 * @param subjectKey subject class key
271 * @param subject subject key
272 * @return empty response
273 */
274 @DELETE
275 @Path("{subjectKey}/{subject}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700276 @SuppressWarnings("unchecked")
Sahil Lele372d1f32015-07-31 15:01:41 -0700277 public Response delete(@PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700278 @PathParam("subject") String subject) {
279 NetworkConfigService service = get(NetworkConfigService.class);
280 Object s = service.getSubjectFactory(subjectKey).createSubject(subject);
281 service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
282 return Response.ok().build();
283 }
284
285 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700286 * Clear specific network configuration for a subject.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700287 *
288 * @param subjectKey subject class key
289 * @param subject subject key
290 * @param configKey configuration class key
291 * @return empty response
292 */
293 @DELETE
294 @Path("{subjectKey}/{subject}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700295 @SuppressWarnings("unchecked")
Sahil Lele372d1f32015-07-31 15:01:41 -0700296 public Response delete(@PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700297 @PathParam("subject") String subject,
298 @PathParam("configKey") String configKey) {
299 NetworkConfigService service = get(NetworkConfigService.class);
300 service.removeConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700301 service.getConfigClass(subjectKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700302 return Response.ok().build();
303 }
304
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700305}