9e2b6273fa0d018a3e589d1870548dcefb3ccd69
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.rest.resources;
17
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;
22
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;
34
35 /**
36  * Manage network configurations.
37  */
38 @Path("network/configuration")
39 public class NetworkConfigWebResource extends AbstractWebResource {
40
41     /**
42      * Get 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      * Get all network configuration for a 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      * Get all network configuration for a 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,
90                            service.getSubjectFactory(subjectKey).createSubject(subject));
91         return ok(root).build();
92     }
93
94     /**
95      * Get specific network configuration for a subject.
96      *
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),
111                                     service.getConfigClass(subjectKey, configKey)).node()).build();
112     }
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     /**
128      * Upload bulk network configuration.
129      *
130      * @param request network configuration JSON rooted at the top node
131      * @throws IOException if unable to parse the request
132      * @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),
142                                                     service.getSubjectFactory(sk)));
143         return Response.ok().build();
144     }
145
146     /**
147      * Upload multiple network configurations for a subject class.
148      *
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
153      */
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     /**
167      * Upload mutliple network configurations for a subject.
168      *
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
174      */
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,
185                            service.getSubjectFactory(subjectKey).createSubject(subject),
186                            subjectKey);
187         return Response.ok().build();
188     }
189
190     /**
191      * Upload specific network configuration for a subject.
192      *
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
199      */
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),
211                             service.getConfigClass(subjectKey, configKey), root);
212         return Response.ok().build();
213     }
214
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()));
221     }
222
223     private void consumeSubjectJson(NetworkConfigService service,
224                                     ObjectNode subjectNode, Object subject,
225                                     String subjectKey) {
226         subjectNode.fieldNames().forEachRemaining(c ->
227             service.applyConfig(subject, service.getConfigClass(subjectKey, c),
228                                 (ObjectNode) subjectNode.path(c)));
229     }
230
231
232     /**
233      * 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.
269      *
270      * @param subjectKey subject class key
271      * @param subject    subject key
272      * @return empty response
273      */
274     @DELETE
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();
283     }
284
285     /**
286      * Clear specific network configuration for a subject.
287      *
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}")
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();
303     }
304
305 }