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