808fcc16d50809320276fbca59706da6338e787c
[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             SubjectFactory subjectFactory = service.getSubjectFactory(sc);
54             produceJson(service, newObject(root, subjectFactory.subjectClassKey()),
55                         subjectFactory, sc);
56         });
57         return ok(root).build();
58     }
59
60     /**
61      * Get all network configuration for a subject class.
62      *
63      * @param subjectClassKey subject class key
64      * @return network configuration JSON
65      */
66     @GET
67     @Path("{subjectClassKey}")
68     @Produces(MediaType.APPLICATION_JSON)
69     @SuppressWarnings("unchecked")
70     public Response download(@PathParam("subjectClassKey") String subjectClassKey) {
71         NetworkConfigService service = get(NetworkConfigService.class);
72         ObjectNode root = mapper().createObjectNode();
73         SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
74         produceJson(service, root, subjectFactory, subjectFactory.subjectClass());
75         return ok(root).build();
76     }
77
78     /**
79      * Get all network configuration for a subjectKey.
80      *
81      * @param subjectClassKey subjectKey class key
82      * @param subjectKey      subjectKey key
83      * @return network configuration JSON
84      */
85     @GET
86     @Path("{subjectClassKey}/{subjectKey}")
87     @Produces(MediaType.APPLICATION_JSON)
88     @SuppressWarnings("unchecked")
89     public Response download(@PathParam("subjectClassKey") String subjectClassKey,
90                              @PathParam("subjectKey") String subjectKey) {
91         NetworkConfigService service = get(NetworkConfigService.class);
92         ObjectNode root = mapper().createObjectNode();
93         SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
94         produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey));
95         return ok(root).build();
96     }
97
98     /**
99      * Get specific network configuration for a subjectKey.
100      *
101      * @param subjectClassKey subjectKey class key
102      * @param subjectKey      subjectKey key
103      * @param configKey       configuration class key
104      * @return network configuration JSON
105      */
106     @GET
107     @Path("{subjectClassKey}/{subjectKey}/{configKey}")
108     @Produces(MediaType.APPLICATION_JSON)
109     @SuppressWarnings("unchecked")
110     public Response download(@PathParam("subjectClassKey") String subjectClassKey,
111                              @PathParam("subjectKey") String subjectKey,
112                              @PathParam("configKey") String configKey) {
113         NetworkConfigService service = get(NetworkConfigService.class);
114         return ok(service.getConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
115                                     service.getConfigClass(subjectClassKey, configKey)).node()).build();
116     }
117
118     @SuppressWarnings("unchecked")
119     private void produceJson(NetworkConfigService service, ObjectNode node,
120                              SubjectFactory subjectFactory, Class subjectClass) {
121         service.getSubjects(subjectClass).forEach(s ->
122             produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s));
123     }
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     /**
132      * Upload bulk network configuration.
133      *
134      * @param request network configuration JSON rooted at the top node
135      * @return empty response
136      * @throws IOException if unable to parse the request
137      */
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),
146                                                     service.getSubjectFactory(sk)));
147         return Response.ok().build();
148     }
149
150     /**
151      * Upload multiple network configurations for a subject class.
152      *
153      * @param subjectClassKey subject class key
154      * @param request         network configuration JSON rooted at the top node
155      * @return empty response
156      * @throws IOException if unable to parse the request
157      */
158     @POST
159     @Path("{subjectClassKey}")
160     @Consumes(MediaType.APPLICATION_JSON)
161     @SuppressWarnings("unchecked")
162     public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
163                            InputStream request) throws IOException {
164         NetworkConfigService service = get(NetworkConfigService.class);
165         ObjectNode root = (ObjectNode) mapper().readTree(request);
166         consumeJson(service, root, service.getSubjectFactory(subjectClassKey));
167         return Response.ok().build();
168     }
169
170     /**
171      * Upload mutliple network configurations for a subjectKey.
172      *
173      * @param subjectClassKey subjectKey class key
174      * @param subjectKey      subjectKey key
175      * @param request         network configuration JSON rooted at the top node
176      * @return empty response
177      * @throws IOException if unable to parse the request
178      */
179     @POST
180     @Path("{subjectClassKey}/{subjectKey}")
181     @Consumes(MediaType.APPLICATION_JSON)
182     @SuppressWarnings("unchecked")
183     public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
184                            @PathParam("subjectKey") String subjectKey,
185                            InputStream request) throws IOException {
186         NetworkConfigService service = get(NetworkConfigService.class);
187         ObjectNode root = (ObjectNode) mapper().readTree(request);
188         consumeSubjectJson(service, root,
189                            service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
190                            subjectClassKey);
191         return Response.ok().build();
192     }
193
194     /**
195      * Upload specific network configuration for a subjectKey.
196      *
197      * @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
201      * @return empty response
202      * @throws IOException if unable to parse the request
203      */
204     @POST
205     @Path("{subjectClassKey}/{subjectKey}/{configKey}")
206     @Consumes(MediaType.APPLICATION_JSON)
207     @SuppressWarnings("unchecked")
208     public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
209                            @PathParam("subjectKey") String subjectKey,
210                            @PathParam("configKey") String configKey,
211                            InputStream request) throws IOException {
212         NetworkConfigService service = get(NetworkConfigService.class);
213         ObjectNode root = (ObjectNode) mapper().readTree(request);
214         service.applyConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
215                             service.getConfigClass(subjectClassKey, configKey), root);
216         return Response.ok().build();
217     }
218
219     private void consumeJson(NetworkConfigService service, ObjectNode classNode,
220                              SubjectFactory subjectFactory) {
221         classNode.fieldNames().forEachRemaining(s ->
222             consumeSubjectJson(service, (ObjectNode) classNode.path(s),
223                                subjectFactory.createSubject(s),
224                                subjectFactory.subjectClassKey()));
225     }
226
227     private void consumeSubjectJson(NetworkConfigService service,
228                                     ObjectNode subjectNode, Object subject,
229                                     String subjectKey) {
230         subjectNode.fieldNames().forEachRemaining(c ->
231             service.applyConfig(subject, service.getConfigClass(subjectKey, c),
232                                 subjectNode.path(c)));
233     }
234
235
236     /**
237      * 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)
248                                 .forEach(config -> service.removeConfig(subject, config.getClass()))));
249         return Response.ok().build();
250     }
251
252     /**
253      * Clear all network configurations for a subject class.
254      *
255      * @param subjectClassKey subject class key
256      * @return empty response
257      */
258     @DELETE
259     @Path("{subjectClassKey}")
260     @SuppressWarnings("unchecked")
261     public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
262         NetworkConfigService service = get(NetworkConfigService.class);
263         service.getSubjects(service.getSubjectFactory(subjectClassKey).getClass())
264                 .forEach(subject -> service.getConfigs(subject)
265                         .forEach(config -> service.removeConfig(subject, config.getClass())));
266         return Response.ok().build();
267     }
268
269     /**
270      * Clear all network configurations for a subjectKey.
271      *
272      * @param subjectClassKey subjectKey class key
273      * @param subjectKey      subjectKey key
274      * @return empty response
275      */
276     @DELETE
277     @Path("{subjectClassKey}/{subjectKey}")
278     @SuppressWarnings("unchecked")
279     public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
280                            @PathParam("subjectKey") String subjectKey) {
281         NetworkConfigService service = get(NetworkConfigService.class);
282         Object s = service.getSubjectFactory(subjectClassKey).createSubject(subjectKey);
283         service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass()));
284         return Response.ok().build();
285     }
286
287     /**
288      * Clear specific network configuration for a subjectKey.
289      *
290      * @param subjectClassKey subjectKey class key
291      * @param subjectKey      subjectKey key
292      * @param configKey       configuration class key
293      * @return empty response
294      */
295     @DELETE
296     @Path("{subjectClassKey}/{subjectKey}/{configKey}")
297     @SuppressWarnings("unchecked")
298     public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
299                            @PathParam("subjectKey") String subjectKey,
300                            @PathParam("configKey") String configKey) {
301         NetworkConfigService service = get(NetworkConfigService.class);
302         service.removeConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey),
303                              service.getConfigClass(subjectClassKey, configKey));
304         return Response.ok().build();
305     }
306
307 }