blob: 07fe8a1409e9539cfd1911629ba737c3366ce36b [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
32/**
33 * REST resource for cluster-wide component configuration.
34 */
35@Path("configuration")
36public 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}