blob: 94a6f96dce33b4c35932b3ed60a8eaf9c17500ff [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.
andrea5056b512015-11-18 10:25:28 -080043 * @rsModel NetCfgGet
Thomas Vachuska96d55b12015-05-11 08:52:03 -070044 * @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 *
andrea5056b512015-11-18 10:25:28 -0800134 * @rsModel NetCfgGet
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700135 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700136 * @return empty response
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700137 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700138 */
139 @POST
140 @Consumes(MediaType.APPLICATION_JSON)
141 @SuppressWarnings("unchecked")
142 public Response upload(InputStream request) throws IOException {
143 NetworkConfigService service = get(NetworkConfigService.class);
144 ObjectNode root = (ObjectNode) mapper().readTree(request);
145 root.fieldNames()
146 .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk),
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700147 service.getSubjectFactory(sk)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700148 return Response.ok().build();
149 }
150
151 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700152 * Upload multiple network configurations for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700153 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700154 * @param subjectClassKey subject class key
155 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700156 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700157 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700158 */
159 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700160 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700161 @Consumes(MediaType.APPLICATION_JSON)
162 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700163 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700164 InputStream request) throws IOException {
165 NetworkConfigService service = get(NetworkConfigService.class);
166 ObjectNode root = (ObjectNode) mapper().readTree(request);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700167 consumeJson(service, root, service.getSubjectFactory(subjectClassKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700168 return Response.ok().build();
169 }
170
171 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700172 * Upload mutliple network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700173 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700174 * @param subjectClassKey subjectKey class key
175 * @param subjectKey subjectKey key
176 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700177 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700178 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700179 */
180 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700181 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700182 @Consumes(MediaType.APPLICATION_JSON)
183 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700184 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
185 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700186 InputStream request) throws IOException {
187 NetworkConfigService service = get(NetworkConfigService.class);
188 ObjectNode root = (ObjectNode) mapper().readTree(request);
189 consumeSubjectJson(service, root,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700190 service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
191 subjectClassKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700192 return Response.ok().build();
193 }
194
195 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700196 * Upload specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700197 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700198 * @param subjectClassKey subjectKey class key
199 * @param subjectKey subjectKey key
200 * @param configKey configuration class key
201 * @param request network configuration JSON rooted at the top node
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700202 * @return empty response
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700203 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700204 */
205 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700206 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700207 @Consumes(MediaType.APPLICATION_JSON)
208 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700209 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
210 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700211 @PathParam("configKey") String configKey,
212 InputStream request) throws IOException {
213 NetworkConfigService service = get(NetworkConfigService.class);
214 ObjectNode root = (ObjectNode) mapper().readTree(request);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700215 service.applyConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
216 service.getConfigClass(subjectClassKey, configKey), root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700217 return Response.ok().build();
218 }
219
220 private void consumeJson(NetworkConfigService service, ObjectNode classNode,
221 SubjectFactory subjectFactory) {
222 classNode.fieldNames().forEachRemaining(s ->
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700223 consumeSubjectJson(service, (ObjectNode) classNode.path(s),
224 subjectFactory.createSubject(s),
225 subjectFactory.subjectClassKey()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700226 }
227
228 private void consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700229 ObjectNode subjectNode, Object subject,
230 String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700231 subjectNode.fieldNames().forEachRemaining(c ->
Jonathan Hart111b42b2015-07-14 13:28:05 -0700232 service.applyConfig(subject, service.getConfigClass(subjectKey, c),
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700233 subjectNode.path(c)));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700234 }
235
236
237 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700238 * Clear entire network configuration base.
239 *
240 * @return empty response
241 */
242 @DELETE
243 @SuppressWarnings("unchecked")
244 public Response delete() {
245 NetworkConfigService service = get(NetworkConfigService.class);
246 service.getSubjectClasses()
247 .forEach(subjectClass -> service.getSubjects(subjectClass)
248 .forEach(subject -> service.getConfigs(subject)
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700249 .forEach(config -> service.removeConfig(subject, config.getClass()))));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700250 return Response.ok().build();
251 }
252
253 /**
254 * Clear all network configurations for a subject class.
255 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700256 * @param subjectClassKey subject class key
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700257 * @return empty response
258 */
259 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700260 @Path("{subjectClassKey}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700261 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700262 public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700263 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700264 service.getSubjects(service.getSubjectFactory(subjectClassKey).getClass())
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700265 .forEach(subject -> service.getConfigs(subject)
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700266 .forEach(config -> service.removeConfig(subject, config.getClass())));
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700267 return Response.ok().build();
268 }
269
270 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700271 * Clear all network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700272 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700273 * @param subjectClassKey subjectKey class key
274 * @param subjectKey subjectKey key
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700275 * @return empty response
276 */
277 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700278 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700279 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700280 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
281 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700282 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700283 Object s = service.getSubjectFactory(subjectClassKey).createSubject(subjectKey);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700284 service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
285 return Response.ok().build();
286 }
287
288 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700289 * Clear specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700290 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700291 * @param subjectClassKey subjectKey class key
292 * @param subjectKey subjectKey key
293 * @param configKey configuration class key
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700294 * @return empty response
295 */
296 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700297 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700298 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700299 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
300 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700301 @PathParam("configKey") String configKey) {
302 NetworkConfigService service = get(NetworkConfigService.class);
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700303 service.removeConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
304 service.getConfigClass(subjectClassKey, configKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700305 return Response.ok().build();
306 }
307
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700308}