2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.rest.resources;
18 import com.fasterxml.jackson.databind.node.ObjectNode;
19 import org.onosproject.net.config.NetworkConfigService;
20 import org.onosproject.net.config.SubjectFactory;
21 import org.onosproject.rest.AbstractWebResource;
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.DELETE;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import java.io.IOException;
33 import java.io.InputStream;
36 * Manage network configurations.
38 @Path("network/configuration")
39 public class NetworkConfigWebResource extends AbstractWebResource {
42 * Get entire network configuration base.
44 * @return network configuration JSON
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();
58 * Get all network configuration for a subject class.
60 * @param subjectKey subject class key
61 * @return network configuration JSON
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();
75 * Get all network configuration for a subject.
77 * @param subjectKey subject class key
78 * @param subject subject key
79 * @return network configuration JSON
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,
90 service.getSubjectFactory(subjectKey).createSubject(subject));
91 return ok(root).build();
95 * Get specific network configuration for a subject.
97 * @param subjectKey subject class key
98 * @param subject subject key
99 * @param configKey configuration class key
100 * @return network configuration JSON
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),
111 service.getConfigClass(subjectKey, configKey)).node()).build();
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));
121 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
123 service.getConfigs(subject).forEach(c -> node.set(c.key(), c.node()));
128 * Upload bulk network configuration.
130 * @param request network configuration JSON rooted at the top node
131 * @throws IOException if unable to parse the request
132 * @return empty response
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);
141 .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk),
142 service.getSubjectFactory(sk)));
143 return Response.ok().build();
147 * Upload multiple network configurations for a subject class.
149 * @param subjectKey subject class key
150 * @param request network configuration JSON rooted at the top node
151 * @return empty response
152 * @throws IOException if unable to parse the request
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();
167 * Upload mutliple network configurations for a subject.
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
173 * @throws IOException if unable to parse the request
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,
185 service.getSubjectFactory(subjectKey).createSubject(subject),
187 return Response.ok().build();
191 * Upload specific network configuration for a subject.
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
198 * @throws IOException if unable to parse the request
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),
211 service.getConfigClass(subjectKey, configKey), root);
212 return Response.ok().build();
215 private void consumeJson(NetworkConfigService service, ObjectNode classNode,
216 SubjectFactory subjectFactory) {
217 classNode.fieldNames().forEachRemaining(s ->
218 consumeSubjectJson(service, (ObjectNode) classNode.path(s),
219 subjectFactory.createSubject(s),
220 subjectFactory.subjectKey()));
223 private void consumeSubjectJson(NetworkConfigService service,
224 ObjectNode subjectNode, Object subject,
226 subjectNode.fieldNames().forEachRemaining(c ->
227 service.applyConfig(subject, service.getConfigClass(subjectKey, c),
228 (ObjectNode) subjectNode.path(c)));
233 * Clear entire network configuration base.
235 * @return empty response
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();
250 * Clear all network configurations for a subject class.
252 * @param subjectKey subject class key
253 * @return empty response
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();
268 * Clear all network configurations for a subject.
270 * @param subjectKey subject class key
271 * @param subject subject key
272 * @return empty response
275 @Path("{subjectKey}/{subject}")
276 @SuppressWarnings("unchecked")
277 public Response delete(@PathParam("subjectKey") String subjectKey,
278 @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();
286 * Clear specific network configuration for a subject.
288 * @param subjectKey subject class key
289 * @param subject subject key
290 * @param configKey configuration class key
291 * @return empty response
294 @Path("{subjectKey}/{subject}/{configKey}")
295 @SuppressWarnings("unchecked")
296 public Response delete(@PathParam("subjectKey") String subjectKey,
297 @PathParam("subject") String subject,
298 @PathParam("configKey") String configKey) {
299 NetworkConfigService service = get(NetworkConfigService.class);
300 service.removeConfig(service.getSubjectFactory(subjectKey).createSubject(subject),
301 service.getConfigClass(subjectKey, configKey));
302 return Response.ok().build();