blob: 9d3bcca23b29314205a06faaa3b105cd3dc299b5 [file] [log] [blame]
Thomas Vachuskae6847432015-04-03 23:56:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053018import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuskae6847432015-04-03 23:56:46 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.cfg.ComponentConfigService;
21import org.onosproject.cfg.ConfigProperty;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070022import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskae6847432015-04-03 23:56:46 -070023
Jian Lic2a542b2016-05-10 11:48:19 -070024import javax.ws.rs.Consumes;
Thomas Vachuskae6847432015-04-03 23:56:46 -070025import javax.ws.rs.DELETE;
Charles Chan2cece3d2016-12-02 17:11:26 -080026import javax.ws.rs.DefaultValue;
Thomas Vachuskae6847432015-04-03 23:56:46 -070027import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
Jian Licc730a62016-05-10 16:36:16 -070031import javax.ws.rs.Produces;
Charles Chan2cece3d2016-12-02 17:11:26 -080032import javax.ws.rs.QueryParam;
Jian Lic2a542b2016-05-10 11:48:19 -070033import javax.ws.rs.core.MediaType;
Thomas Vachuskae6847432015-04-03 23:56:46 -070034import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053037import java.util.List;
38import java.util.ArrayList;
Thomas Vachuskae6847432015-04-03 23:56:46 -070039import java.util.Set;
40
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070041import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070042import static org.onlab.util.Tools.readTreeFromStream;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070043
Thomas Vachuskae6847432015-04-03 23:56:46 -070044/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070045 * Manage component configurations.
Thomas Vachuskae6847432015-04-03 23:56:46 -070046 */
47@Path("configuration")
48public class ComponentConfigWebResource extends AbstractWebResource {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053049 private static final int MULTI_STATUS_RESPONE = 207;
Thomas Vachuskae6847432015-04-03 23:56:46 -070050
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070051 /**
Jian Licc730a62016-05-10 16:36:16 -070052 * Gets all component configurations.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070053 * Returns collection of all registered component configurations.
54 *
Jian Licc730a62016-05-10 16:36:16 -070055 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070056 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070057 @GET
Jian Licc730a62016-05-10 16:36:16 -070058 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070059 public Response getComponentConfigs() {
60 ComponentConfigService service = get(ComponentConfigService.class);
61 Set<String> components = service.getComponentNames();
62 ObjectNode root = mapper().createObjectNode();
63 components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
64 return ok(root).build();
65 }
66
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070067 /**
Jian Licc730a62016-05-10 16:36:16 -070068 * Gets configuration of the specified component.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070069 *
70 * @param component component name
Jian Licc730a62016-05-10 16:36:16 -070071 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070072 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070073 @GET
74 @Path("{component}")
Jian Licc730a62016-05-10 16:36:16 -070075 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070076 public Response getComponentConfigs(@PathParam("component") String component) {
77 ComponentConfigService service = get(ComponentConfigService.class);
78 ObjectNode root = mapper().createObjectNode();
79 encodeConfigs(component, nullIsNotFound(service.getProperties(component),
80 "No such component"), root);
81 return ok(root).build();
82 }
83
84 // Encodes the specified properties as an object in the given node.
85 private void encodeConfigs(String component, Set<ConfigProperty> props,
86 ObjectNode node) {
87 ObjectNode compNode = mapper().createObjectNode();
88 node.set(component, compNode);
89 props.forEach(p -> compNode.put(p.name(), p.value()));
90 }
91
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070092 /**
Jian Licc730a62016-05-10 16:36:16 -070093 * Selectively sets configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070094 * Sets only the properties present in the JSON request.
95 *
96 * @param component component name
Charles Chan2cece3d2016-12-02 17:11:26 -080097 * @param preset preset the property if true
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070098 * @param request JSON configuration
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070099 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700100 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700101 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700102 @POST
Jian Lic2a542b2016-05-10 11:48:19 -0700103 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700104 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700105 public Response setConfigs(@PathParam("component") String component,
Charles Chan2cece3d2016-12-02 17:11:26 -0800106 @DefaultValue("false") @QueryParam("preset") boolean preset,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700107 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700108 ComponentConfigService service = get(ComponentConfigService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -0700109 ObjectNode props = readTreeFromStream(mapper(), request);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530110 List<String> errorMsgs = new ArrayList<String>();
Charles Chan2cece3d2016-12-02 17:11:26 -0800111 if (preset) {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530112 props.fieldNames().forEachRemaining(k -> {
113 try {
114 service.preSetProperty(component, k, props.path(k).asText());
115 } catch (IllegalArgumentException e) {
116 errorMsgs.add(e.getMessage());
117 }
118 });
Charles Chan2cece3d2016-12-02 17:11:26 -0800119 } else {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530120 props.fieldNames().forEachRemaining(k -> {
121 try {
122 service.setProperty(component, k, props.path(k).asText());
123 } catch (IllegalArgumentException e) {
124 errorMsgs.add(e.getMessage());
125 }
126 });
127 }
128 if (!errorMsgs.isEmpty()) {
129 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
Charles Chan2cece3d2016-12-02 17:11:26 -0800130 }
Jian Licc730a62016-05-10 16:36:16 -0700131 return Response.ok().build();
Thomas Vachuskae6847432015-04-03 23:56:46 -0700132 }
133
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530134 private ObjectNode produceErrorJson(List<String> errorMsgs) {
135 ObjectMapper mapper = new ObjectMapper();
136 ObjectNode result = mapper.createObjectNode().put("code", 207).putPOJO("message", errorMsgs);
137 return result;
138 }
139
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700140 /**
Jian Licc730a62016-05-10 16:36:16 -0700141 * Selectively clears configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700142 * Clears only the properties present in the JSON request.
143 *
144 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700145 * @param request JSON configuration
Jian Lic2a542b2016-05-10 11:48:19 -0700146 * @return 204 NO CONTENT
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700147 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700148 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700149 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700150 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700151 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700152 public Response unsetConfigs(@PathParam("component") String component,
153 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700154 ComponentConfigService service = get(ComponentConfigService.class);
Ray Milkey86ee5e82018-04-02 15:33:07 -0700155 ObjectNode props = readTreeFromStream(mapper(), request);
Thomas Vachuskae6847432015-04-03 23:56:46 -0700156 props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
157 return Response.noContent().build();
158 }
159}