blob: d1a8ff7b76e2c79f5899748c19a0c9072ab5c439 [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 Licc730a62016-05-10 16:36:16 -070029import javax.ws.rs.Produces;
Jian Lic2a542b2016-05-10 11:48:19 -070030import javax.ws.rs.core.MediaType;
Thomas Vachuskae6847432015-04-03 23:56:46 -070031import javax.ws.rs.core.Response;
32import java.io.IOException;
33import java.io.InputStream;
34import java.util.Set;
35
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070036import static org.onlab.util.Tools.nullIsNotFound;
37
Thomas Vachuskae6847432015-04-03 23:56:46 -070038/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070039 * Manage component configurations.
Thomas Vachuskae6847432015-04-03 23:56:46 -070040 */
41@Path("configuration")
42public class ComponentConfigWebResource extends AbstractWebResource {
43
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 /**
Jian Licc730a62016-05-10 16:36:16 -070045 * Gets all component configurations.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070046 * Returns collection of all registered component configurations.
47 *
Jian Licc730a62016-05-10 16:36:16 -070048 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070049 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070050 @GET
Jian Licc730a62016-05-10 16:36:16 -070051 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070052 public Response getComponentConfigs() {
53 ComponentConfigService service = get(ComponentConfigService.class);
54 Set<String> components = service.getComponentNames();
55 ObjectNode root = mapper().createObjectNode();
56 components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
57 return ok(root).build();
58 }
59
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070060 /**
Jian Licc730a62016-05-10 16:36:16 -070061 * Gets configuration of the specified component.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 *
63 * @param component component name
Jian Licc730a62016-05-10 16:36:16 -070064 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070066 @GET
67 @Path("{component}")
Jian Licc730a62016-05-10 16:36:16 -070068 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070069 public Response getComponentConfigs(@PathParam("component") String component) {
70 ComponentConfigService service = get(ComponentConfigService.class);
71 ObjectNode root = mapper().createObjectNode();
72 encodeConfigs(component, nullIsNotFound(service.getProperties(component),
73 "No such component"), root);
74 return ok(root).build();
75 }
76
77 // Encodes the specified properties as an object in the given node.
78 private void encodeConfigs(String component, Set<ConfigProperty> props,
79 ObjectNode node) {
80 ObjectNode compNode = mapper().createObjectNode();
81 node.set(component, compNode);
82 props.forEach(p -> compNode.put(p.name(), p.value()));
83 }
84
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070085 /**
Jian Licc730a62016-05-10 16:36:16 -070086 * Selectively sets configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070087 * Sets only the properties present in the JSON request.
88 *
89 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070090 * @param request JSON configuration
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070091 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070092 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070093 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070094 @POST
Jian Lic2a542b2016-05-10 11:48:19 -070095 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070096 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070097 public Response setConfigs(@PathParam("component") String component,
98 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -070099 ComponentConfigService service = get(ComponentConfigService.class);
100 ObjectNode props = (ObjectNode) mapper().readTree(request);
101 props.fieldNames().forEachRemaining(k -> service.setProperty(component, k,
102 props.path(k).asText()));
Jian Licc730a62016-05-10 16:36:16 -0700103 return Response.ok().build();
Thomas Vachuskae6847432015-04-03 23:56:46 -0700104 }
105
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700106 /**
Jian Licc730a62016-05-10 16:36:16 -0700107 * Selectively clears configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700108 * Clears only the properties present in the JSON request.
109 *
110 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700111 * @param request JSON configuration
Jian Lic2a542b2016-05-10 11:48:19 -0700112 * @return 204 NO CONTENT
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700113 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700114 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700115 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700116 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700117 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700118 public Response unsetConfigs(@PathParam("component") String component,
119 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700120 ComponentConfigService service = get(ComponentConfigService.class);
121 ObjectNode props = (ObjectNode) mapper().readTree(request);
122 props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
123 return Response.noContent().build();
124 }
125}