blob: 808fcc16d50809320276fbca59706da6338e787c [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();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070052 service.getSubjectClasses().forEach(sc -> {
53 SubjectFactory subjectFactory = service.getSubjectFactory(sc);
54 produceJson(service, newObject(root, subjectFactory.subjectClassKey()),
55 subjectFactory, sc);
56 });
Thomas Vachuska96d55b12015-05-11 08:52:03 -070057 return ok(root).build();
58 }
59
60 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070061 * Get all network configuration for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070062 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070063 * @param subjectClassKey subject class key
Thomas Vachuska96d55b12015-05-11 08:52:03 -070064 * @return network configuration JSON
65 */
66 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070067 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -070068 @Produces(MediaType.APPLICATION_JSON)
69 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070070 public Response download(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -070071 NetworkConfigService service = get(NetworkConfigService.class);
72 ObjectNode root = mapper().createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070073 SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
74 produceJson(service, root, subjectFactory, subjectFactory.subjectClass());
Thomas Vachuska96d55b12015-05-11 08:52:03 -070075 return ok(root).build();
76 }
77
78 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070079 * Get all network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070080 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070081 * @param subjectClassKey subjectKey class key
82 * @param subjectKey subjectKey key
Thomas Vachuska96d55b12015-05-11 08:52:03 -070083 * @return network configuration JSON
84 */
85 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070086 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -070087 @Produces(MediaType.APPLICATION_JSON)
88 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070089 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
90 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091 NetworkConfigService service = get(NetworkConfigService.class);
92 ObjectNode root = mapper().createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070093 SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
94 produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -070095 return ok(root).build();
96 }
97
98 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070099 * Get specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700100 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700101 * @param subjectClassKey subjectKey class key
102 * @param subjectKey subjectKey key
103 * @param configKey configuration class key
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700104 * @return network configuration JSON
105 */
106 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700107 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700108 @Produces(MediaType.APPLICATION_JSON)
109 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700110 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
111 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700112 @PathParam("configKey") String configKey) {
113 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700114 return ok(service.getConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
115 service.getConfigClass(subjectClassKey, configKey)).node()).build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700116 }
117
118 @SuppressWarnings("unchecked")
119 private void produceJson(NetworkConfigService service, ObjectNode node,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700120 SubjectFactory subjectFactory, Class subjectClass) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700121 service.getSubjects(subjectClass).forEach(s ->
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700122 produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700123 }
124
125 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
126 Object subject) {
127 service.getConfigs(subject).forEach(c -> node.set(c.key(), c.node()));
128 }
129
130
131 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700132 * Upload bulk network configuration.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700133 *
134 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700135 * @return empty response
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700136 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700137 */
138 @POST
139 @Consumes(MediaType.APPLICATION_JSON)
140 @SuppressWarnings("unchecked")
141 public Response upload(InputStream request) throws IOException {
142 NetworkConfigService service = get(NetworkConfigService.class);
143 ObjectNode root = (ObjectNode) mapper().readTree(request);
144 root.fieldNames()
145 .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk),
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700146 service.getSubjectFactory(sk)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700147 return Response.ok().build();
148 }
149
150 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700151 * Upload multiple network configurations for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700152 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700153 * @param subjectClassKey subject class key
154 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700155 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700156 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700157 */
158 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700159 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700160 @Consumes(MediaType.APPLICATION_JSON)
161 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700162 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700163 InputStream request) throws IOException {
164 NetworkConfigService service = get(NetworkConfigService.class);
165 ObjectNode root = (ObjectNode) mapper().readTree(request);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700166 consumeJson(service, root, service.getSubjectFactory(subjectClassKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700167 return Response.ok().build();
168 }
169
170 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700171 * Upload mutliple network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700172 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700173 * @param subjectClassKey subjectKey class key
174 * @param subjectKey subjectKey key
175 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700176 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700177 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700178 */
179 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700180 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700181 @Consumes(MediaType.APPLICATION_JSON)
182 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700183 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
184 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700185 InputStream request) throws IOException {
186 NetworkConfigService service = get(NetworkConfigService.class);
187 ObjectNode root = (ObjectNode) mapper().readTree(request);
188 consumeSubjectJson(service, root,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700189 service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
190 subjectClassKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700191 return Response.ok().build();
192 }
193
194 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700195 * Upload specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700196 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700197 * @param subjectClassKey subjectKey class key
198 * @param subjectKey subjectKey key
199 * @param configKey configuration class key
200 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700201 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700202 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700203 */
204 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700205 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700206 @Consumes(MediaType.APPLICATION_JSON)
207 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700208 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
209 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700210 @PathParam("configKey") String configKey,
211 InputStream request) throws IOException {
212 NetworkConfigService service = get(NetworkConfigService.class);
213 ObjectNode root = (ObjectNode) mapper().readTree(request);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700214 service.applyConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
215 service.getConfigClass(subjectClassKey, configKey), root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700216 return Response.ok().build();
217 }
218
219 private void consumeJson(NetworkConfigService service, ObjectNode classNode,
220 SubjectFactory subjectFactory) {
221 classNode.fieldNames().forEachRemaining(s ->
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700222 consumeSubjectJson(service, (ObjectNode) classNode.path(s),
223 subjectFactory.createSubject(s),
224 subjectFactory.subjectClassKey()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700225 }
226
227 private void consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700228 ObjectNode subjectNode, Object subject,
229 String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700230 subjectNode.fieldNames().forEachRemaining(c ->
Jonathan Hart111b42b2015-07-14 13:28:05 -0700231 service.applyConfig(subject, service.getConfigClass(subjectKey, c),
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700232 subjectNode.path(c)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700233 }
234
235
236 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700237 * Clear entire network configuration base.
238 *
239 * @return empty response
240 */
241 @DELETE
242 @SuppressWarnings("unchecked")
243 public Response delete() {
244 NetworkConfigService service = get(NetworkConfigService.class);
245 service.getSubjectClasses()
246 .forEach(subjectClass -> service.getSubjects(subjectClass)
247 .forEach(subject -> service.getConfigs(subject)
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700248 .forEach(config -> service.removeConfig(subject, config.getClass()))));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700249 return Response.ok().build();
250 }
251
252 /**
253 * Clear all network configurations for a subject class.
254 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700255 * @param subjectClassKey subject class key
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700256 * @return empty response
257 */
258 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700259 @Path("{subjectClassKey}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700260 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700261 public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700262 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700263 service.getSubjects(service.getSubjectFactory(subjectClassKey).getClass())
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700264 .forEach(subject -> service.getConfigs(subject)
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700265 .forEach(config -> service.removeConfig(subject, config.getClass())));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700266 return Response.ok().build();
267 }
268
269 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700270 * Clear all network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700271 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700272 * @param subjectClassKey subjectKey class key
273 * @param subjectKey subjectKey key
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700274 * @return empty response
275 */
276 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700277 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700278 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700279 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
280 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700281 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700282 Object s = service.getSubjectFactory(subjectClassKey).createSubject(subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700283 service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
284 return Response.ok().build();
285 }
286
287 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700288 * Clear specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700289 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700290 * @param subjectClassKey subjectKey class key
291 * @param subjectKey subjectKey key
292 * @param configKey configuration class key
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700293 * @return empty response
294 */
295 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700296 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700297 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700298 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
299 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700300 @PathParam("configKey") String configKey) {
301 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700302 service.removeConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
303 service.getConfigClass(subjectClassKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700304 return Response.ok().build();
305 }
306
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700307}