blob: 8aacbb6d0aa061255160bdb67a1deafd1edfa26f [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;
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
Ray Milkeyb83797c2017-06-12 15:42:42 -070073 private String subjectClassInvalidErrorString(String subjectClassKey) {
74 return "Config for '" + subjectClassKey + "' is invalid";
75 }
76
deepada611462017-06-02 09:22:55 +053077 private String subjectClassNotValidErrorString(String subjectClassKey) {
78 return "subjectClassKey '" + subjectClassKey + "' not found";
79 }
80
Thomas Vachuska96d55b12015-05-11 08:52:03 -070081 /**
Jian Licc730a62016-05-10 16:36:16 -070082 * Gets entire network configuration base.
Ray Milkey36992c82015-11-17 13:31:15 -080083 *
Jian Licc730a62016-05-10 16:36:16 -070084 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -070085 */
86 @GET
87 @Produces(MediaType.APPLICATION_JSON)
88 @SuppressWarnings("unchecked")
89 public Response download() {
90 NetworkConfigService service = get(NetworkConfigService.class);
91 ObjectNode root = mapper().createObjectNode();
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070092 service.getSubjectClasses().forEach(sc -> {
93 SubjectFactory subjectFactory = service.getSubjectFactory(sc);
94 produceJson(service, newObject(root, subjectFactory.subjectClassKey()),
95 subjectFactory, sc);
96 });
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097 return ok(root).build();
98 }
99
100 /**
Jian Licc730a62016-05-10 16:36:16 -0700101 * Gets all network configuration for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700102 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700103 * @param subjectClassKey subject class key
Jian Licc730a62016-05-10 16:36:16 -0700104 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700105 */
106 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700107 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700108 @Produces(MediaType.APPLICATION_JSON)
109 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700110 public Response download(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700111 NetworkConfigService service = get(NetworkConfigService.class);
112 ObjectNode root = mapper().createObjectNode();
Ray Milkey36992c82015-11-17 13:31:15 -0800113 SubjectFactory subjectFactory =
114 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
115 subjectClassNotFoundErrorString(subjectClassKey));
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700116 produceJson(service, root, subjectFactory, subjectFactory.subjectClass());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700117 return ok(root).build();
118 }
119
120 /**
Jian Licc730a62016-05-10 16:36:16 -0700121 * Gets all network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700122 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700123 * @param subjectClassKey subjectKey class key
124 * @param subjectKey subjectKey key
Jian Licc730a62016-05-10 16:36:16 -0700125 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700126 */
127 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700128 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700129 @Produces(MediaType.APPLICATION_JSON)
130 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700131 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
132 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700133 NetworkConfigService service = get(NetworkConfigService.class);
134 ObjectNode root = mapper().createObjectNode();
Ray Milkey36992c82015-11-17 13:31:15 -0800135 SubjectFactory subjectFactory =
136 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
137 subjectClassNotFoundErrorString(subjectClassKey));
138 produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey),
139 true,
140 subjectNotFoundErrorString(subjectClassKey, subjectKey));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700141 return ok(root).build();
142 }
143
144 /**
Jian Licc730a62016-05-10 16:36:16 -0700145 * Gets specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700146 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700147 * @param subjectClassKey subjectKey class key
148 * @param subjectKey subjectKey key
149 * @param configKey configuration class key
Jian Licc730a62016-05-10 16:36:16 -0700150 * @return 200 OK with network configuration JSON
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700151 */
152 @GET
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700153 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700154 @Produces(MediaType.APPLICATION_JSON)
155 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700156 public Response download(@PathParam("subjectClassKey") String subjectClassKey,
157 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700158 @PathParam("configKey") String configKey) {
159 NetworkConfigService service = get(NetworkConfigService.class);
Ray Milkey36992c82015-11-17 13:31:15 -0800160
deepada611462017-06-02 09:22:55 +0530161 SubjectFactory subjectFactory =
162 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
163 subjectClassNotFoundErrorString(subjectClassKey));
Ray Milkey36992c82015-11-17 13:31:15 -0800164 Object subject =
deepada611462017-06-02 09:22:55 +0530165 nullIsNotFound(subjectFactory.createSubject(subjectKey),
Ray Milkey36992c82015-11-17 13:31:15 -0800166 subjectNotFoundErrorString(subjectClassKey, subjectKey));
167
168 Class configClass =
169 nullIsNotFound(service.getConfigClass(subjectClassKey, configKey),
170 configKeyNotFoundErrorString(subjectClassKey, subjectKey, configKey));
Jian Li7d069232016-05-13 17:08:29 -0700171 Config config = nullIsNotFound((Config) service.getConfig(subject, configClass),
Ray Milkey36992c82015-11-17 13:31:15 -0800172 configKeyNotFoundErrorString(subjectClassKey,
173 subjectKey,
174 configKey));
175 return ok(config.node()).build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700176 }
177
178 @SuppressWarnings("unchecked")
179 private void produceJson(NetworkConfigService service, ObjectNode node,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700180 SubjectFactory subjectFactory, Class subjectClass) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700181 service.getSubjects(subjectClass).forEach(s ->
Ray Milkey36992c82015-11-17 13:31:15 -0800182 produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s, false, ""));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700183 }
184
185 private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
Ray Milkey36992c82015-11-17 13:31:15 -0800186 Object subject,
187 boolean emptyIsError,
188 String emptyErrorMessage) {
189 Set<? extends Config<Object>> configs = service.getConfigs(subject);
190 if (emptyIsError) {
191 // caller wants an empty set to be a 404
192 configs = emptyIsNotFound(configs, emptyErrorMessage);
193 }
194 configs.forEach(c -> node.set(c.key(), c.node()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700195 }
196
197
198 /**
Jian Licc730a62016-05-10 16:36:16 -0700199 * Uploads bulk network configuration.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700200 *
201 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700202 * @return 200 OK
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700203 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700204 */
205 @POST
206 @Consumes(MediaType.APPLICATION_JSON)
207 @SuppressWarnings("unchecked")
208 public Response upload(InputStream request) throws IOException {
209 NetworkConfigService service = get(NetworkConfigService.class);
210 ObjectNode root = (ObjectNode) mapper().readTree(request);
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530211 List<String> errorMsgs = new ArrayList<String>();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700212 root.fieldNames()
Ray Milkey88cc3432017-03-30 17:19:08 -0700213 .forEachRemaining(sk -> {
Ray Milkeyb83797c2017-06-12 15:42:42 -0700214 if (service.getSubjectFactory(sk) == null) {
deepada611462017-06-02 09:22:55 +0530215 errorMsgs.add(subjectClassNotValidErrorString(sk));
Ray Milkeyb83797c2017-06-12 15:42:42 -0700216 } else if (!root.path(sk).isObject()) {
217 errorMsgs.add(subjectClassInvalidErrorString(sk));
deepada611462017-06-02 09:22:55 +0530218 } else {
219 errorMsgs.addAll(consumeJson(service, (ObjectNode) root.path(sk),
220 service.getSubjectFactory(sk)));
221 }
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530222 });
Jon Hallcbd1b392017-01-18 20:15:44 -0800223 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530224 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
225 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700226 return Response.ok().build();
227 }
228
229 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700230 * Upload multiple network configurations for a subject class.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700231 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700232 * @param subjectClassKey subject class key
233 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700234 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700235 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700236 */
237 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700238 @Path("{subjectClassKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700239 @Consumes(MediaType.APPLICATION_JSON)
240 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700241 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700242 InputStream request) throws IOException {
243 NetworkConfigService service = get(NetworkConfigService.class);
244 ObjectNode root = (ObjectNode) mapper().readTree(request);
deepada611462017-06-02 09:22:55 +0530245 SubjectFactory subjectFactory =
246 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
247 subjectClassNotValidErrorString(subjectClassKey));
248 List<String> errorMsgs = consumeJson(service, root, subjectFactory);
Jon Hallcbd1b392017-01-18 20:15:44 -0800249 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530250 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
251 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700252 return Response.ok().build();
253 }
254
255 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700256 * Upload mutliple network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700257 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700258 * @param subjectClassKey subjectKey class key
259 * @param subjectKey subjectKey key
260 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700261 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700262 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700263 */
264 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700265 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700266 @Consumes(MediaType.APPLICATION_JSON)
267 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700268 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
269 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700270 InputStream request) throws IOException {
271 NetworkConfigService service = get(NetworkConfigService.class);
272 ObjectNode root = (ObjectNode) mapper().readTree(request);
deepada611462017-06-02 09:22:55 +0530273 SubjectFactory subjectFactory =
274 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
275 subjectClassNotValidErrorString(subjectClassKey));
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530276 List<String> errorMsgs = consumeSubjectJson(service, root,
deepada611462017-06-02 09:22:55 +0530277 subjectFactory.createSubject(subjectKey),
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530278 subjectClassKey);
Jon Hallcbd1b392017-01-18 20:15:44 -0800279 if (!errorMsgs.isEmpty()) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530280 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
281 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700282 return Response.ok().build();
283 }
284
285 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700286 * Upload specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700287 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700288 * @param subjectClassKey subjectKey class key
289 * @param subjectKey subjectKey key
290 * @param configKey configuration class key
291 * @param request network configuration JSON rooted at the top node
Jian Licc730a62016-05-10 16:36:16 -0700292 * @return 200 OK
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700293 * @throws IOException if unable to parse the request
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700294 */
295 @POST
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700296 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700297 @Consumes(MediaType.APPLICATION_JSON)
298 @SuppressWarnings("unchecked")
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700299 public Response upload(@PathParam("subjectClassKey") String subjectClassKey,
300 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700301 @PathParam("configKey") String configKey,
302 InputStream request) throws IOException {
303 NetworkConfigService service = get(NetworkConfigService.class);
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700304 JsonNode root = mapper().readTree(request);
deepada611462017-06-02 09:22:55 +0530305 SubjectFactory subjectFactory =
306 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
307 subjectClassNotValidErrorString(subjectClassKey));
308 service.applyConfig(subjectClassKey, subjectFactory.createSubject(subjectKey),
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800309 configKey, root);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700310 return Response.ok().build();
311 }
312
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530313 private List<String> consumeJson(NetworkConfigService service, ObjectNode classNode,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700314 SubjectFactory subjectFactory) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530315 List<String> errorMsgs = new ArrayList<String>();
316 classNode.fieldNames().forEachRemaining(s -> {
317 List<String> error = consumeSubjectJson(service, (ObjectNode) classNode.path(s),
318 subjectFactory.createSubject(s),
319 subjectFactory.subjectClassKey());
320 errorMsgs.addAll(error);
321 });
322 return errorMsgs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700323 }
324
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530325 private List<String> consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -0700326 ObjectNode subjectNode, Object subject,
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800327 String subjectClassKey) {
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530328 List<String> errorMsgs = new ArrayList<String>();
329 subjectNode.fieldNames().forEachRemaining(configKey -> {
330 try {
331 service.applyConfig(subjectClassKey, subject, configKey, subjectNode.path(configKey));
332 } catch (IllegalArgumentException e) {
333 errorMsgs.add("Error parsing config " + subjectClassKey + "/" + subject + "/" + configKey);
deepada611462017-06-02 09:22:55 +0530334 } catch (InvalidConfigException exception) {
335 errorMsgs.add(exception.getMessage());
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530336 }
337 });
338 return errorMsgs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700339 }
340
Deepa Vaddireddy7fae1892016-06-15 17:13:15 +0530341 private ObjectNode produceErrorJson(List<String> errorMsgs) {
342 ObjectMapper mapper = new ObjectMapper();
343 ObjectNode result = mapper.createObjectNode().put("code", 207).putPOJO("message", errorMsgs);
344 return result;
345 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700346
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800347 // FIXME: Refactor to allow queued configs to be removed
348
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700349 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700350 * Clear entire network configuration base.
351 *
Jian Lic2a542b2016-05-10 11:48:19 -0700352 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700353 */
354 @DELETE
355 @SuppressWarnings("unchecked")
356 public Response delete() {
357 NetworkConfigService service = get(NetworkConfigService.class);
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530358 service.removeConfig();
Ray Milkey984e2d82016-06-16 14:33:10 -0700359 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700360 }
361
362 /**
363 * Clear all network configurations for a subject class.
364 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700365 * @param subjectClassKey subject class key
Ray Milkey984e2d82016-06-16 14:33:10 -0700366 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700367 */
368 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700369 @Path("{subjectClassKey}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700370 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700371 public Response delete(@PathParam("subjectClassKey") String subjectClassKey) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700372 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530373 SubjectFactory subjectFactory =
374 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
375 subjectClassNotValidErrorString(subjectClassKey));
376 service.getSubjects(subjectFactory.subjectClass())
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530377 .forEach(subject -> service.removeConfig(subject));
Ray Milkey984e2d82016-06-16 14:33:10 -0700378 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700379 }
380
381 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700382 * Clear all network configurations for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700383 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700384 * @param subjectClassKey subjectKey class key
385 * @param subjectKey subjectKey key
Ray Milkey984e2d82016-06-16 14:33:10 -0700386 * @return 204 NO CONTENT
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700387 */
388 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700389 @Path("{subjectClassKey}/{subjectKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700390 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700391 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700392 @PathParam("subjectKey") String subjectKey) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700393 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530394 SubjectFactory subjectFactory =
395 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
396 subjectClassNotValidErrorString(subjectClassKey));
397 service.removeConfig(subjectFactory.createSubject(subjectKey));
Ray Milkey984e2d82016-06-16 14:33:10 -0700398 return Response.noContent().build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700399 }
400
401 /**
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700402 * Clear specific network configuration for a subjectKey.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700403 *
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700404 * @param subjectClassKey subjectKey class key
405 * @param subjectKey subjectKey key
406 * @param configKey configuration class key
Ray Milkey984e2d82016-06-16 14:33:10 -0700407 * @return 204 NO CONTENT
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700408 */
409 @DELETE
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700410 @Path("{subjectClassKey}/{subjectKey}/{configKey}")
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700411 @SuppressWarnings("unchecked")
Ray Milkey984e2d82016-06-16 14:33:10 -0700412 public Response delete(@PathParam("subjectClassKey") String subjectClassKey,
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700413 @PathParam("subjectKey") String subjectKey,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700414 @PathParam("configKey") String configKey) {
415 NetworkConfigService service = get(NetworkConfigService.class);
deepada611462017-06-02 09:22:55 +0530416 SubjectFactory subjectFactory =
417 nullIsNotFound(service.getSubjectFactory(subjectClassKey),
418 subjectClassNotValidErrorString(subjectClassKey));
419 service.removeConfig(subjectClassKey, subjectFactory.createSubject(subjectKey),
420 configKey);
Ray Milkey984e2d82016-06-16 14:33:10 -0700421 return Response.noContent().build();
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700422 }
423
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700424}