Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.util.Set; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 21 | |
| 22 | import javax.ws.rs.Consumes; |
| 23 | import javax.ws.rs.DELETE; |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.POST; |
| 26 | import javax.ws.rs.Path; |
| 27 | import javax.ws.rs.PathParam; |
| 28 | import javax.ws.rs.Produces; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 31 | |
| 32 | import org.onosproject.net.config.Config; |
| 33 | import org.onosproject.net.config.NetworkConfigService; |
| 34 | import org.onosproject.net.config.SubjectFactory; |
| 35 | import org.onosproject.rest.AbstractWebResource; |
| 36 | |
| 37 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 38 | |
| 39 | import static org.onlab.util.Tools.emptyIsNotFound; |
| 40 | import static org.onlab.util.Tools.nullIsNotFound; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 43 | * Manage network configurations. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 44 | */ |
| 45 | @Path("network/configuration") |
| 46 | public class NetworkConfigWebResource extends AbstractWebResource { |
| 47 | |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 48 | |
| 49 | private String subjectClassNotFoundErrorString(String subjectClassKey) { |
| 50 | return "Config for '" + subjectClassKey + "' not found"; |
| 51 | } |
| 52 | |
| 53 | private String subjectNotFoundErrorString(String subjectClassKey, |
| 54 | String subjectKey) { |
| 55 | return "Config for '" |
| 56 | + subjectClassKey + "/" + subjectKey |
| 57 | + "' not found"; |
| 58 | } |
| 59 | |
| 60 | private String configKeyNotFoundErrorString(String subjectClassKey, |
| 61 | String subjectKey, |
| 62 | String configKey) { |
| 63 | return "Config for '" |
| 64 | + subjectClassKey + "/" + subjectKey + "/" + configKey |
| 65 | + "' not found"; |
| 66 | } |
| 67 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 68 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 69 | * Get entire network configuration base. |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 70 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 71 | * @return network configuration JSON |
| 72 | */ |
| 73 | @GET |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | @SuppressWarnings("unchecked") |
| 76 | public Response download() { |
| 77 | NetworkConfigService service = get(NetworkConfigService.class); |
| 78 | ObjectNode root = mapper().createObjectNode(); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 79 | service.getSubjectClasses().forEach(sc -> { |
| 80 | SubjectFactory subjectFactory = service.getSubjectFactory(sc); |
| 81 | produceJson(service, newObject(root, subjectFactory.subjectClassKey()), |
| 82 | subjectFactory, sc); |
| 83 | }); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 84 | return ok(root).build(); |
| 85 | } |
| 86 | |
| 87 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 88 | * Get all network configuration for a subject class. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 89 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 90 | * @param subjectClassKey subject class key |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | * @return network configuration JSON |
| 92 | */ |
| 93 | @GET |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 94 | @Path("{subjectClassKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 95 | @Produces(MediaType.APPLICATION_JSON) |
| 96 | @SuppressWarnings("unchecked") |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 97 | public Response download(@PathParam("subjectClassKey") String subjectClassKey) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 98 | NetworkConfigService service = get(NetworkConfigService.class); |
| 99 | ObjectNode root = mapper().createObjectNode(); |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 100 | SubjectFactory subjectFactory = |
| 101 | nullIsNotFound(service.getSubjectFactory(subjectClassKey), |
| 102 | subjectClassNotFoundErrorString(subjectClassKey)); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 103 | produceJson(service, root, subjectFactory, subjectFactory.subjectClass()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 104 | return ok(root).build(); |
| 105 | } |
| 106 | |
| 107 | /** |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 108 | * Get all network configuration for a subjectKey. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 109 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 110 | * @param subjectClassKey subjectKey class key |
| 111 | * @param subjectKey subjectKey key |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 112 | * @return network configuration JSON |
| 113 | */ |
| 114 | @GET |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 115 | @Path("{subjectClassKey}/{subjectKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 116 | @Produces(MediaType.APPLICATION_JSON) |
| 117 | @SuppressWarnings("unchecked") |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 118 | public Response download(@PathParam("subjectClassKey") String subjectClassKey, |
| 119 | @PathParam("subjectKey") String subjectKey) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 120 | NetworkConfigService service = get(NetworkConfigService.class); |
| 121 | ObjectNode root = mapper().createObjectNode(); |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 122 | SubjectFactory subjectFactory = |
| 123 | nullIsNotFound(service.getSubjectFactory(subjectClassKey), |
| 124 | subjectClassNotFoundErrorString(subjectClassKey)); |
| 125 | produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey), |
| 126 | true, |
| 127 | subjectNotFoundErrorString(subjectClassKey, subjectKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 128 | return ok(root).build(); |
| 129 | } |
| 130 | |
| 131 | /** |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 132 | * Get specific network configuration for a subjectKey. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 133 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 134 | * @param subjectClassKey subjectKey class key |
| 135 | * @param subjectKey subjectKey key |
| 136 | * @param configKey configuration class key |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 137 | * @return network configuration JSON |
| 138 | */ |
| 139 | @GET |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 140 | @Path("{subjectClassKey}/{subjectKey}/{configKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 141 | @Produces(MediaType.APPLICATION_JSON) |
| 142 | @SuppressWarnings("unchecked") |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 143 | public Response download(@PathParam("subjectClassKey") String subjectClassKey, |
| 144 | @PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 145 | @PathParam("configKey") String configKey) { |
| 146 | NetworkConfigService service = get(NetworkConfigService.class); |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 147 | |
| 148 | Object subject = |
| 149 | nullIsNotFound(service.getSubjectFactory(subjectClassKey) |
| 150 | .createSubject(subjectKey), |
| 151 | subjectNotFoundErrorString(subjectClassKey, subjectKey)); |
| 152 | |
| 153 | Class configClass = |
| 154 | nullIsNotFound(service.getConfigClass(subjectClassKey, configKey), |
| 155 | configKeyNotFoundErrorString(subjectClassKey, subjectKey, configKey)); |
| 156 | Config config = |
| 157 | nullIsNotFound(service.getConfig(subject, configClass), |
| 158 | configKeyNotFoundErrorString(subjectClassKey, |
| 159 | subjectKey, |
| 160 | configKey)); |
| 161 | return ok(config.node()).build(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @SuppressWarnings("unchecked") |
| 165 | private void produceJson(NetworkConfigService service, ObjectNode node, |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 166 | SubjectFactory subjectFactory, Class subjectClass) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 167 | service.getSubjects(subjectClass).forEach(s -> |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 168 | produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s, false, "")); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | private void produceSubjectJson(NetworkConfigService service, ObjectNode node, |
Ray Milkey | 36992c8 | 2015-11-17 13:31:15 -0800 | [diff] [blame] | 172 | Object subject, |
| 173 | boolean emptyIsError, |
| 174 | String emptyErrorMessage) { |
| 175 | Set<? extends Config<Object>> configs = service.getConfigs(subject); |
| 176 | if (emptyIsError) { |
| 177 | // caller wants an empty set to be a 404 |
| 178 | configs = emptyIsNotFound(configs, emptyErrorMessage); |
| 179 | } |
| 180 | configs.forEach(c -> node.set(c.key(), c.node())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 185 | * Upload bulk network configuration. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 186 | * |
| 187 | * @param request network configuration JSON rooted at the top node |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 188 | * @return empty response |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 189 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 190 | */ |
| 191 | @POST |
| 192 | @Consumes(MediaType.APPLICATION_JSON) |
| 193 | @SuppressWarnings("unchecked") |
| 194 | public Response upload(InputStream request) throws IOException { |
| 195 | NetworkConfigService service = get(NetworkConfigService.class); |
| 196 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 197 | root.fieldNames() |
| 198 | .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk), |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 199 | service.getSubjectFactory(sk))); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 200 | return Response.ok().build(); |
| 201 | } |
| 202 | |
| 203 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 204 | * Upload multiple network configurations for a subject class. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 205 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 206 | * @param subjectClassKey subject class key |
| 207 | * @param request network configuration JSON rooted at the top node |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 208 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 209 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 210 | */ |
| 211 | @POST |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 212 | @Path("{subjectClassKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 213 | @Consumes(MediaType.APPLICATION_JSON) |
| 214 | @SuppressWarnings("unchecked") |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 215 | public Response upload(@PathParam("subjectClassKey") String subjectClassKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 216 | InputStream request) throws IOException { |
| 217 | NetworkConfigService service = get(NetworkConfigService.class); |
| 218 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 219 | consumeJson(service, root, service.getSubjectFactory(subjectClassKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 220 | return Response.ok().build(); |
| 221 | } |
| 222 | |
| 223 | /** |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 224 | * Upload mutliple network configurations for a subjectKey. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 225 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 226 | * @param subjectClassKey subjectKey class key |
| 227 | * @param subjectKey subjectKey key |
| 228 | * @param request network configuration JSON rooted at the top node |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 229 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 230 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 231 | */ |
| 232 | @POST |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 233 | @Path("{subjectClassKey}/{subjectKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 234 | @Consumes(MediaType.APPLICATION_JSON) |
| 235 | @SuppressWarnings("unchecked") |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 236 | public Response upload(@PathParam("subjectClassKey") String subjectClassKey, |
| 237 | @PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 238 | InputStream request) throws IOException { |
| 239 | NetworkConfigService service = get(NetworkConfigService.class); |
| 240 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 241 | consumeSubjectJson(service, root, |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 242 | service.getSubjectFactory(subjectClassKey).createSubject(subjectKey), |
| 243 | subjectClassKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 244 | return Response.ok().build(); |
| 245 | } |
| 246 | |
| 247 | /** |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 248 | * Upload specific network configuration for a subjectKey. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 249 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 250 | * @param subjectClassKey subjectKey class key |
| 251 | * @param subjectKey subjectKey key |
| 252 | * @param configKey configuration class key |
| 253 | * @param request network configuration JSON rooted at the top node |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 254 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 255 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 256 | */ |
| 257 | @POST |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 258 | @Path("{subjectClassKey}/{subjectKey}/{configKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 259 | @Consumes(MediaType.APPLICATION_JSON) |
| 260 | @SuppressWarnings("unchecked") |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 261 | public Response upload(@PathParam("subjectClassKey") String subjectClassKey, |
| 262 | @PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 263 | @PathParam("configKey") String configKey, |
| 264 | InputStream request) throws IOException { |
| 265 | NetworkConfigService service = get(NetworkConfigService.class); |
| 266 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 267 | service.applyConfig(subjectClassKey, |
| 268 | service.getSubjectFactory(subjectClassKey).createSubject(subjectKey), |
| 269 | configKey, root); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 270 | return Response.ok().build(); |
| 271 | } |
| 272 | |
| 273 | private void consumeJson(NetworkConfigService service, ObjectNode classNode, |
| 274 | SubjectFactory subjectFactory) { |
| 275 | classNode.fieldNames().forEachRemaining(s -> |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 276 | consumeSubjectJson(service, (ObjectNode) classNode.path(s), |
| 277 | subjectFactory.createSubject(s), |
| 278 | subjectFactory.subjectClassKey())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | private void consumeSubjectJson(NetworkConfigService service, |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 282 | ObjectNode subjectNode, Object subject, |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 283 | String subjectClassKey) { |
| 284 | subjectNode.fieldNames().forEachRemaining(configKey -> |
| 285 | service.applyConfig(subjectClassKey, subject, configKey, subjectNode.path(configKey))); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 289 | // FIXME: Refactor to allow queued configs to be removed |
| 290 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 291 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 292 | * Clear entire network configuration base. |
| 293 | * |
| 294 | * @return empty response |
| 295 | */ |
| 296 | @DELETE |
| 297 | @SuppressWarnings("unchecked") |
| 298 | public Response delete() { |
| 299 | NetworkConfigService service = get(NetworkConfigService.class); |
| 300 | service.getSubjectClasses() |
| 301 | .forEach(subjectClass -> service.getSubjects(subjectClass) |
| 302 | .forEach(subject -> service.getConfigs(subject) |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 303 | .forEach(config -> service.removeConfig(subject, config.getClass())))); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 304 | return Response.ok().build(); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Clear all network configurations for a subject class. |
| 309 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 310 | * @param subjectClassKey subject class key |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 311 | */ |
| 312 | @DELETE |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 313 | @Path("{subjectClassKey}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 314 | @SuppressWarnings("unchecked") |
Ray Milkey | b9fe25d | 2015-11-18 15:54:44 -0800 | [diff] [blame] | 315 | public void delete(@PathParam("subjectClassKey") String subjectClassKey) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 316 | NetworkConfigService service = get(NetworkConfigService.class); |
Ray Milkey | b9fe25d | 2015-11-18 15:54:44 -0800 | [diff] [blame] | 317 | service.getSubjects(service.getSubjectFactory(subjectClassKey).subjectClass()) |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 318 | .forEach(subject -> service.getConfigs(subject) |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 319 | .forEach(config -> service.removeConfig(subject, config.getClass()))); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 323 | * Clear all network configurations for a subjectKey. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 324 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 325 | * @param subjectClassKey subjectKey class key |
| 326 | * @param subjectKey subjectKey key |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 327 | */ |
| 328 | @DELETE |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 329 | @Path("{subjectClassKey}/{subjectKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 330 | @SuppressWarnings("unchecked") |
Ray Milkey | b9fe25d | 2015-11-18 15:54:44 -0800 | [diff] [blame] | 331 | public void delete(@PathParam("subjectClassKey") String subjectClassKey, |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 332 | @PathParam("subjectKey") String subjectKey) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 333 | NetworkConfigService service = get(NetworkConfigService.class); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 334 | Object s = service.getSubjectFactory(subjectClassKey).createSubject(subjectKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 335 | service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 339 | * Clear specific network configuration for a subjectKey. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 340 | * |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 341 | * @param subjectClassKey subjectKey class key |
| 342 | * @param subjectKey subjectKey key |
| 343 | * @param configKey configuration class key |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 344 | */ |
| 345 | @DELETE |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 346 | @Path("{subjectClassKey}/{subjectKey}/{configKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 347 | @SuppressWarnings("unchecked") |
Ray Milkey | b9fe25d | 2015-11-18 15:54:44 -0800 | [diff] [blame] | 348 | public void delete(@PathParam("subjectClassKey") String subjectClassKey, |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 349 | @PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 350 | @PathParam("configKey") String configKey) { |
| 351 | NetworkConfigService service = get(NetworkConfigService.class); |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 352 | service.removeConfig(service.getSubjectFactory(subjectClassKey).createSubject(subjectKey), |
| 353 | service.getConfigClass(subjectClassKey, configKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 356 | } |