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; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 19 | import org.onosproject.net.config.NetworkConfigService; |
| 20 | import org.onosproject.net.config.SubjectFactory; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 36 | * Manage network configurations. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 37 | */ |
| 38 | @Path("network/configuration") |
| 39 | public class NetworkConfigWebResource extends AbstractWebResource { |
| 40 | |
| 41 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 42 | * Get entire network configuration base. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 58 | * Get all network configuration for a subject class. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 75 | * Get all network configuration for a subject. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -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 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 95 | * Get specific network configuration for a subject. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 96 | * |
| 97 | * @param subjectKey subject class key |
| 98 | * @param subject subject key |
| 99 | * @param configKey configuration class key |
| 100 | * @return network configuration JSON |
| 101 | */ |
| 102 | @GET |
| 103 | @Path("{subjectKey}/{subject}/{configKey}") |
| 104 | @Produces(MediaType.APPLICATION_JSON) |
| 105 | @SuppressWarnings("unchecked") |
| 106 | public Response download(@PathParam("subjectKey") String subjectKey, |
| 107 | @PathParam("subject") String subject, |
| 108 | @PathParam("configKey") String configKey) { |
| 109 | NetworkConfigService service = get(NetworkConfigService.class); |
| 110 | return ok(service.getConfig(service.getSubjectFactory(subjectKey).createSubject(subject), |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 111 | service.getConfigClass(subjectKey, configKey)).node()).build(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @SuppressWarnings("unchecked") |
| 115 | private void produceJson(NetworkConfigService service, ObjectNode node, |
| 116 | Class subjectClass) { |
| 117 | service.getSubjects(subjectClass).forEach(s -> |
| 118 | produceSubjectJson(service, newObject(node, s.toString()), s)); |
| 119 | } |
| 120 | |
| 121 | private void produceSubjectJson(NetworkConfigService service, ObjectNode node, |
| 122 | Object subject) { |
| 123 | service.getConfigs(subject).forEach(c -> node.set(c.key(), c.node())); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 128 | * Upload bulk network configuration. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 129 | * |
| 130 | * @param request network configuration JSON rooted at the top node |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 131 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 132 | * @return empty response |
| 133 | */ |
| 134 | @POST |
| 135 | @Consumes(MediaType.APPLICATION_JSON) |
| 136 | @SuppressWarnings("unchecked") |
| 137 | public Response upload(InputStream request) throws IOException { |
| 138 | NetworkConfigService service = get(NetworkConfigService.class); |
| 139 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 140 | root.fieldNames() |
| 141 | .forEachRemaining(sk -> consumeJson(service, (ObjectNode) root.path(sk), |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 142 | service.getSubjectFactory(sk))); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 143 | return Response.ok().build(); |
| 144 | } |
| 145 | |
| 146 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 147 | * Upload multiple network configurations for a subject class. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 148 | * |
| 149 | * @param subjectKey subject class key |
| 150 | * @param request network configuration JSON rooted at the top node |
| 151 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 152 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 153 | */ |
| 154 | @POST |
| 155 | @Path("{subjectKey}") |
| 156 | @Consumes(MediaType.APPLICATION_JSON) |
| 157 | @SuppressWarnings("unchecked") |
| 158 | public Response upload(@PathParam("subjectKey") String subjectKey, |
| 159 | InputStream request) throws IOException { |
| 160 | NetworkConfigService service = get(NetworkConfigService.class); |
| 161 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 162 | consumeJson(service, root, service.getSubjectFactory(subjectKey)); |
| 163 | return Response.ok().build(); |
| 164 | } |
| 165 | |
| 166 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 167 | * Upload mutliple network configurations for a subject. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 168 | * |
| 169 | * @param subjectKey subject class key |
| 170 | * @param subject subject key |
| 171 | * @param request network configuration JSON rooted at the top node |
| 172 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 173 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 174 | */ |
| 175 | @POST |
| 176 | @Path("{subjectKey}/{subject}") |
| 177 | @Consumes(MediaType.APPLICATION_JSON) |
| 178 | @SuppressWarnings("unchecked") |
| 179 | public Response upload(@PathParam("subjectKey") String subjectKey, |
| 180 | @PathParam("subject") String subject, |
| 181 | InputStream request) throws IOException { |
| 182 | NetworkConfigService service = get(NetworkConfigService.class); |
| 183 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 184 | consumeSubjectJson(service, root, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 185 | service.getSubjectFactory(subjectKey).createSubject(subject), |
| 186 | subjectKey); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 187 | return Response.ok().build(); |
| 188 | } |
| 189 | |
| 190 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 191 | * Upload specific network configuration for a subject. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 192 | * |
| 193 | * @param subjectKey subject class key |
| 194 | * @param subject subject key |
| 195 | * @param configKey configuration class key |
| 196 | * @param request network configuration JSON rooted at the top node |
| 197 | * @return empty response |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 198 | * @throws IOException if unable to parse the request |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 199 | */ |
| 200 | @POST |
| 201 | @Path("{subjectKey}/{subject}/{configKey}") |
| 202 | @Consumes(MediaType.APPLICATION_JSON) |
| 203 | @SuppressWarnings("unchecked") |
| 204 | public Response upload(@PathParam("subjectKey") String subjectKey, |
| 205 | @PathParam("subject") String subject, |
| 206 | @PathParam("configKey") String configKey, |
| 207 | InputStream request) throws IOException { |
| 208 | NetworkConfigService service = get(NetworkConfigService.class); |
| 209 | ObjectNode root = (ObjectNode) mapper().readTree(request); |
| 210 | service.applyConfig(service.getSubjectFactory(subjectKey).createSubject(subject), |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 211 | service.getConfigClass(subjectKey, configKey), root); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 212 | return Response.ok().build(); |
| 213 | } |
| 214 | |
| 215 | private void consumeJson(NetworkConfigService service, ObjectNode classNode, |
| 216 | SubjectFactory subjectFactory) { |
| 217 | classNode.fieldNames().forEachRemaining(s -> |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 218 | consumeSubjectJson(service, (ObjectNode) classNode.path(s), |
| 219 | subjectFactory.createSubject(s), |
| 220 | subjectFactory.subjectKey())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | private void consumeSubjectJson(NetworkConfigService service, |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 224 | ObjectNode subjectNode, Object subject, |
| 225 | String subjectKey) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 226 | subjectNode.fieldNames().forEachRemaining(c -> |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 227 | service.applyConfig(subject, service.getConfigClass(subjectKey, c), |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 228 | (ObjectNode) subjectNode.path(c))); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 233 | * Clear entire network configuration base. |
| 234 | * |
| 235 | * @return empty response |
| 236 | */ |
| 237 | @DELETE |
| 238 | @SuppressWarnings("unchecked") |
| 239 | public Response delete() { |
| 240 | NetworkConfigService service = get(NetworkConfigService.class); |
| 241 | service.getSubjectClasses() |
| 242 | .forEach(subjectClass -> service.getSubjects(subjectClass) |
| 243 | .forEach(subject -> service.getConfigs(subject) |
| 244 | .forEach(config -> service |
| 245 | .removeConfig(subject, config.getClass())))); |
| 246 | return Response.ok().build(); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Clear all network configurations for a subject class. |
| 251 | * |
| 252 | * @param subjectKey subject class key |
| 253 | * @return empty response |
| 254 | */ |
| 255 | @DELETE |
| 256 | @Path("{subjectKey}") |
| 257 | @SuppressWarnings("unchecked") |
| 258 | public Response delete(@PathParam("subjectKey") String subjectKey) { |
| 259 | NetworkConfigService service = get(NetworkConfigService.class); |
| 260 | service.getSubjects(service.getSubjectFactory(subjectKey).getClass()) |
| 261 | .forEach(subject -> service.getConfigs(subject) |
| 262 | .forEach(config -> service |
| 263 | .removeConfig(subject, config.getClass()))); |
| 264 | return Response.ok().build(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Clear all network configurations for a subject. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 269 | * |
| 270 | * @param subjectKey subject class key |
| 271 | * @param subject subject key |
| 272 | * @return empty response |
| 273 | */ |
| 274 | @DELETE |
| 275 | @Path("{subjectKey}/{subject}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 276 | @SuppressWarnings("unchecked") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 277 | public Response delete(@PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 278 | @PathParam("subject") String subject) { |
| 279 | NetworkConfigService service = get(NetworkConfigService.class); |
| 280 | Object s = service.getSubjectFactory(subjectKey).createSubject(subject); |
| 281 | service.getConfigs(s).forEach(c -> service.removeConfig(s, c.getClass())); |
| 282 | return Response.ok().build(); |
| 283 | } |
| 284 | |
| 285 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 286 | * Clear specific network configuration for a subject. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 287 | * |
| 288 | * @param subjectKey subject class key |
| 289 | * @param subject subject key |
| 290 | * @param configKey configuration class key |
| 291 | * @return empty response |
| 292 | */ |
| 293 | @DELETE |
| 294 | @Path("{subjectKey}/{subject}/{configKey}") |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 295 | @SuppressWarnings("unchecked") |
Sahil Lele | 372d1f3 | 2015-07-31 15:01:41 -0700 | [diff] [blame] | 296 | public Response delete(@PathParam("subjectKey") String subjectKey, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 297 | @PathParam("subject") String subject, |
| 298 | @PathParam("configKey") String configKey) { |
| 299 | NetworkConfigService service = get(NetworkConfigService.class); |
| 300 | service.removeConfig(service.getSubjectFactory(subjectKey).createSubject(subject), |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame^] | 301 | service.getConfigClass(subjectKey, configKey)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 302 | return Response.ok().build(); |
| 303 | } |
| 304 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 305 | } |