blob: 5751e35741bc474fcc180fb58e203311ea3d9f78 [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;
Charles Chan2cece3d2016-12-02 17:11:26 -080025import javax.ws.rs.DefaultValue;
Thomas Vachuskae6847432015-04-03 23:56:46 -070026import javax.ws.rs.GET;
27import javax.ws.rs.POST;
28import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
Jian Licc730a62016-05-10 16:36:16 -070030import javax.ws.rs.Produces;
Charles Chan2cece3d2016-12-02 17:11:26 -080031import javax.ws.rs.QueryParam;
Jian Lic2a542b2016-05-10 11:48:19 -070032import javax.ws.rs.core.MediaType;
Thomas Vachuskae6847432015-04-03 23:56:46 -070033import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
36import java.util.Set;
37
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070038import static org.onlab.util.Tools.nullIsNotFound;
39
Thomas Vachuskae6847432015-04-03 23:56:46 -070040/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070041 * Manage component configurations.
Thomas Vachuskae6847432015-04-03 23:56:46 -070042 */
43@Path("configuration")
44public class ComponentConfigWebResource extends AbstractWebResource {
45
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070046 /**
Jian Licc730a62016-05-10 16:36:16 -070047 * Gets all component configurations.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070048 * Returns collection of all registered component configurations.
49 *
Jian Licc730a62016-05-10 16:36:16 -070050 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070051 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070052 @GET
Jian Licc730a62016-05-10 16:36:16 -070053 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070054 public Response getComponentConfigs() {
55 ComponentConfigService service = get(ComponentConfigService.class);
56 Set<String> components = service.getComponentNames();
57 ObjectNode root = mapper().createObjectNode();
58 components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
59 return ok(root).build();
60 }
61
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 /**
Jian Licc730a62016-05-10 16:36:16 -070063 * Gets configuration of the specified component.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 *
65 * @param component component name
Jian Licc730a62016-05-10 16:36:16 -070066 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070067 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070068 @GET
69 @Path("{component}")
Jian Licc730a62016-05-10 16:36:16 -070070 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070071 public Response getComponentConfigs(@PathParam("component") String component) {
72 ComponentConfigService service = get(ComponentConfigService.class);
73 ObjectNode root = mapper().createObjectNode();
74 encodeConfigs(component, nullIsNotFound(service.getProperties(component),
75 "No such component"), root);
76 return ok(root).build();
77 }
78
79 // Encodes the specified properties as an object in the given node.
80 private void encodeConfigs(String component, Set<ConfigProperty> props,
81 ObjectNode node) {
82 ObjectNode compNode = mapper().createObjectNode();
83 node.set(component, compNode);
84 props.forEach(p -> compNode.put(p.name(), p.value()));
85 }
86
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070087 /**
Jian Licc730a62016-05-10 16:36:16 -070088 * Selectively sets configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070089 * Sets only the properties present in the JSON request.
90 *
91 * @param component component name
Charles Chan2cece3d2016-12-02 17:11:26 -080092 * @param preset preset the property if true
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070093 * @param request JSON configuration
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070094 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070095 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070096 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070097 @POST
Jian Lic2a542b2016-05-10 11:48:19 -070098 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070099 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700100 public Response setConfigs(@PathParam("component") String component,
Charles Chan2cece3d2016-12-02 17:11:26 -0800101 @DefaultValue("false") @QueryParam("preset") boolean preset,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700102 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700103 ComponentConfigService service = get(ComponentConfigService.class);
104 ObjectNode props = (ObjectNode) mapper().readTree(request);
Charles Chan2cece3d2016-12-02 17:11:26 -0800105 if (preset) {
106 props.fieldNames().forEachRemaining(k ->
107 service.preSetProperty(component, k, props.path(k).asText()));
108 } else {
109 props.fieldNames().forEachRemaining(k ->
110 service.setProperty(component, k, props.path(k).asText()));
111 }
Jian Licc730a62016-05-10 16:36:16 -0700112 return Response.ok().build();
Thomas Vachuskae6847432015-04-03 23:56:46 -0700113 }
114
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700115 /**
Jian Licc730a62016-05-10 16:36:16 -0700116 * Selectively clears configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700117 * Clears only the properties present in the JSON request.
118 *
119 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700120 * @param request JSON configuration
Jian Lic2a542b2016-05-10 11:48:19 -0700121 * @return 204 NO CONTENT
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700122 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700123 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700124 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700125 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700126 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700127 public Response unsetConfigs(@PathParam("component") String component,
128 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700129 ComponentConfigService service = get(ComponentConfigService.class);
130 ObjectNode props = (ObjectNode) mapper().readTree(request);
131 props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
132 return Response.noContent().build();
133 }
134}