Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 3 | * |
| 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 17 | |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.cfg.ComponentConfigService; |
| 21 | import org.onosproject.cfg.ConfigProperty; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 23 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 24 | import javax.ws.rs.Consumes; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 25 | import javax.ws.rs.DELETE; |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 26 | import javax.ws.rs.DefaultValue; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 27 | import javax.ws.rs.GET; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 31 | import javax.ws.rs.Produces; |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 32 | import javax.ws.rs.QueryParam; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 33 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 34 | import javax.ws.rs.core.Response; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 37 | import java.util.List; |
| 38 | import java.util.ArrayList; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 39 | import java.util.Set; |
| 40 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 41 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 42 | import static org.onlab.util.Tools.readTreeFromStream; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 43 | |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 44 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 45 | * Manage component configurations. |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 46 | */ |
| 47 | @Path("configuration") |
| 48 | public class ComponentConfigWebResource extends AbstractWebResource { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 49 | private static final int MULTI_STATUS_RESPONE = 207; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 50 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 51 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 52 | * Gets all component configurations. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 53 | * Returns collection of all registered component configurations. |
| 54 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 55 | * @return 200 OK with a collection of component configurations |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 56 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 57 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 58 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 59 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 67 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 68 | * Gets configuration of the specified component. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 69 | * |
| 70 | * @param component component name |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 71 | * @return 200 OK with a collection of component configurations |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 72 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 73 | @GET |
| 74 | @Path("{component}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 75 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 76 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 92 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 93 | * Selectively sets configuration properties. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 94 | * Sets only the properties present in the JSON request. |
| 95 | * |
| 96 | * @param component component name |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 97 | * @param preset preset the property if true |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 98 | * @param request JSON configuration |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 99 | * @return 200 OK |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 100 | * @throws IOException to signify bad request |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 101 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 102 | @POST |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 103 | @Consumes(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 104 | @Path("{component}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 105 | public Response setConfigs(@PathParam("component") String component, |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 106 | @DefaultValue("false") @QueryParam("preset") boolean preset, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 107 | InputStream request) throws IOException { |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 108 | ComponentConfigService service = get(ComponentConfigService.class); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 109 | ObjectNode props = readTreeFromStream(mapper(), request); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 110 | List<String> errorMsgs = new ArrayList<String>(); |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 111 | if (preset) { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 112 | 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 Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 119 | } else { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 120 | 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 Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 130 | } |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 131 | return Response.ok().build(); |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 132 | } |
| 133 | |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 134 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 140 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 141 | * Selectively clears configuration properties. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 142 | * Clears only the properties present in the JSON request. |
| 143 | * |
| 144 | * @param component component name |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 145 | * @param request JSON configuration |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 146 | * @return 204 NO CONTENT |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 147 | * @throws IOException to signify bad request |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 148 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 149 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 150 | @Consumes(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 151 | @Path("{component}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 152 | public Response unsetConfigs(@PathParam("component") String component, |
| 153 | InputStream request) throws IOException { |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 154 | ComponentConfigService service = get(ComponentConfigService.class); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 155 | ObjectNode props = readTreeFromStream(mapper(), request); |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 156 | props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k)); |
| 157 | return Response.noContent().build(); |
| 158 | } |
psneha | 459608e | 2018-08-02 01:16:46 -0400 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Gets specified value of a specified component and variable. |
| 162 | * |
| 163 | * @param component component name |
| 164 | * @param attribute attribute name |
| 165 | * @return 200 OK with a collection of component configurations |
| 166 | */ |
| 167 | @GET |
| 168 | @Path("{component}/{attribute}") |
| 169 | @Produces(MediaType.APPLICATION_JSON) |
| 170 | public Response getComponentConfig(@PathParam("component") String component, |
| 171 | @PathParam("attribute") String attribute) { |
| 172 | ComponentConfigService service = get(ComponentConfigService.class); |
| 173 | ObjectNode root = mapper().createObjectNode(); |
| 174 | encodeConfigs(component, attribute, |
| 175 | nullIsNotFound(service.getProperty(component, attribute), |
| 176 | (service.getProperties(component) == null) ? |
| 177 | "No such component" : (service.getProperty(component, attribute) == null) ? |
| 178 | ("No such attribute in " + component) : "No such attribute and component"), root); |
| 179 | return ok(root).build(); |
| 180 | } |
| 181 | |
| 182 | // Encodes the specified property with attribute as an object in the given node. |
| 183 | private void encodeConfigs(String component, String attribute, ConfigProperty props, |
| 184 | ObjectNode node) { |
| 185 | ObjectNode compNode = mapper().createObjectNode(); |
| 186 | node.set(component, compNode); |
| 187 | compNode.put(attribute, props.value()); |
| 188 | } |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 189 | } |