blob: ad45e50fd75b5c444a2abdae9e9b898c358b65a1 [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
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;
42
Thomas Vachuskae6847432015-04-03 23:56:46 -070043/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 * Manage component configurations.
Thomas Vachuskae6847432015-04-03 23:56:46 -070045 */
46@Path("configuration")
47public class ComponentConfigWebResource extends AbstractWebResource {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +053048 private static final int MULTI_STATUS_RESPONE = 207;
Thomas Vachuskae6847432015-04-03 23:56:46 -070049
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 /**
Jian Licc730a62016-05-10 16:36:16 -070051 * Gets all component configurations.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070052 * Returns collection of all registered component configurations.
53 *
Jian Licc730a62016-05-10 16:36:16 -070054 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070055 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070056 @GET
Jian Licc730a62016-05-10 16:36:16 -070057 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070058 public Response getComponentConfigs() {
59 ComponentConfigService service = get(ComponentConfigService.class);
60 Set<String> components = service.getComponentNames();
61 ObjectNode root = mapper().createObjectNode();
62 components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
63 return ok(root).build();
64 }
65
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070066 /**
Jian Licc730a62016-05-10 16:36:16 -070067 * Gets configuration of the specified component.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070068 *
69 * @param component component name
Jian Licc730a62016-05-10 16:36:16 -070070 * @return 200 OK with a collection of component configurations
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 */
Thomas Vachuskae6847432015-04-03 23:56:46 -070072 @GET
73 @Path("{component}")
Jian Licc730a62016-05-10 16:36:16 -070074 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -070075 public Response getComponentConfigs(@PathParam("component") String component) {
76 ComponentConfigService service = get(ComponentConfigService.class);
77 ObjectNode root = mapper().createObjectNode();
78 encodeConfigs(component, nullIsNotFound(service.getProperties(component),
79 "No such component"), root);
80 return ok(root).build();
81 }
82
83 // Encodes the specified properties as an object in the given node.
84 private void encodeConfigs(String component, Set<ConfigProperty> props,
85 ObjectNode node) {
86 ObjectNode compNode = mapper().createObjectNode();
87 node.set(component, compNode);
88 props.forEach(p -> compNode.put(p.name(), p.value()));
89 }
90
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070091 /**
Jian Licc730a62016-05-10 16:36:16 -070092 * Selectively sets configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070093 * Sets only the properties present in the JSON request.
94 *
95 * @param component component name
Charles Chan2cece3d2016-12-02 17:11:26 -080096 * @param preset preset the property if true
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070097 * @param request JSON configuration
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070098 * @return 200 OK
Thomas Vachuska87ae1d92015-08-19 17:39:11 -070099 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700100 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700101 @POST
Jian Lic2a542b2016-05-10 11:48:19 -0700102 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700103 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700104 public Response setConfigs(@PathParam("component") String component,
Charles Chan2cece3d2016-12-02 17:11:26 -0800105 @DefaultValue("false") @QueryParam("preset") boolean preset,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700106 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700107 ComponentConfigService service = get(ComponentConfigService.class);
108 ObjectNode props = (ObjectNode) mapper().readTree(request);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530109 List<String> errorMsgs = new ArrayList<String>();
Charles Chan2cece3d2016-12-02 17:11:26 -0800110 if (preset) {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530111 props.fieldNames().forEachRemaining(k -> {
112 try {
113 service.preSetProperty(component, k, props.path(k).asText());
114 } catch (IllegalArgumentException e) {
115 errorMsgs.add(e.getMessage());
116 }
117 });
Charles Chan2cece3d2016-12-02 17:11:26 -0800118 } else {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530119 props.fieldNames().forEachRemaining(k -> {
120 try {
121 service.setProperty(component, k, props.path(k).asText());
122 } catch (IllegalArgumentException e) {
123 errorMsgs.add(e.getMessage());
124 }
125 });
126 }
127 if (!errorMsgs.isEmpty()) {
128 return Response.status(MULTI_STATUS_RESPONE).entity(produceErrorJson(errorMsgs)).build();
Charles Chan2cece3d2016-12-02 17:11:26 -0800129 }
Jian Licc730a62016-05-10 16:36:16 -0700130 return Response.ok().build();
Thomas Vachuskae6847432015-04-03 23:56:46 -0700131 }
132
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530133 private ObjectNode produceErrorJson(List<String> errorMsgs) {
134 ObjectMapper mapper = new ObjectMapper();
135 ObjectNode result = mapper.createObjectNode().put("code", 207).putPOJO("message", errorMsgs);
136 return result;
137 }
138
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700139 /**
Jian Licc730a62016-05-10 16:36:16 -0700140 * Selectively clears configuration properties.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700141 * Clears only the properties present in the JSON request.
142 *
143 * @param component component name
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700144 * @param request JSON configuration
Jian Lic2a542b2016-05-10 11:48:19 -0700145 * @return 204 NO CONTENT
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700146 * @throws IOException to signify bad request
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700147 */
Thomas Vachuskae6847432015-04-03 23:56:46 -0700148 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700149 @Consumes(MediaType.APPLICATION_JSON)
Thomas Vachuskae6847432015-04-03 23:56:46 -0700150 @Path("{component}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700151 public Response unsetConfigs(@PathParam("component") String component,
152 InputStream request) throws IOException {
Thomas Vachuskae6847432015-04-03 23:56:46 -0700153 ComponentConfigService service = get(ComponentConfigService.class);
154 ObjectNode props = (ObjectNode) mapper().readTree(request);
155 props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
156 return Response.noContent().build();
157 }
158}