blob: 77dd4ea929e015eacf1e13c379028f13ba4f5daf [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070044
45/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070046 * Manage network configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070047 */
48@Path("network/configuration")
49public class NetworkConfigWebResource extends AbstractWebResource {
50
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +053051 //FIX ME not found Multi status error code 207 in jaxrs Response Status.
52 private static final int MULTI_STATUS_RESPONE = 207;
53
Ray Milkey36992c82015-11-17 13:31:15 -080054 private String subjectClassNotFoundErrorString(String subjectClassKey) {
55 return "Config for '" + subjectClassKey + "' not found";
56 }
57
58 private String subjectNotFoundErrorString(String subjectClassKey,
59 String subjectKey) {
60 return "Config for '"
61 + subjectClassKey + "/" + subjectKey
62 + "' not found";
63 }
64
65 private String configKeyNotFoundErrorString(String subjectClassKey,
66 String subjectKey,
67 String configKey) {
68 return "Config for '"
69 + subjectClassKey + "/" + subjectKey + "/" + configKey
70 + "' not found";
71 }
72
deepada611462017-06-02 09:22:55 +053073 private String subjectClassNotValidErrorString(String subjectClassKey) {
74 return "subjectClassKey '" + subjectClassKey + "' not found";
75 }
76
Thomas Vachuska96d55b12015-05-11 08:52:03 -070077 /**
Jian Licc730a62016-05-10 16:36:16 -070078 * Gets entire network configuration base.
Ray Milkey36992c82015-11-17 13:31:15 -080079 *
Jian Licc730a62016-05-10 16:36:16 -070080 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -070081 */
82 @GET
83 @Produces(MediaType.APPLICATION_JSON)
84 @SuppressWarnings("unchecked")
85 public Response download() {
86 NetworkConfigService service = get(NetworkConfigService.class);
87 ObjectNode root = mapper().createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070088 service.getSubjectClasses().forEach(sc -> {
89 SubjectFactory subjectFactory = service.getSubjectFactory(sc);
90 produceJson(service, newObject(root, subjectFactory.subjectClassKey()),
91 subjectFactory, sc);
92 });
Thomas Vachuska96d55b12015-05-11 08:52:03 -070093 return ok(root).build();
94 }
95
96 /**
Jian Licc730a62016-05-10 16:36:16 -070097 * Gets all network configuration for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -070098 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070099 * @param subjectClassKey subject class key
Jian Licc730a62016-05-10 16:36:16 -0700100 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700101 */
102 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700103 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700104 @Produces(MediaType.APPLICATION_JSON)
105 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700106 public Response download(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700107 NetworkConfigService service = get(NetworkConfigService.class);
108 ObjectNode root = mapper().createObjectNode();
Ray Milkey36992c82015-11-17 13:31:15 -0800109 SubjectFactory subjectFactory =
110 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
111 subjectClassNotFoundErrorString(subjectClassKey));
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700112 produceJson(service, root, subjectFactory, subjectFactory.subjectClass());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700113 return ok(root).build();
114 }
115
116 /**
Jian Licc730a62016-05-10 16:36:16 -0700117 * Gets all network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700118 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700119 * @param subjectClassKey subjectKey class key
120 * @param subjectKey subjectKey key
Jian Licc730a62016-05-10 16:36:16 -0700121 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700122 */
123 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700124 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700125 @Produces(MediaType.APPLICATION_JSON)
126 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700127 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
128 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700129 NetworkConfigService service = get(NetworkConfigService.class);
130 ObjectNode root = mapper().createObjectNode();
Ray Milkey36992c82015-11-17 13:31:15 -0800131 SubjectFactory subjectFactory =
132 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
133 subjectClassNotFoundErrorString(subjectClassKey));
134 produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey),
135 true,
136 subjectNotFoundErrorString(subjectClassKey, subjectKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700137 return ok(root).build();
138 }
139
140 /**
Jian Licc730a62016-05-10 16:36:16 -0700141 * Gets specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700142 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700143 * @param subjectClassKey subjectKey class key
144 * @param subjectKey subjectKey key
145 * @param configKey configuration class key
Jian Licc730a62016-05-10 16:36:16 -0700146 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700147 */
148 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700149 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700150 @Produces(MediaType.APPLICATION_JSON)
151 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700152 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
153 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700154 @PathParam("configKey") String configKey) {
155 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey36992c82015-11-17 13:31:15 -0800156
deepada611462017-06-02 09:22:55 +0530157 SubjectFactory subjectFactory =
158 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
159 subjectClassNotFoundErrorString(subjectClassKey));
Ray Milkey36992c82015-11-17 13:31:15 -0800160 Object subject =
deepada611462017-06-02 09:22:55 +0530161 nullIsNotFound(subjectFactory.createSubject(subjectKey),
Ray Milkey36992c82015-11-17 13:31:15 -0800162 subjectNotFoundErrorString(subjectClassKey, subjectKey));
163
164 Class configClass =
165 nullIsNotFound(service.getConfigClass(subjectClassKey, configKey),
166 configKeyNotFoundErrorString(subjectClassKey, subjectKey, configKey));
Jian Li7d069232016-05-13 17:08:29 -0700167 Config config = nullIsNotFound((Config) service.getConfig(subject, configClass),
Ray Milkey36992c82015-11-17 13:31:15 -0800168 configKeyNotFoundErrorString(subjectClassKey,
169 subjectKey,
170 configKey));
171 return ok(config.node()).build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700172 }
173
174 @SuppressWarnings("unchecked")
175 private void produceJson(NetworkConfigService service, ObjectNode node,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700176 SubjectFactory subjectFactory, Class subjectClass) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700177 service.getSubjects(subjectClass).forEach(s ->
Ray Milkey36992c82015-11-17 13:31:15 -0800178 produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s, false, ""));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700179 }
180
181 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
Ray Milkey36992c82015-11-17 13:31:15 -0800182 Object subject,
183 boolean emptyIsError,
184 String emptyErrorMessage) {
185 Set<? extends Config<Object>> configs = service.getConfigs(subject);
186 if (emptyIsError) {
187 // caller wants an empty set to be a 404
188 configs = emptyIsNotFound(configs, emptyErrorMessage);
189 }
190 configs.forEach(c -> node.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700191 }
192
193
194 /**
Jian Licc730a62016-05-10 16:36:16 -0700195 * Uploads bulk network configuration.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700196 *
197 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700198 * @return 200 OK
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700199 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700200 */
201 @POST
202 @Consumes(MediaType.APPLICATION_JSON)
203 @SuppressWarnings("unchecked")
204 public Response upload(InputStream request) throws IOException {
205 NetworkConfigService service = get(NetworkConfigService.class);
206 ObjectNode root = (ObjectNode) mapper().readTree(request);
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530207 List<String> errorMsgs = new ArrayList<String>();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700208 root.fieldNames()
Ray Milkey88cc3432017-03-30 17:19:08 -0700209 .forEachRemaining(sk -> {
deepada611462017-06-02 09:22:55 +0530210 if (service.getSubjectFactory(sk) == null) {
211 errorMsgs.add(subjectClassNotValidErrorString(sk));
212 } else {
213 errorMsgs.addAll(consumeJson(service, (ObjectNode) root.path(sk),
214 service.getSubjectFactory(sk)));
215 }
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530216 });
Jon Hallcbd1b392017-01-18 20:15:44 -0800217 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530218 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
219 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700220 return Response.ok().build();
221 }
222
223 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700224 * Upload multiple network configurations for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700225 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700226 * @param subjectClassKey subject class key
227 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700228 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700229 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700230 */
231 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700232 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700233 @Consumes(MediaType.APPLICATION_JSON)
234 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700235 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700236 InputStream request) throws IOException {
237 NetworkConfigService service = get(NetworkConfigService.class);
238 ObjectNode root = (ObjectNode) mapper().readTree(request);
deepada611462017-06-02 09:22:55 +0530239 SubjectFactory subjectFactory =
240 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
241 subjectClassNotValidErrorString(subjectClassKey));
242 List<String> errorMsgs = consumeJson(service, root, subjectFactory);
Jon Hallcbd1b392017-01-18 20:15:44 -0800243 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530244 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
245 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700246 return Response.ok().build();
247 }
248
249 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700250 * Upload mutliple network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700251 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700252 * @param subjectClassKey subjectKey class key
253 * @param subjectKey subjectKey key
254 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700255 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700256 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700257 */
258 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700259 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700260 @Consumes(MediaType.APPLICATION_JSON)
261 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700262 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
263 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700264 InputStream request) throws IOException {
265 NetworkConfigService service = get(NetworkConfigService.class);
266 ObjectNode root = (ObjectNode) mapper().readTree(request);
deepada611462017-06-02 09:22:55 +0530267 SubjectFactory subjectFactory =
268 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
269 subjectClassNotValidErrorString(subjectClassKey));
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530270 List<String> errorMsgs = consumeSubjectJson(service, root,
deepada611462017-06-02 09:22:55 +0530271 subjectFactory.createSubject(subjectKey),
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530272 subjectClassKey);
Jon Hallcbd1b392017-01-18 20:15:44 -0800273 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530274 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
275 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700276 return Response.ok().build();
277 }
278
279 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700280 * Upload specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700281 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700282 * @param subjectClassKey subjectKey class key
283 * @param subjectKey subjectKey key
284 * @param configKey configuration class key
285 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700286 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700287 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700288 */
289 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700290 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700291 @Consumes(MediaType.APPLICATION_JSON)
292 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700293 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
294 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700295 @PathParam("configKey") String configKey,
296 InputStream request) throws IOException {
297 NetworkConfigService service = get(NetworkConfigService.class);
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700298 JsonNode root = mapper().readTree(request);
deepada611462017-06-02 09:22:55 +0530299 SubjectFactory subjectFactory =
300 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
301 subjectClassNotValidErrorString(subjectClassKey));
302 service.applyConfig(subjectClassKey, subjectFactory.createSubject(subjectKey),
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800303 configKey, root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700304 return Response.ok().build();
305 }
306
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530307 private List<String> consumeJson(NetworkConfigService service, ObjectNode classNode,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700308 SubjectFactory subjectFactory) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530309 List<String> errorMsgs = new ArrayList<String>();
310 classNode.fieldNames().forEachRemaining(s -> {
311 List<String> error = consumeSubjectJson(service, (ObjectNode) classNode.path(s),
312 subjectFactory.createSubject(s),
313 subjectFactory.subjectClassKey());
314 errorMsgs.addAll(error);
315 });
316 return errorMsgs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700317 }
318
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530319 private List<String> consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700320 ObjectNode subjectNode, Object subject,
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800321 String subjectClassKey) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530322 List<String> errorMsgs = new ArrayList<String>();
323 subjectNode.fieldNames().forEachRemaining(configKey -> {
324 try {
325 service.applyConfig(subjectClassKey, subject, configKey, subjectNode.path(configKey));
326 } catch (IllegalArgumentException e) {
327 errorMsgs.add("Error parsing config " + subjectClassKey + "/" + subject + "/" + configKey);
deepada611462017-06-02 09:22:55 +0530328 } catch (InvalidConfigException exception) {
329 errorMsgs.add(exception.getMessage());
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530330 }
331 });
332 return errorMsgs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700333 }
334
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530335 private ObjectNode produceErrorJson(List<String> errorMsgs) {
336 ObjectMapper mapper = new ObjectMapper();
337 ObjectNode result = mapper.createObjectNode().put("code", 207).putPOJO("message", errorMsgs);
338 return result;
339 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700340
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800341 // FIXME: Refactor to allow queued configs to be removed
342
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700343 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700344 * Clear entire network configuration base.
345 *
Jian Lic2a542b2016-05-10 11:48:19 -0700346 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700347 */
348 @DELETE
349 @SuppressWarnings("unchecked")
350 public Response delete() {
351 NetworkConfigService service = get(NetworkConfigService.class);
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530352 service.removeConfig();
Ray Milkey984e2d82016-06-16 14:33:10 -0700353 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700354 }
355
356 /**
357 * Clear all network configurations for a subject class.
358 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700359 * @param subjectClassKey subject class key
Ray Milkey984e2d82016-06-16 14:33:10 -0700360 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700361 */
362 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700363 @Path("{subjectClassKey}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700364 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700365 public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700366 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530367 SubjectFactory subjectFactory =
368 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
369 subjectClassNotValidErrorString(subjectClassKey));
370 service.getSubjects(subjectFactory.subjectClass())
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530371 .forEach(subject -> service.removeConfig(subject));
Ray Milkey984e2d82016-06-16 14:33:10 -0700372 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700373 }
374
375 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700376 * Clear all network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700377 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700378 * @param subjectClassKey subjectKey class key
379 * @param subjectKey subjectKey key
Ray Milkey984e2d82016-06-16 14:33:10 -0700380 * @return 204 NO CONTENT
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700381 */
382 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700383 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700384 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700385 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700386 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700387 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530388 SubjectFactory subjectFactory =
389 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
390 subjectClassNotValidErrorString(subjectClassKey));
391 service.removeConfig(subjectFactory.createSubject(subjectKey));
Ray Milkey984e2d82016-06-16 14:33:10 -0700392 return Response.noContent().build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700393 }
394
395 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700396 * Clear specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700397 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700398 * @param subjectClassKey subjectKey class key
399 * @param subjectKey subjectKey key
400 * @param configKey configuration class key
Ray Milkey984e2d82016-06-16 14:33:10 -0700401 * @return 204 NO CONTENT
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700402 */
403 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700404 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700405 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700406 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700407 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700408 @PathParam("configKey") String configKey) {
409 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530410 SubjectFactory subjectFactory =
411 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
412 subjectClassNotValidErrorString(subjectClassKey));
413 service.removeConfig(subjectClassKey, subjectFactory.createSubject(subjectKey),
414 configKey);
Ray Milkey984e2d82016-06-16 14:33:10 -0700415 return Response.noContent().build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700416 }
417
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700418}