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