Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.incubator.net.config.NetworkConfigService; |
| 20 | import org.onosproject.incubator.net.config.SubjectFactory; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
| 22 | |
| 23 | import javax.ws.rs.Consumes; |
| 24 | import javax.ws.rs.DELETE; |
| 25 | import javax.ws.rs.GET; |
| 26 | import javax.ws.rs.POST; |
| 27 | import javax.ws.rs.Path; |
| 28 | import javax.ws.rs.PathParam; |
| 29 | import javax.ws.rs.Produces; |
| 30 | import javax.ws.rs.core.MediaType; |
| 31 | import javax.ws.rs.core.Response; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | |
| 35 | /** |
| 36 | * REST resource for injecting and retrieving common network configuration. |
| 37 | */ |
| 38 | @Path("network/configuration") |
| 39 | public class NetworkConfigWebResource extends AbstractWebResource { |
| 40 | |
| 41 | /** |
| 42 | * Returns entire network configuration base. |
| 43 | * |
| 44 | * @return network configuration JSON |
| 45 | */ |
| 46 | @GET |
| 47 | @Produces(MediaType.APPLICATION_JSON) |
| 48 | @SuppressWarnings("unchecked") |
| 49 | public Response download() { |
| 50 | NetworkConfigService service = get(NetworkConfigService.class); |
| 51 | ObjectNode root = mapper().createObjectNode(); |
| 52 | service.getSubjectClasses().forEach(sc -> |
| 53 | produceJson(service, newObject(root, service.getSubjectFactory(sc).subjectKey()), sc)); |
| 54 | return ok(root).build(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the network configuration for the specified subject class. |
| 59 | * |
| 60 | * @param subjectKey subject class key |
| 61 | * @return network configuration JSON |
| 62 | */ |
| 63 | @GET |
| 64 | @Path("{subjectKey}") |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
| 66 | @SuppressWarnings("unchecked") |
| 67 | public Response download(@PathParam("subjectKey") String subjectKey) { |
| 68 | NetworkConfigService service = get(NetworkConfigService.class); |
| 69 | ObjectNode root = mapper().createObjectNode(); |
| 70 | produceJson(service, root, service.getSubjectFactory(subjectKey).subjectClass()); |
| 71 | return ok(root).build(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the network configuration for the specified subject. |
| 76 | * |
| 77 | * @param subjectKey subject class key |
| 78 | * @param subject subject key |
| 79 | * @return network configuration JSON |
| 80 | */ |
| 81 | @GET |
| 82 | @Path("{subjectKey}/{subject}") |
| 83 | @Produces(MediaType.APPLICATION_JSON) |
| 84 | @SuppressWarnings("unchecked") |
| 85 | public Response download(@PathParam("subjectKey") String subjectKey, |
| 86 | @PathParam("subject") String subject) { |
| 87 | NetworkConfigService service = get(NetworkConfigService.class); |
| 88 | ObjectNode root = mapper().createObjectNode(); |
| 89 | produceSubjectJson(service, root, |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 90 | service.getSubjectFactory(subjectKey).createSubject(subject)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | return ok(root).build(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the network configuration for the specified subject and given |
| 96 | * configuration class. |
| 97 | * |
| 98 | * @param subjectKey subject class key |
| 99 | * @param subject subject key |
| 100 | * @param configKey configuration class key |
| 101 | * @return network configuration JSON |
| 102 | */ |
| 103 | @GET |
| 104 | @Path("{subjectKey}/{subject}/{configKey}") |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
| 106 | @SuppressWarnings("unchecked") |
| 107 | public Response download(@PathParam("subjectKey") String subjectKey, |
| 108 | @PathParam("subject") String subject, |
| 109 | @PathParam("configKey") String configKey) { |
| 110 | NetworkConfigService service = get(NetworkConfigService.class); |
| 111 | return ok(service.getConfig(service.getSubjectFactory(subjectKey).createSubject(subject), |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 112 | service.getConfigClass(subjectKey, configKey)).node()).build(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @SuppressWarnings("unchecked") |
| 116 | private void produceJson(NetworkConfigService service, ObjectNode node, |
| 117 | Class subjectClass) { |
| 118 | service.getSubjects(subjectClass).forEach(s -> |
| 119 | produceSubjectJson(service, newObject(node, s.toString()), s)); |
| 120 | } |
| 121 | |
| 122 | private void produceSubjectJson(NetworkConfigService service, ObjectNode node, |
| 123 | Object subject) { |
| 124 | service.getConfigs(subject).forEach(c -> node.set(c.key(), c.node())); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Uploads network configuration in bulk. |
| 130 | * |
| 131 | * @param request network configuration JSON rooted at the top node |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 132 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 133 | * @return empty response |
| 134 | */ |
| 135 | @POST |
| 136 | @Consumes(MediaType.APPLICATION_JSON) |
| 137 | @SuppressWarnings("unchecked") |
| 138 | public Response upload(InputStream request) throws IOException { |
| 139 | NetworkConfigService service = get(NetworkConfigService.class); |
| 140 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 141 | root.fieldNames() |
| 142 | .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk), |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 143 | service.getSubjectFactory(sk))); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 144 | return Response.ok().build(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Uploads network configuration for the specified subject class. |
| 149 | * |
| 150 | * @param subjectKey subject class key |
| 151 | * @param request network configuration JSON rooted at the top node |
| 152 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 153 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 154 | */ |
| 155 | @POST |
| 156 | @Path("{subjectKey}") |
| 157 | @Consumes(MediaType.APPLICATION_JSON) |
| 158 | @SuppressWarnings("unchecked") |
| 159 | public Response upload(@PathParam("subjectKey") String subjectKey, |
| 160 | InputStream request) throws IOException { |
| 161 | NetworkConfigService service = get(NetworkConfigService.class); |
| 162 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 163 | consumeJson(service, root, service.getSubjectFactory(subjectKey)); |
| 164 | return Response.ok().build(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Uploads network configuration for the specified subject. |
| 169 | * |
| 170 | * @param subjectKey subject class key |
| 171 | * @param subject subject key |
| 172 | * @param request network configuration JSON rooted at the top node |
| 173 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 174 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 175 | */ |
| 176 | @POST |
| 177 | @Path("{subjectKey}/{subject}") |
| 178 | @Consumes(MediaType.APPLICATION_JSON) |
| 179 | @SuppressWarnings("unchecked") |
| 180 | public Response upload(@PathParam("subjectKey") String subjectKey, |
| 181 | @PathParam("subject") String subject, |
| 182 | InputStream request) throws IOException { |
| 183 | NetworkConfigService service = get(NetworkConfigService.class); |
| 184 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 185 | consumeSubjectJson(service, root, |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 186 | service.getSubjectFactory(subjectKey).createSubject(subject), |
| 187 | subjectKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 188 | return Response.ok().build(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Uploads network configuration for the specified subject and given |
| 193 | * configuration class. |
| 194 | * |
| 195 | * @param subjectKey subject class key |
| 196 | * @param subject subject key |
| 197 | * @param configKey configuration class key |
| 198 | * @param request network configuration JSON rooted at the top node |
| 199 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 200 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 201 | */ |
| 202 | @POST |
| 203 | @Path("{subjectKey}/{subject}/{configKey}") |
| 204 | @Consumes(MediaType.APPLICATION_JSON) |
| 205 | @SuppressWarnings("unchecked") |
| 206 | public Response upload(@PathParam("subjectKey") String subjectKey, |
| 207 | @PathParam("subject") String subject, |
| 208 | @PathParam("configKey") String configKey, |
| 209 | InputStream request) throws IOException { |
| 210 | NetworkConfigService service = get(NetworkConfigService.class); |
| 211 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 212 | service.applyConfig(service.getSubjectFactory(subjectKey).createSubject(subject), |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 213 | service.getConfigClass(subjectKey, configKey), root); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 214 | return Response.ok().build(); |
| 215 | } |
| 216 | |
| 217 | private void consumeJson(NetworkConfigService service, ObjectNode classNode, |
| 218 | SubjectFactory subjectFactory) { |
| 219 | classNode.fieldNames().forEachRemaining(s -> |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 220 | consumeSubjectJson(service, (ObjectNode) classNode.path(s), |
| 221 | subjectFactory.createSubject(s), |
| 222 | subjectFactory.subjectKey())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | private void consumeSubjectJson(NetworkConfigService service, |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 226 | ObjectNode subjectNode, Object subject, |
| 227 | String subjectKey) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 228 | subjectNode.fieldNames().forEachRemaining(c -> |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 229 | service.applyConfig(subject, service.getConfigClass(subjectKey, c), |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 230 | (ObjectNode) subjectNode.path(c))); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | /** |
| 235 | * Clears network configuration for the specified subject. |
| 236 | * |
| 237 | * @param subjectKey subject class key |
| 238 | * @param subject subject key |
| 239 | * @return empty response |
| 240 | */ |
| 241 | @DELETE |
| 242 | @Path("{subjectKey}/{subject}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 243 | @SuppressWarnings("unchecked") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 244 | public Response delete(@PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 245 | @PathParam("subject") String subject) { |
| 246 | NetworkConfigService service = get(NetworkConfigService.class); |
| 247 | Object s = service.getSubjectFactory(subjectKey).createSubject(subject); |
| 248 | service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass())); |
| 249 | return Response.ok().build(); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Clears network configuration for the specified subject and given |
| 254 | * configuration class. |
| 255 | * |
| 256 | * @param subjectKey subject class key |
| 257 | * @param subject subject key |
| 258 | * @param configKey configuration class key |
| 259 | * @return empty response |
| 260 | */ |
| 261 | @DELETE |
| 262 | @Path("{subjectKey}/{subject}/{configKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 263 | @SuppressWarnings("unchecked") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 264 | public Response delete(@PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 265 | @PathParam("subject") String subject, |
| 266 | @PathParam("configKey") String configKey) { |
| 267 | NetworkConfigService service = get(NetworkConfigService.class); |
| 268 | service.removeConfig(service.getSubjectFactory(subjectKey).createSubject(subject), |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 269 | service.getConfigClass(subjectKey, configKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 270 | return Response.ok().build(); |
| 271 | } |
| 272 | |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Clears all network configurations. |
| 276 | * |
| 277 | * @return empty response |
| 278 | */ |
| 279 | @DELETE |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 280 | @SuppressWarnings("unchecked") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 281 | public Response delete() { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 282 | NetworkConfigService service = get(NetworkConfigService.class); |
| 283 | service.getSubjectClasses().forEach(subjectClass -> { |
| 284 | service.getSubjects(subjectClass).forEach(subject -> { |
| 285 | service.getConfigs(subject).forEach(config -> { |
| 286 | service.removeConfig(subject, config.getClass()); |
| 287 | }); |
| 288 | }); |
| 289 | }); |
| 290 | return Response.ok().build(); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | // TODO: this one below doesn't work correctly |
| 295 | /** |
| 296 | * Clears network configuration for the specified subject class. |
| 297 | * |
| 298 | * @param subjectKey subject class key |
| 299 | * @return empty response |
| 300 | */ |
| 301 | @DELETE |
| 302 | @Path("{subjectKey}/") |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 303 | @SuppressWarnings("unchecked") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 304 | public Response delete(@PathParam("subjectKey") String subjectKey) { |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 305 | NetworkConfigService service = get(NetworkConfigService.class); |
| 306 | service.getSubjects(service.getSubjectFactory(subjectKey).getClass()).forEach(subject -> { |
| 307 | service.getConfigs(subject).forEach(config -> { |
| 308 | service.removeConfig(subject, config.getClass()); |
| 309 | }); |
| 310 | }); |
| 311 | return Response.ok().build(); |
| 312 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 313 | } |