blob: e50e01164f40e356581c3af988a20f21a45131ec [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska96d55b12015-05-11 08:52:03 -07003 *
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
Jonathan Hartb11c4d02016-03-23 09:05:44 -070018import com.fasterxml.jackson.databind.JsonNode;
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +053019import com.fasterxml.jackson.databind.ObjectMapper;
Jonathan Hartb11c4d02016-03-23 09:05:44 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.net.config.Config;
deepada611462017-06-02 09:22:55 +053022import org.onosproject.net.config.InvalidConfigException;
Jonathan Hartb11c4d02016-03-23 09:05:44 -070023import org.onosproject.net.config.NetworkConfigService;
24import org.onosproject.net.config.SubjectFactory;
25import org.onosproject.rest.AbstractWebResource;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070026
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
Jonathan Hartb11c4d02016-03-23 09:05:44 -070036import java.io.IOException;
37import java.io.InputStream;
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +053038import java.util.ArrayList;
39import java.util.List;
Jonathan Hartb11c4d02016-03-23 09:05:44 -070040import java.util.Set;
Ray Milkey36992c82015-11-17 13:31:15 -080041
42import static org.onlab.util.Tools.emptyIsNotFound;
43import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070044import static org.onlab.util.Tools.readTreeFromStream;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070045
46/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070047 * Manage network configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070048 */
49@Path("network/configuration")
50public class NetworkConfigWebResource extends AbstractWebResource {
51
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +053052 //FIX ME not found Multi status error code 207 in jaxrs Response Status.
53 private static final int MULTI_STATUS_RESPONE = 207;
54
Ray Milkey36992c82015-11-17 13:31:15 -080055 private String subjectClassNotFoundErrorString(String subjectClassKey) {
56 return "Config for '" + subjectClassKey + "' not found";
57 }
58
59 private String subjectNotFoundErrorString(String subjectClassKey,
60 String subjectKey) {
61 return "Config for '"
62 + subjectClassKey + "/" + subjectKey
63 + "' not found";
64 }
65
66 private String configKeyNotFoundErrorString(String subjectClassKey,
67 String subjectKey,
68 String configKey) {
69 return "Config for '"
70 + subjectClassKey + "/" + subjectKey + "/" + configKey
71 + "' not found";
72 }
73
Ray Milkeyb83797c2017-06-12 15:42:42 -070074 private String subjectClassInvalidErrorString(String subjectClassKey) {
75 return "Config for '" + subjectClassKey + "' is invalid";
76 }
77
deepada611462017-06-02 09:22:55 +053078 private String subjectClassNotValidErrorString(String subjectClassKey) {
79 return "subjectClassKey '" + subjectClassKey + "' not found";
80 }
81
Thomas Vachuska96d55b12015-05-11 08:52:03 -070082 /**
Jian Licc730a62016-05-10 16:36:16 -070083 * Gets entire network configuration base.
Ray Milkey36992c82015-11-17 13:31:15 -080084 *
Jian Licc730a62016-05-10 16:36:16 -070085 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -070086 */
87 @GET
88 @Produces(MediaType.APPLICATION_JSON)
89 @SuppressWarnings("unchecked")
90 public Response download() {
91 NetworkConfigService service = get(NetworkConfigService.class);
92 ObjectNode root = mapper().createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070093 service.getSubjectClasses().forEach(sc -> {
94 SubjectFactory subjectFactory = service.getSubjectFactory(sc);
95 produceJson(service, newObject(root, subjectFactory.subjectClassKey()),
96 subjectFactory, sc);
97 });
Thomas Vachuska96d55b12015-05-11 08:52:03 -070098 return ok(root).build();
99 }
100
101 /**
Jian Licc730a62016-05-10 16:36:16 -0700102 * Gets all network configuration for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700103 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700104 * @param subjectClassKey subject class key
Jian Licc730a62016-05-10 16:36:16 -0700105 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700106 */
107 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700108 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700109 @Produces(MediaType.APPLICATION_JSON)
110 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700111 public Response download(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700112 NetworkConfigService service = get(NetworkConfigService.class);
113 ObjectNode root = mapper().createObjectNode();
Ray Milkey36992c82015-11-17 13:31:15 -0800114 SubjectFactory subjectFactory =
115 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
116 subjectClassNotFoundErrorString(subjectClassKey));
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700117 produceJson(service, root, subjectFactory, subjectFactory.subjectClass());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700118 return ok(root).build();
119 }
120
121 /**
Jian Licc730a62016-05-10 16:36:16 -0700122 * Gets all network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700123 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700124 * @param subjectClassKey subjectKey class key
125 * @param subjectKey subjectKey key
Jian Licc730a62016-05-10 16:36:16 -0700126 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700127 */
128 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700129 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700130 @Produces(MediaType.APPLICATION_JSON)
131 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700132 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
133 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700134 NetworkConfigService service = get(NetworkConfigService.class);
135 ObjectNode root = mapper().createObjectNode();
Ray Milkey36992c82015-11-17 13:31:15 -0800136 SubjectFactory subjectFactory =
137 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
138 subjectClassNotFoundErrorString(subjectClassKey));
139 produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey),
140 true,
141 subjectNotFoundErrorString(subjectClassKey, subjectKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700142 return ok(root).build();
143 }
144
145 /**
Jian Licc730a62016-05-10 16:36:16 -0700146 * Gets specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700147 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700148 * @param subjectClassKey subjectKey class key
149 * @param subjectKey subjectKey key
150 * @param configKey configuration class key
Jian Licc730a62016-05-10 16:36:16 -0700151 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700152 */
153 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700154 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700155 @Produces(MediaType.APPLICATION_JSON)
156 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700157 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
158 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700159 @PathParam("configKey") String configKey) {
160 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey36992c82015-11-17 13:31:15 -0800161
deepada611462017-06-02 09:22:55 +0530162 SubjectFactory subjectFactory =
163 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
164 subjectClassNotFoundErrorString(subjectClassKey));
Ray Milkey36992c82015-11-17 13:31:15 -0800165 Object subject =
deepada611462017-06-02 09:22:55 +0530166 nullIsNotFound(subjectFactory.createSubject(subjectKey),
Ray Milkey36992c82015-11-17 13:31:15 -0800167 subjectNotFoundErrorString(subjectClassKey, subjectKey));
168
169 Class configClass =
170 nullIsNotFound(service.getConfigClass(subjectClassKey, configKey),
171 configKeyNotFoundErrorString(subjectClassKey, subjectKey, configKey));
Jian Li7d069232016-05-13 17:08:29 -0700172 Config config = nullIsNotFound((Config) service.getConfig(subject, configClass),
Ray Milkey36992c82015-11-17 13:31:15 -0800173 configKeyNotFoundErrorString(subjectClassKey,
174 subjectKey,
175 configKey));
176 return ok(config.node()).build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700177 }
178
179 @SuppressWarnings("unchecked")
180 private void produceJson(NetworkConfigService service, ObjectNode node,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700181 SubjectFactory subjectFactory, Class subjectClass) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700182 service.getSubjects(subjectClass).forEach(s ->
Ray Milkey36992c82015-11-17 13:31:15 -0800183 produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s, false, ""));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700184 }
185
186 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
Ray Milkey36992c82015-11-17 13:31:15 -0800187 Object subject,
188 boolean emptyIsError,
189 String emptyErrorMessage) {
190 Set<? extends Config<Object>> configs = service.getConfigs(subject);
191 if (emptyIsError) {
192 // caller wants an empty set to be a 404
193 configs = emptyIsNotFound(configs, emptyErrorMessage);
194 }
195 configs.forEach(c -> node.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700196 }
197
198
199 /**
Jian Licc730a62016-05-10 16:36:16 -0700200 * Uploads bulk network configuration.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700201 *
202 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700203 * @return 200 OK
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700204 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700205 */
206 @POST
207 @Consumes(MediaType.APPLICATION_JSON)
208 @SuppressWarnings("unchecked")
209 public Response upload(InputStream request) throws IOException {
210 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -0700211 ObjectNode root = readTreeFromStream(mapper(), request);
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530212 List<String> errorMsgs = new ArrayList<String>();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700213 root.fieldNames()
Ray Milkey88cc3432017-03-30 17:19:08 -0700214 .forEachRemaining(sk -> {
Ray Milkeyb83797c2017-06-12 15:42:42 -0700215 if (service.getSubjectFactory(sk) == null) {
deepada611462017-06-02 09:22:55 +0530216 errorMsgs.add(subjectClassNotValidErrorString(sk));
Ray Milkeyb83797c2017-06-12 15:42:42 -0700217 } else if (!root.path(sk).isObject()) {
218 errorMsgs.add(subjectClassInvalidErrorString(sk));
deepada611462017-06-02 09:22:55 +0530219 } else {
220 errorMsgs.addAll(consumeJson(service, (ObjectNode) root.path(sk),
221 service.getSubjectFactory(sk)));
222 }
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530223 });
Jon Hallcbd1b392017-01-18 20:15:44 -0800224 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530225 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
226 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700227 return Response.ok().build();
228 }
229
230 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700231 * Upload multiple network configurations for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700232 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700233 * @param subjectClassKey subject class key
234 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700235 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700236 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700237 */
238 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700239 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700240 @Consumes(MediaType.APPLICATION_JSON)
241 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700242 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700243 InputStream request) throws IOException {
244 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -0700245 ObjectNode root = readTreeFromStream(mapper(), request);
deepada611462017-06-02 09:22:55 +0530246 SubjectFactory subjectFactory =
247 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
248 subjectClassNotValidErrorString(subjectClassKey));
249 List<String> errorMsgs = consumeJson(service, root, subjectFactory);
Jon Hallcbd1b392017-01-18 20:15:44 -0800250 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530251 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
252 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700253 return Response.ok().build();
254 }
255
256 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700257 * Upload mutliple network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700258 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700259 * @param subjectClassKey subjectKey class key
260 * @param subjectKey subjectKey key
261 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700262 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700263 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700264 */
265 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700266 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700267 @Consumes(MediaType.APPLICATION_JSON)
268 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700269 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
270 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700271 InputStream request) throws IOException {
272 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -0700273 ObjectNode root = readTreeFromStream(mapper(), request);
deepada611462017-06-02 09:22:55 +0530274 SubjectFactory subjectFactory =
275 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
276 subjectClassNotValidErrorString(subjectClassKey));
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530277 List<String> errorMsgs = consumeSubjectJson(service, root,
deepada611462017-06-02 09:22:55 +0530278 subjectFactory.createSubject(subjectKey),
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530279 subjectClassKey);
Jon Hallcbd1b392017-01-18 20:15:44 -0800280 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530281 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
282 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700283 return Response.ok().build();
284 }
285
286 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700287 * Upload specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700288 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700289 * @param subjectClassKey subjectKey class key
290 * @param subjectKey subjectKey key
291 * @param configKey configuration class key
292 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700293 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700294 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700295 */
296 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700297 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700298 @Consumes(MediaType.APPLICATION_JSON)
299 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700300 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
301 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700302 @PathParam("configKey") String configKey,
303 InputStream request) throws IOException {
304 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -0700305 JsonNode root = readTreeFromStream(mapper(), request);
deepada611462017-06-02 09:22:55 +0530306 SubjectFactory subjectFactory =
307 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
308 subjectClassNotValidErrorString(subjectClassKey));
309 service.applyConfig(subjectClassKey, subjectFactory.createSubject(subjectKey),
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800310 configKey, root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700311 return Response.ok().build();
312 }
313
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530314 private List<String> consumeJson(NetworkConfigService service, ObjectNode classNode,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700315 SubjectFactory subjectFactory) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530316 List<String> errorMsgs = new ArrayList<String>();
317 classNode.fieldNames().forEachRemaining(s -> {
318 List<String> error = consumeSubjectJson(service, (ObjectNode) classNode.path(s),
319 subjectFactory.createSubject(s),
320 subjectFactory.subjectClassKey());
321 errorMsgs.addAll(error);
322 });
323 return errorMsgs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700324 }
325
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530326 private List<String> consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700327 ObjectNode subjectNode, Object subject,
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800328 String subjectClassKey) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530329 List<String> errorMsgs = new ArrayList<String>();
330 subjectNode.fieldNames().forEachRemaining(configKey -> {
331 try {
332 service.applyConfig(subjectClassKey, subject, configKey, subjectNode.path(configKey));
333 } catch (IllegalArgumentException e) {
334 errorMsgs.add("Error parsing config " + subjectClassKey + "/" + subject + "/" + configKey);
deepada611462017-06-02 09:22:55 +0530335 } catch (InvalidConfigException exception) {
336 errorMsgs.add(exception.getMessage());
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530337 }
338 });
339 return errorMsgs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700340 }
341
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530342 private ObjectNode produceErrorJson(List<String> errorMsgs) {
343 ObjectMapper mapper = new ObjectMapper();
344 ObjectNode result = mapper.createObjectNode().put("code", 207).putPOJO("message", errorMsgs);
345 return result;
346 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700347
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800348 // FIXME: Refactor to allow queued configs to be removed
349
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700350 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700351 * Clear entire network configuration base.
352 *
Jian Lic2a542b2016-05-10 11:48:19 -0700353 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700354 */
355 @DELETE
356 @SuppressWarnings("unchecked")
357 public Response delete() {
358 NetworkConfigService service = get(NetworkConfigService.class);
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530359 service.removeConfig();
Ray Milkey984e2d82016-06-16 14:33:10 -0700360 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700361 }
362
363 /**
364 * Clear all network configurations for a subject class.
365 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700366 * @param subjectClassKey subject class key
Ray Milkey984e2d82016-06-16 14:33:10 -0700367 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700368 */
369 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700370 @Path("{subjectClassKey}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700371 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700372 public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700373 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530374 SubjectFactory subjectFactory =
375 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
376 subjectClassNotValidErrorString(subjectClassKey));
377 service.getSubjects(subjectFactory.subjectClass())
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530378 .forEach(subject -> service.removeConfig(subject));
Ray Milkey984e2d82016-06-16 14:33:10 -0700379 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700380 }
381
382 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700383 * Clear all network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700384 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700385 * @param subjectClassKey subjectKey class key
386 * @param subjectKey subjectKey key
Ray Milkey984e2d82016-06-16 14:33:10 -0700387 * @return 204 NO CONTENT
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700388 */
389 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700390 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700391 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700392 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700393 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700394 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530395 SubjectFactory subjectFactory =
396 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
397 subjectClassNotValidErrorString(subjectClassKey));
398 service.removeConfig(subjectFactory.createSubject(subjectKey));
Ray Milkey984e2d82016-06-16 14:33:10 -0700399 return Response.noContent().build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700400 }
401
402 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700403 * Clear specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700404 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700405 * @param subjectClassKey subjectKey class key
406 * @param subjectKey subjectKey key
407 * @param configKey configuration class key
Ray Milkey984e2d82016-06-16 14:33:10 -0700408 * @return 204 NO CONTENT
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700409 */
410 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700411 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700412 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700413 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700414 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700415 @PathParam("configKey") String configKey) {
416 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530417 SubjectFactory subjectFactory =
418 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
419 subjectClassNotValidErrorString(subjectClassKey));
420 service.removeConfig(subjectClassKey, subjectFactory.createSubject(subjectKey),
421 configKey);
Ray Milkey984e2d82016-06-16 14:33:10 -0700422 return Response.noContent().build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700423 }
424
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700425}