Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 42 | |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 43 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 44 | * Manage component configurations. |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 45 | */ |
| 46 | @Path("configuration") |
| 47 | public class ComponentConfigWebResource extends AbstractWebResource { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 48 | private static final int MULTI_STATUS_RESPONE = 207; |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 49 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 50 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 51 | * Gets all component configurations. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 52 | * Returns collection of all registered component configurations. |
| 53 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 54 | * @return 200 OK with a collection of component configurations |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 55 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 56 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 57 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 58 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 66 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 67 | * Gets configuration of the specified component. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 68 | * |
| 69 | * @param component component name |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 70 | * @return 200 OK with a collection of component configurations |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 71 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 72 | @GET |
| 73 | @Path("{component}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 74 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 75 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 91 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 92 | * Selectively sets configuration properties. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 93 | * Sets only the properties present in the JSON request. |
| 94 | * |
| 95 | * @param component component name |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 96 | * @param preset preset the property if true |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 97 | * @param request JSON configuration |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 98 | * @return 200 OK |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 99 | * @throws IOException to signify bad request |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 100 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 101 | @POST |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 102 | @Consumes(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 103 | @Path("{component}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 104 | public Response setConfigs(@PathParam("component") String component, |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 105 | @DefaultValue("false") @QueryParam("preset") boolean preset, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 106 | InputStream request) throws IOException { |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 107 | ComponentConfigService service = get(ComponentConfigService.class); |
| 108 | ObjectNode props = (ObjectNode) mapper().readTree(request); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 109 | List<String> errorMsgs = new ArrayList<String>(); |
Charles Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 110 | if (preset) { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 111 | 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 Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 118 | } else { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 119 | 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 Chan | 2cece3d | 2016-12-02 17:11:26 -0800 | [diff] [blame] | 129 | } |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 130 | return Response.ok().build(); |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 131 | } |
| 132 | |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 133 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 139 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 140 | * Selectively clears configuration properties. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 141 | * Clears only the properties present in the JSON request. |
| 142 | * |
| 143 | * @param component component name |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 144 | * @param request JSON configuration |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 145 | * @return 204 NO CONTENT |
Thomas Vachuska | 87ae1d9 | 2015-08-19 17:39:11 -0700 | [diff] [blame] | 146 | * @throws IOException to signify bad request |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 147 | */ |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 148 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 149 | @Consumes(MediaType.APPLICATION_JSON) |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 150 | @Path("{component}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 151 | public Response unsetConfigs(@PathParam("component") String component, |
| 152 | InputStream request) throws IOException { |
Thomas Vachuska | e684743 | 2015-04-03 23:56:46 -0700 | [diff] [blame] | 153 | 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 | } |