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