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