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