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