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