blob: 4adb6c574cce5902a379354f21791e3eb2557105 [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/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070036 * Manage component configurations.
Thomas Vachuskae6847432015-04-03 23:56:46 -070037 */
38@Path("configuration")
39public class ComponentConfigWebResource extends AbstractWebResource {
40
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070041 /**
42 * Get all component configurations.
43 * Returns collection of all registered component configurations.
44 *
45 * @return 200 OK
46 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070047 @GET
48 public Response getComponentConfigs() {
49 ComponentConfigService service = get(ComponentConfigService.class);
50 Set<String> components = service.getComponentNames();
51 ObjectNode root = mapper().createObjectNode();
52 components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
53 return ok(root).build();
54 }
55
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070056 /**
57 * Get configuration of the specified component.
58 *
59 * @param component component name
60 * @return 200 OK
61 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070062 @GET
63 @Path("{component}")
64 public Response getComponentConfigs(@PathParam("component") String component) {
65 ComponentConfigService service = get(ComponentConfigService.class);
66 ObjectNode root = mapper().createObjectNode();
67 encodeConfigs(component, nullIsNotFound(service.getProperties(component),
68 "No such component"), root);
69 return ok(root).build();
70 }
71
72 // Encodes the specified properties as an object in the given node.
73 private void encodeConfigs(String component, Set<ConfigProperty> props,
74 ObjectNode node) {
75 ObjectNode compNode = mapper().createObjectNode();
76 node.set(component, compNode);
77 props.forEach(p -> compNode.put(p.name(), p.value()));
78 }
79
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070080 /**
81 * Selectively set configuration properties.
82 * Sets only the properties present in the JSON request.
83 *
84 * @param component component name
85 * @return 200 OK
86 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070087 @POST
88 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070089 public Response setConfigs(@PathParam("component") String component,
90 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -070091 ComponentConfigService service = get(ComponentConfigService.class);
92 ObjectNode props = (ObjectNode) mapper().readTree(request);
93 props.fieldNames().forEachRemaining(k -> service.setProperty(component, k,
94 props.path(k).asText()));
95 return Response.noContent().build();
96 }
97
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070098 /**
99 * Selectively clear configuration properties.
100 * Clears only the properties present in the JSON request.
101 *
102 * @param component component name
103 * @return 200 OK
104 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700105 @DELETE
106 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700107 public Response unsetConfigs(@PathParam("component") String component,
108 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700109 ComponentConfigService service = get(ComponentConfigService.class);
110 ObjectNode props = (ObjectNode) mapper().readTree(request);
111 props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
112 return Response.noContent().build();
113 }
114}