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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.cfg.ConfigProperty; |
| 21 | |
| 22 | import javax.ws.rs.DELETE; |
| 23 | import javax.ws.rs.GET; |
| 24 | import javax.ws.rs.POST; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.PathParam; |
| 27 | import javax.ws.rs.core.Response; |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | /** |
| 33 | * REST resource for cluster-wide component configuration. |
| 34 | */ |
| 35 | @Path("configuration") |
| 36 | public class ComponentConfigWebResource extends AbstractWebResource { |
| 37 | |
| 38 | @GET |
| 39 | public Response getComponentConfigs() { |
| 40 | ComponentConfigService service = get(ComponentConfigService.class); |
| 41 | Set<String> components = service.getComponentNames(); |
| 42 | ObjectNode root = mapper().createObjectNode(); |
| 43 | components.forEach(c -> encodeConfigs(c, service.getProperties(c), root)); |
| 44 | return ok(root).build(); |
| 45 | } |
| 46 | |
| 47 | @GET |
| 48 | @Path("{component}") |
| 49 | public Response getComponentConfigs(@PathParam("component") String component) { |
| 50 | ComponentConfigService service = get(ComponentConfigService.class); |
| 51 | ObjectNode root = mapper().createObjectNode(); |
| 52 | encodeConfigs(component, nullIsNotFound(service.getProperties(component), |
| 53 | "No such component"), root); |
| 54 | return ok(root).build(); |
| 55 | } |
| 56 | |
| 57 | // Encodes the specified properties as an object in the given node. |
| 58 | private void encodeConfigs(String component, Set<ConfigProperty> props, |
| 59 | ObjectNode node) { |
| 60 | ObjectNode compNode = mapper().createObjectNode(); |
| 61 | node.set(component, compNode); |
| 62 | props.forEach(p -> compNode.put(p.name(), p.value())); |
| 63 | } |
| 64 | |
| 65 | @POST |
| 66 | @Path("{component}") |
| 67 | public Response setComponentConfigs(@PathParam("component") String component, |
| 68 | InputStream request) throws IOException { |
| 69 | ComponentConfigService service = get(ComponentConfigService.class); |
| 70 | ObjectNode props = (ObjectNode) mapper().readTree(request); |
| 71 | props.fieldNames().forEachRemaining(k -> service.setProperty(component, k, |
| 72 | props.path(k).asText())); |
| 73 | return Response.noContent().build(); |
| 74 | } |
| 75 | |
| 76 | @DELETE |
| 77 | @Path("{component}") |
| 78 | public Response unsetComponentConfigs(@PathParam("component") String component, |
| 79 | InputStream request) throws IOException { |
| 80 | ComponentConfigService service = get(ComponentConfigService.class); |
| 81 | ObjectNode props = (ObjectNode) mapper().readTree(request); |
| 82 | props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k)); |
| 83 | return Response.noContent().build(); |
| 84 | } |
| 85 | } |