Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.k8snode.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Sets; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 22 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 23 | import org.onosproject.k8snode.api.K8sApiConfigAdminService; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 24 | import org.onosproject.k8snode.api.K8sNode; |
| 25 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
| 26 | import org.onosproject.rest.AbstractWebResource; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import javax.ws.rs.Consumes; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 31 | import javax.ws.rs.DELETE; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 32 | import javax.ws.rs.POST; |
| 33 | import javax.ws.rs.PUT; |
| 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.PathParam; |
| 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.Context; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import javax.ws.rs.core.UriBuilder; |
| 41 | import javax.ws.rs.core.UriInfo; |
| 42 | import java.io.InputStream; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 46 | import static javax.ws.rs.core.Response.created; |
| 47 | import static org.onlab.util.Tools.nullIsIllegal; |
| 48 | import static org.onlab.util.Tools.readTreeFromStream; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 49 | import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Handles REST API call of kubernetes node config. |
| 53 | */ |
| 54 | |
| 55 | @Path("configure") |
| 56 | public class K8sNodeWebResource extends AbstractWebResource { |
| 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
| 60 | private static final String MESSAGE_NODE = "Received node %s request"; |
| 61 | private static final String NODES = "nodes"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 62 | private static final String API_CONFIGS = "apiConfigs"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 63 | private static final String CREATE = "CREATE"; |
| 64 | private static final String UPDATE = "UPDATE"; |
| 65 | private static final String NODE_ID = "NODE_ID"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 66 | private static final String REMOVE = "REMOVE"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 67 | |
| 68 | private static final String HOST_NAME = "hostname"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 69 | private static final String ENDPOINT = "endpoint"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 70 | private static final String ERROR_MESSAGE = " cannot be null"; |
| 71 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 72 | private final K8sNodeAdminService nodeAdminService = get(K8sNodeAdminService.class); |
| 73 | private final K8sApiConfigAdminService configAdminService = get(K8sApiConfigAdminService.class); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 74 | |
| 75 | @Context |
| 76 | private UriInfo uriInfo; |
| 77 | |
| 78 | /** |
| 79 | * Creates a set of kubernetes nodes' config from the JSON input stream. |
| 80 | * |
| 81 | * @param input kubernetes nodes JSON input stream |
| 82 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 83 | * is malformed |
| 84 | * @onos.rsModel K8sNode |
| 85 | */ |
| 86 | @POST |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 87 | @Path("node") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 88 | @Consumes(MediaType.APPLICATION_JSON) |
| 89 | @Produces(MediaType.APPLICATION_JSON) |
| 90 | public Response createNodes(InputStream input) { |
| 91 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 92 | |
| 93 | readNodeConfiguration(input).forEach(node -> { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 94 | K8sNode existing = nodeAdminService.node(node.hostname()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 95 | if (existing == null) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 96 | nodeAdminService.createNode(node); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 97 | } |
| 98 | }); |
| 99 | |
| 100 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 101 | .path(NODES) |
| 102 | .path(NODE_ID); |
| 103 | |
| 104 | return created(locationBuilder.build()).build(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Updates a set of kubernetes nodes' config from the JSON input stream. |
| 109 | * |
| 110 | * @param input kubernetes nodes JSON input stream |
| 111 | * @return 200 OK with the updated kubernetes node's config, 400 BAD_REQUEST |
| 112 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
| 113 | * @onos.rsModel K8sNode |
| 114 | */ |
| 115 | @PUT |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 116 | @Path("node") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 117 | @Consumes(MediaType.APPLICATION_JSON) |
| 118 | @Produces(MediaType.APPLICATION_JSON) |
| 119 | public Response updateNodes(InputStream input) { |
| 120 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
| 121 | |
| 122 | Set<K8sNode> nodes = readNodeConfiguration(input); |
| 123 | for (K8sNode node: nodes) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 124 | K8sNode existing = nodeAdminService.node(node.hostname()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 125 | if (existing == null) { |
| 126 | log.warn("There is no node configuration to update : {}", node.hostname()); |
| 127 | return Response.notModified().build(); |
| 128 | } else if (!existing.equals(node)) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 129 | nodeAdminService.updateNode(node); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | return Response.ok().build(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Removes a set of kubernetes nodes' config from the JSON input stream. |
| 138 | * |
| 139 | * @param hostname host name contained in kubernetes nodes configuration |
| 140 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and |
| 141 | * 304 NOT_MODIFIED without the updated config |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 142 | */ |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 143 | @DELETE |
| 144 | @Path("node/{hostname}") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 145 | @Consumes(MediaType.APPLICATION_JSON) |
| 146 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 147 | public Response deleteNodes(@PathParam("hostname") String hostname) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 148 | log.trace(String.format(MESSAGE_NODE, REMOVE)); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 149 | |
| 150 | K8sNode existing = |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 151 | nodeAdminService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE)); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 152 | |
| 153 | if (existing == null) { |
| 154 | log.warn("There is no node configuration to delete : {}", hostname); |
| 155 | return Response.notModified().build(); |
| 156 | } else { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 157 | nodeAdminService.removeNode(hostname); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return Response.noContent().build(); |
| 161 | } |
| 162 | |
| 163 | private Set<K8sNode> readNodeConfiguration(InputStream input) { |
| 164 | Set<K8sNode> nodeSet = Sets.newHashSet(); |
| 165 | try { |
| 166 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 167 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
| 168 | nodes.forEach(node -> { |
| 169 | try { |
| 170 | ObjectNode objectNode = node.deepCopy(); |
| 171 | K8sNode k8sNode = |
| 172 | codec(K8sNode.class).decode(objectNode, this); |
| 173 | |
| 174 | nodeSet.add(k8sNode); |
| 175 | } catch (Exception e) { |
| 176 | log.error("Exception occurred due to {}", e); |
| 177 | throw new IllegalArgumentException(); |
| 178 | } |
| 179 | }); |
| 180 | } catch (Exception e) { |
| 181 | throw new IllegalArgumentException(e); |
| 182 | } |
| 183 | |
| 184 | return nodeSet; |
| 185 | } |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * Creates a set of kubernetes API config from the JSON input stream. |
| 189 | * |
| 190 | * @param input kubernetes API configs JSON input stream |
| 191 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 192 | * is malformed |
| 193 | * @onos.rsModel K8sApiConfig |
| 194 | */ |
| 195 | @POST |
| 196 | @Path("api") |
| 197 | @Consumes(MediaType.APPLICATION_JSON) |
| 198 | @Produces(MediaType.APPLICATION_JSON) |
| 199 | public Response createApiConfigs(InputStream input) { |
| 200 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 201 | |
| 202 | readApiConfigConfiguration(input).forEach(config -> { |
| 203 | K8sApiConfig existing = configAdminService.apiConfig(endpoint(config)); |
| 204 | if (existing == null) { |
| 205 | configAdminService.createApiConfig(config); |
| 206 | } |
| 207 | }); |
| 208 | |
| 209 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 210 | .path(API_CONFIGS); |
| 211 | |
| 212 | return created(locationBuilder.build()).build(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Updates a set of kubernetes API config from the JSON input stream. |
| 217 | * |
| 218 | * @param input kubernetes API configs JSON input stream |
| 219 | * @return 200 OK with the updated kubernetes API config, 400 BAD_REQUEST |
| 220 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
| 221 | * @onos.rsModel K8sApiConfig |
| 222 | */ |
| 223 | @PUT |
| 224 | @Path("api") |
| 225 | @Consumes(MediaType.APPLICATION_JSON) |
| 226 | @Produces(MediaType.APPLICATION_JSON) |
| 227 | public Response updateApiConfigs(InputStream input) { |
| 228 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
| 229 | |
| 230 | Set<K8sApiConfig> configs = readApiConfigConfiguration(input); |
| 231 | for (K8sApiConfig config: configs) { |
| 232 | K8sApiConfig existing = configAdminService.apiConfig(endpoint(config)); |
| 233 | if (existing == null) { |
| 234 | log.warn("There is no API configuration to update : {}", endpoint(config)); |
| 235 | return Response.notModified().build(); |
| 236 | } else if (!existing.equals(config)) { |
| 237 | configAdminService.updateApiConfig(config); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return Response.ok().build(); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Removes a kubernetes API config. |
| 246 | * |
| 247 | * @param endpoint kubernetes API endpoint |
| 248 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed |
| 249 | */ |
| 250 | @DELETE |
| 251 | @Path("api/{endpoint : .+}") |
| 252 | @Consumes(MediaType.APPLICATION_JSON) |
| 253 | @Produces(MediaType.APPLICATION_JSON) |
| 254 | public Response deleteApiConfig(@PathParam("endpoint") String endpoint) { |
| 255 | log.trace(String.format(MESSAGE_NODE, REMOVE)); |
| 256 | |
| 257 | K8sApiConfig existing = |
| 258 | configAdminService.apiConfig(nullIsIllegal(endpoint, ENDPOINT + ERROR_MESSAGE)); |
| 259 | |
| 260 | if (existing == null) { |
| 261 | log.warn("There is no API configuration to delete : {}", endpoint); |
| 262 | return Response.notModified().build(); |
| 263 | } else { |
| 264 | configAdminService.removeApiConfig(endpoint); |
| 265 | } |
| 266 | |
| 267 | return Response.noContent().build(); |
| 268 | } |
| 269 | |
| 270 | private Set<K8sApiConfig> readApiConfigConfiguration(InputStream input) { |
| 271 | Set<K8sApiConfig> configSet = Sets.newHashSet(); |
| 272 | try { |
| 273 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 274 | ArrayNode configs = (ArrayNode) jsonTree.path(API_CONFIGS); |
| 275 | configs.forEach(config -> { |
| 276 | try { |
| 277 | ObjectNode objectNode = config.deepCopy(); |
| 278 | K8sApiConfig k8sApiConfig = |
| 279 | codec(K8sApiConfig.class).decode(objectNode, this); |
| 280 | |
| 281 | configSet.add(k8sApiConfig); |
| 282 | } catch (Exception e) { |
| 283 | log.error("Exception occurred due to {}", e); |
| 284 | throw new IllegalArgumentException(); |
| 285 | } |
| 286 | }); |
| 287 | } catch (Exception e) { |
| 288 | throw new IllegalArgumentException(e); |
| 289 | } |
| 290 | |
| 291 | return configSet; |
| 292 | } |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 293 | } |