blob: cc575818824c5767ebc436109788127d3c7ea4f0 [file] [log] [blame]
Thomas Vachuskae6847432015-04-03 23:56:46 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskae6847432015-04-03 23:56:46 -07003 *
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
Jian Lic2a542b2016-05-10 11:48:19 -070023import javax.ws.rs.Consumes;
Thomas Vachuskae6847432015-04-03 23:56:46 -070024import javax.ws.rs.DELETE;
25import javax.ws.rs.GET;
26import javax.ws.rs.POST;
27import javax.ws.rs.Path;
28import javax.ws.rs.PathParam;
Jian Lic2a542b2016-05-10 11:48:19 -070029import javax.ws.rs.core.MediaType;
Thomas Vachuskae6847432015-04-03 23:56:46 -070030import javax.ws.rs.core.Response;
31import java.io.IOException;
32import java.io.InputStream;
33import java.util.Set;
34
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070035import static org.onlab.util.Tools.nullIsNotFound;
36
Thomas Vachuskae6847432015-04-03 23:56:46 -070037/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070038 * Manage component configurations.
Thomas Vachuskae6847432015-04-03 23:56:46 -070039 */
40@Path("configuration")
41public class ComponentConfigWebResource extends AbstractWebResource {
42
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070043 /**
44 * Get all component configurations.
45 * Returns collection of all registered component configurations.
46 *
47 * @return 200 OK
48 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070049 @GET
50 public Response getComponentConfigs() {
51 ComponentConfigService service = get(ComponentConfigService.class);
52 Set<String> components = service.getComponentNames();
53 ObjectNode root = mapper().createObjectNode();
54 components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
55 return ok(root).build();
56 }
57
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 /**
59 * Get configuration of the specified component.
60 *
61 * @param component component name
62 * @return 200 OK
63 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070064 @GET
65 @Path("{component}")
66 public Response getComponentConfigs(@PathParam("component") String component) {
67 ComponentConfigService service = get(ComponentConfigService.class);
68 ObjectNode root = mapper().createObjectNode();
69 encodeConfigs(component, nullIsNotFound(service.getProperties(component),
70 "No such component"), root);
71 return ok(root).build();
72 }
73
74 // Encodes the specified properties as an object in the given node.
75 private void encodeConfigs(String component, Set<ConfigProperty> props,
76 ObjectNode node) {
77 ObjectNode compNode = mapper().createObjectNode();
78 node.set(component, compNode);
79 props.forEach(p -> compNode.put(p.name(), p.value()));
80 }
81
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070082 /**
83 * Selectively set configuration properties.
84 * Sets only the properties present in the JSON request.
85 *
86 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070087 * @param request JSON configuration
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070088 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070089 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070090 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070091 @POST
Jian Lic2a542b2016-05-10 11:48:19 -070092 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070093 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070094 public Response setConfigs(@PathParam("component") String component,
95 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -070096 ComponentConfigService service = get(ComponentConfigService.class);
97 ObjectNode props = (ObjectNode) mapper().readTree(request);
98 props.fieldNames().forEachRemaining(k -> service.setProperty(component, k,
99 props.path(k).asText()));
100 return Response.noContent().build();
101 }
102
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700103 /**
104 * Selectively clear configuration properties.
105 * Clears only the properties present in the JSON request.
106 *
107 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700108 * @param request JSON configuration
Jian Lic2a542b2016-05-10 11:48:19 -0700109 * @return 204 NO CONTENT
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700110 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700111 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700112 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700113 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700114 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700115 public Response unsetConfigs(@PathParam("component") String component,
116 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700117 ComponentConfigService service = get(ComponentConfigService.class);
118 ObjectNode props = (ObjectNode) mapper().readTree(request);
119 props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
120 return Response.noContent().build();
121 }
122}