Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -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 | e684743 | 2015-04-03 23:56:46 -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 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.cfg.ConfigProperty; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 21 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 22 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 23 | import javax.ws.rs.Consumes; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 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; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 29 | import javax.ws.rs.Produces; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 30 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 31 | import javax.ws.rs.core.Response; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.util.Set; |
| 35 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 36 | import static org.onlab.util.Tools.nullIsNotFound; |
| 37 | |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 38 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 39 | * Manage component configurations. |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 40 | */ |
| 41 | @Path("configuration") |
| 42 | public class ComponentConfigWebResource extends AbstractWebResource { |
| 43 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 44 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 45 | * Gets all component configurations. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 46 | * Returns collection of all registered component configurations. |
| 47 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 48 | * @return 200 OK with a collection of component configurations |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 49 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 50 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 51 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 52 | public Response getComponentConfigs() { |
| 53 | ComponentConfigService service = get(ComponentConfigService.class); |
| 54 | Set<String> components = service.getComponentNames(); |
| 55 | ObjectNode root = mapper().createObjectNode(); |
| 56 | components.forEach(c -> encodeConfigs(c, service.getProperties(c), root)); |
| 57 | return ok(root).build(); |
| 58 | } |
| 59 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 60 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 61 | * Gets configuration of the specified component. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 62 | * |
| 63 | * @param component component name |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 64 | * @return 200 OK with a collection of component configurations |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 65 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 66 | @GET |
| 67 | @Path("{component}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 68 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 69 | public Response getComponentConfigs(@PathParam("component") String component) { |
| 70 | ComponentConfigService service = get(ComponentConfigService.class); |
| 71 | ObjectNode root = mapper().createObjectNode(); |
| 72 | encodeConfigs(component, nullIsNotFound(service.getProperties(component), |
| 73 | "No such component"), root); |
| 74 | return ok(root).build(); |
| 75 | } |
| 76 | |
| 77 | // Encodes the specified properties as an object in the given node. |
| 78 | private void encodeConfigs(String component, Set<ConfigProperty> props, |
| 79 | ObjectNode node) { |
| 80 | ObjectNode compNode = mapper().createObjectNode(); |
| 81 | node.set(component, compNode); |
| 82 | props.forEach(p -> compNode.put(p.name(), p.value())); |
| 83 | } |
| 84 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 85 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 86 | * Selectively sets configuration properties. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 87 | * Sets only the properties present in the JSON request. |
| 88 | * |
| 89 | * @param component component name |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 90 | * @param request JSON configuration |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 91 | * @return 200 OK |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 92 | * @throws IOException to signify bad request |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 93 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 94 | @POST |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 95 | @Consumes(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 96 | @Path("{component}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 97 | public Response setConfigs(@PathParam("component") String component, |
| 98 | InputStream request) throws IOException { |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 99 | ComponentConfigService service = get(ComponentConfigService.class); |
| 100 | ObjectNode props = (ObjectNode) mapper().readTree(request); |
| 101 | props.fieldNames().forEachRemaining(k -> service.setProperty(component, k, |
| 102 | props.path(k).asText())); |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 103 | return Response.ok().build(); |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 106 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 107 | * Selectively clears configuration properties. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 108 | * Clears only the properties present in the JSON request. |
| 109 | * |
| 110 | * @param component component name |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 111 | * @param request JSON configuration |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 112 | * @return 204 NO CONTENT |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 113 | * @throws IOException to signify bad request |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 114 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 115 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 116 | @Consumes(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 117 | @Path("{component}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 118 | public Response unsetConfigs(@PathParam("component") String component, |
| 119 | InputStream request) throws IOException { |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 120 | ComponentConfigService service = get(ComponentConfigService.class); |
| 121 | ObjectNode props = (ObjectNode) mapper().readTree(request); |
| 122 | props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k)); |
| 123 | return Response.noContent().build(); |
| 124 | } |
| 125 | } |