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