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