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 | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 22 | import org.onlab.packet.IpAddress; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 23 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 24 | import org.onosproject.k8snode.api.K8sApiConfigAdminService; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 25 | import org.onosproject.k8snode.api.K8sHost; |
| 26 | import org.onosproject.k8snode.api.K8sHostAdminService; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 27 | import org.onosproject.k8snode.api.K8sNode; |
| 28 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
Jian Li | 1e03e71 | 2019-06-20 18:13:48 +0900 | [diff] [blame] | 29 | import org.onosproject.k8snode.api.K8sNodeState; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 30 | import org.onosproject.rest.AbstractWebResource; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import javax.ws.rs.Consumes; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 35 | import javax.ws.rs.DELETE; |
Jian Li | 1e03e71 | 2019-06-20 18:13:48 +0900 | [diff] [blame] | 36 | import javax.ws.rs.GET; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 37 | import javax.ws.rs.POST; |
| 38 | import javax.ws.rs.PUT; |
| 39 | import javax.ws.rs.Path; |
| 40 | import javax.ws.rs.PathParam; |
| 41 | import javax.ws.rs.Produces; |
| 42 | import javax.ws.rs.core.Context; |
| 43 | import javax.ws.rs.core.MediaType; |
| 44 | import javax.ws.rs.core.Response; |
| 45 | import javax.ws.rs.core.UriBuilder; |
| 46 | import javax.ws.rs.core.UriInfo; |
| 47 | import java.io.InputStream; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 48 | import java.util.HashSet; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 49 | import java.util.Set; |
| 50 | |
| 51 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 52 | import static javax.ws.rs.core.Response.created; |
| 53 | import static org.onlab.util.Tools.nullIsIllegal; |
| 54 | import static org.onlab.util.Tools.readTreeFromStream; |
Jian Li | d376e06 | 2020-01-02 23:57:13 +0900 | [diff] [blame] | 55 | import static org.onosproject.k8snode.api.K8sNodeState.POST_ON_BOARD; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 56 | import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Handles REST API call of kubernetes node config. |
| 60 | */ |
| 61 | |
| 62 | @Path("configure") |
| 63 | public class K8sNodeWebResource extends AbstractWebResource { |
| 64 | |
| 65 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 66 | |
| 67 | private static final String MESSAGE_NODE = "Received node %s request"; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 68 | private static final String MESSAGE_HOST = "Received host %s request"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 69 | private static final String NODES = "nodes"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 70 | private static final String API_CONFIGS = "apiConfigs"; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 71 | private static final String HOSTS = "hosts"; |
| 72 | private static final String NODE_NAMES = "nodeNames"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 73 | private static final String CREATE = "CREATE"; |
| 74 | private static final String UPDATE = "UPDATE"; |
| 75 | private static final String NODE_ID = "NODE_ID"; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 76 | private static final String HOST_IP = "HOST_IP"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 77 | private static final String REMOVE = "REMOVE"; |
Jian Li | 1e03e71 | 2019-06-20 18:13:48 +0900 | [diff] [blame] | 78 | private static final String QUERY = "QUERY"; |
| 79 | private static final String INIT = "INIT"; |
| 80 | private static final String NOT_EXIST = "Not exist"; |
| 81 | private static final String STATE = "State"; |
Jian Li | d376e06 | 2020-01-02 23:57:13 +0900 | [diff] [blame] | 82 | private static final String RESULT = "Result"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 83 | |
| 84 | private static final String HOST_NAME = "hostname"; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 85 | private static final String ENDPOINT = "endpoint"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 86 | private static final String ERROR_MESSAGE = " cannot be null"; |
| 87 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 88 | private final K8sNodeAdminService nodeAdminService = get(K8sNodeAdminService.class); |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 89 | private final K8sHostAdminService hostAdminService = get(K8sHostAdminService.class); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 90 | private final K8sApiConfigAdminService configAdminService = get(K8sApiConfigAdminService.class); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 91 | |
| 92 | @Context |
| 93 | private UriInfo uriInfo; |
| 94 | |
| 95 | /** |
| 96 | * Creates a set of kubernetes nodes' config from the JSON input stream. |
| 97 | * |
| 98 | * @param input kubernetes nodes JSON input stream |
| 99 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 100 | * is malformed |
| 101 | * @onos.rsModel K8sNode |
| 102 | */ |
| 103 | @POST |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 104 | @Path("node") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 105 | @Consumes(MediaType.APPLICATION_JSON) |
| 106 | @Produces(MediaType.APPLICATION_JSON) |
| 107 | public Response createNodes(InputStream input) { |
| 108 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 109 | |
| 110 | readNodeConfiguration(input).forEach(node -> { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 111 | K8sNode existing = nodeAdminService.node(node.hostname()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 112 | if (existing == null) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 113 | nodeAdminService.createNode(node); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 114 | } |
| 115 | }); |
| 116 | |
| 117 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 118 | .path(NODES) |
| 119 | .path(NODE_ID); |
| 120 | |
| 121 | return created(locationBuilder.build()).build(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Updates a set of kubernetes nodes' config from the JSON input stream. |
| 126 | * |
| 127 | * @param input kubernetes nodes JSON input stream |
| 128 | * @return 200 OK with the updated kubernetes node's config, 400 BAD_REQUEST |
| 129 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
| 130 | * @onos.rsModel K8sNode |
| 131 | */ |
| 132 | @PUT |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 133 | @Path("node") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 134 | @Consumes(MediaType.APPLICATION_JSON) |
| 135 | @Produces(MediaType.APPLICATION_JSON) |
| 136 | public Response updateNodes(InputStream input) { |
| 137 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
| 138 | |
| 139 | Set<K8sNode> nodes = readNodeConfiguration(input); |
| 140 | for (K8sNode node: nodes) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 141 | K8sNode existing = nodeAdminService.node(node.hostname()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 142 | if (existing == null) { |
| 143 | log.warn("There is no node configuration to update : {}", node.hostname()); |
| 144 | return Response.notModified().build(); |
| 145 | } else if (!existing.equals(node)) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 146 | nodeAdminService.updateNode(node); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | return Response.ok().build(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Removes a set of kubernetes nodes' config from the JSON input stream. |
| 155 | * |
| 156 | * @param hostname host name contained in kubernetes nodes configuration |
| 157 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and |
| 158 | * 304 NOT_MODIFIED without the updated config |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 159 | */ |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 160 | @DELETE |
| 161 | @Path("node/{hostname}") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 162 | @Consumes(MediaType.APPLICATION_JSON) |
| 163 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 164 | public Response deleteNodes(@PathParam("hostname") String hostname) { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 165 | log.trace(String.format(MESSAGE_NODE, REMOVE)); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 166 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 167 | K8sNode existing = nodeAdminService.node( |
| 168 | nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE)); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 169 | |
| 170 | if (existing == null) { |
| 171 | log.warn("There is no node configuration to delete : {}", hostname); |
| 172 | return Response.notModified().build(); |
| 173 | } else { |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 174 | nodeAdminService.removeNode(hostname); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | return Response.noContent().build(); |
| 178 | } |
| 179 | |
Jian Li | 1e03e71 | 2019-06-20 18:13:48 +0900 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Obtains the state of the kubernetes node. |
| 183 | * |
| 184 | * @param hostname hostname of the kubernetes |
| 185 | * @return the state of the kubernetes node in Json |
| 186 | */ |
| 187 | @GET |
| 188 | @Produces(MediaType.APPLICATION_JSON) |
| 189 | @Path("state/{hostname}") |
| 190 | public Response stateOfNode(@PathParam("hostname") String hostname) { |
| 191 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 192 | |
| 193 | K8sNode k8sNode = nodeAdminService.node(hostname); |
| 194 | String nodeState = k8sNode != null ? k8sNode.state().toString() : NOT_EXIST; |
| 195 | |
| 196 | return ok(mapper().createObjectNode().put(STATE, nodeState)).build(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Initializes kubernetes node. |
| 201 | * |
| 202 | * @param hostname hostname of kubernetes node |
| 203 | * @return 200 OK with init result, 404 not found, 500 server error |
| 204 | */ |
| 205 | @GET |
| 206 | @Produces(MediaType.APPLICATION_JSON) |
| 207 | @Path("init/node/{hostname}") |
| 208 | public Response initNode(@PathParam("hostname") String hostname) { |
| 209 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 210 | |
| 211 | K8sNode k8sNode = nodeAdminService.node(hostname); |
| 212 | if (k8sNode == null) { |
| 213 | log.error("Given node {} does not exist", hostname); |
| 214 | return Response.serverError().build(); |
| 215 | } |
| 216 | K8sNode updated = k8sNode.updateState(K8sNodeState.INIT); |
| 217 | nodeAdminService.updateNode(updated); |
| 218 | return ok(mapper().createObjectNode()).build(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Initializes all kubernetes nodes. |
| 223 | * |
| 224 | * @return 200 OK with init result, 500 server error |
| 225 | */ |
| 226 | @GET |
| 227 | @Produces(MediaType.APPLICATION_JSON) |
| 228 | @Path("init/all") |
| 229 | public Response initAllNodes() { |
| 230 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 231 | |
| 232 | nodeAdminService.nodes() |
| 233 | .forEach(n -> { |
| 234 | K8sNode updated = n.updateState(K8sNodeState.INIT); |
| 235 | nodeAdminService.updateNode(updated); |
| 236 | }); |
| 237 | |
| 238 | return ok(mapper().createObjectNode()).build(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Initializes kubernetes nodes which are in the stats other than COMPLETE. |
| 243 | * |
| 244 | * @return 200 OK with init result, 500 server error |
| 245 | */ |
| 246 | @GET |
| 247 | @Produces(MediaType.APPLICATION_JSON) |
| 248 | @Path("init/incomplete") |
| 249 | public Response initIncompleteNodes() { |
| 250 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 251 | |
| 252 | nodeAdminService.nodes().stream() |
| 253 | .filter(n -> n.state() != K8sNodeState.COMPLETE) |
| 254 | .forEach(n -> { |
| 255 | K8sNode updated = n.updateState(K8sNodeState.INIT); |
| 256 | nodeAdminService.updateNode(updated); |
| 257 | }); |
| 258 | |
| 259 | return ok(mapper().createObjectNode()).build(); |
| 260 | } |
| 261 | |
Jian Li | d376e06 | 2020-01-02 23:57:13 +0900 | [diff] [blame] | 262 | /** |
| 263 | * Updates a kubernetes nodes' state as post-on-board. |
| 264 | * |
| 265 | * @param hostname kubernetes node name |
| 266 | * @return 200 OK with the updated kubernetes node's config, 400 BAD_REQUEST |
| 267 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
| 268 | */ |
| 269 | @PUT |
Jian Li | 83eb2a7 | 2020-01-03 02:09:03 +0900 | [diff] [blame] | 270 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | d376e06 | 2020-01-02 23:57:13 +0900 | [diff] [blame] | 271 | @Produces(MediaType.APPLICATION_JSON) |
| 272 | @Path("update/postonboard/{hostname}") |
| 273 | public Response postOnBoardNode(@PathParam("hostname") String hostname) { |
Jian Li | 9bb9841 | 2020-01-03 03:27:06 +0900 | [diff] [blame] | 274 | K8sNode node = nodeAdminService.node(hostname); |
| 275 | if (node != null && node.state() != POST_ON_BOARD) { |
| 276 | K8sNode updated = node.updateState(POST_ON_BOARD); |
| 277 | nodeAdminService.updateNode(updated); |
| 278 | } |
Jian Li | d376e06 | 2020-01-02 23:57:13 +0900 | [diff] [blame] | 279 | return Response.ok().build(); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Indicates whether all kubernetes nodes are in post-on-board state. |
| 284 | * |
| 285 | * @return 200 OK with True, or 200 OK with False |
| 286 | */ |
| 287 | @GET |
| 288 | @Produces(MediaType.APPLICATION_JSON) |
| 289 | @Path("get/postonboard/all") |
| 290 | public Response postOnBoardNodes() { |
| 291 | long numOfAllNodes = nodeAdminService.nodes().size(); |
| 292 | long numOfReadyNodes = nodeAdminService.nodes().stream() |
| 293 | .filter(n -> n.state() == POST_ON_BOARD) |
| 294 | .count(); |
Jian Li | 8cc1599 | 2020-01-03 10:29:41 +0900 | [diff] [blame] | 295 | boolean result; |
| 296 | if (numOfAllNodes == 0) { |
| 297 | result = false; |
| 298 | } else { |
| 299 | result = numOfAllNodes == numOfReadyNodes; |
| 300 | } |
Jian Li | d376e06 | 2020-01-02 23:57:13 +0900 | [diff] [blame] | 301 | |
| 302 | return ok(mapper().createObjectNode().put(RESULT, result)).build(); |
| 303 | } |
| 304 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 305 | /** |
| 306 | * Creates a set of kubernetes API config from the JSON input stream. |
| 307 | * |
| 308 | * @param input kubernetes API configs JSON input stream |
| 309 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 310 | * is malformed |
| 311 | * @onos.rsModel K8sApiConfig |
| 312 | */ |
| 313 | @POST |
| 314 | @Path("api") |
| 315 | @Consumes(MediaType.APPLICATION_JSON) |
| 316 | @Produces(MediaType.APPLICATION_JSON) |
| 317 | public Response createApiConfigs(InputStream input) { |
| 318 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 319 | |
| 320 | readApiConfigConfiguration(input).forEach(config -> { |
| 321 | K8sApiConfig existing = configAdminService.apiConfig(endpoint(config)); |
| 322 | if (existing == null) { |
| 323 | configAdminService.createApiConfig(config); |
| 324 | } |
| 325 | }); |
| 326 | |
| 327 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 328 | .path(API_CONFIGS); |
| 329 | |
| 330 | return created(locationBuilder.build()).build(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Updates a set of kubernetes API config from the JSON input stream. |
| 335 | * |
| 336 | * @param input kubernetes API configs JSON input stream |
| 337 | * @return 200 OK with the updated kubernetes API config, 400 BAD_REQUEST |
| 338 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
| 339 | * @onos.rsModel K8sApiConfig |
| 340 | */ |
| 341 | @PUT |
| 342 | @Path("api") |
| 343 | @Consumes(MediaType.APPLICATION_JSON) |
| 344 | @Produces(MediaType.APPLICATION_JSON) |
| 345 | public Response updateApiConfigs(InputStream input) { |
| 346 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
| 347 | |
| 348 | Set<K8sApiConfig> configs = readApiConfigConfiguration(input); |
| 349 | for (K8sApiConfig config: configs) { |
| 350 | K8sApiConfig existing = configAdminService.apiConfig(endpoint(config)); |
| 351 | if (existing == null) { |
| 352 | log.warn("There is no API configuration to update : {}", endpoint(config)); |
| 353 | return Response.notModified().build(); |
| 354 | } else if (!existing.equals(config)) { |
| 355 | configAdminService.updateApiConfig(config); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return Response.ok().build(); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Removes a kubernetes API config. |
| 364 | * |
| 365 | * @param endpoint kubernetes API endpoint |
| 366 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed |
| 367 | */ |
| 368 | @DELETE |
| 369 | @Path("api/{endpoint : .+}") |
| 370 | @Consumes(MediaType.APPLICATION_JSON) |
| 371 | @Produces(MediaType.APPLICATION_JSON) |
| 372 | public Response deleteApiConfig(@PathParam("endpoint") String endpoint) { |
| 373 | log.trace(String.format(MESSAGE_NODE, REMOVE)); |
| 374 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 375 | K8sApiConfig existing = configAdminService.apiConfig( |
| 376 | nullIsIllegal(endpoint, ENDPOINT + ERROR_MESSAGE)); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 377 | |
| 378 | if (existing == null) { |
| 379 | log.warn("There is no API configuration to delete : {}", endpoint); |
| 380 | return Response.notModified().build(); |
| 381 | } else { |
| 382 | configAdminService.removeApiConfig(endpoint); |
| 383 | } |
| 384 | |
| 385 | return Response.noContent().build(); |
| 386 | } |
| 387 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 388 | /** |
| 389 | * Creates a set of kubernetes hosts' config from the JSON input stream. |
| 390 | * |
| 391 | * @param input kubernetes hosts JSON input stream |
| 392 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 393 | * is malformed |
| 394 | * @onos.rsModel K8sHosts |
| 395 | */ |
| 396 | @POST |
| 397 | @Path("host") |
| 398 | @Consumes(MediaType.APPLICATION_JSON) |
| 399 | @Produces(MediaType.APPLICATION_JSON) |
| 400 | public Response createHosts(InputStream input) { |
| 401 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 402 | |
| 403 | readHostsConfiguration(input).forEach(host -> { |
| 404 | K8sHost existing = hostAdminService.host(host.hostIp()); |
| 405 | if (existing == null) { |
| 406 | hostAdminService.createHost(host); |
| 407 | } |
| 408 | }); |
| 409 | |
| 410 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 411 | .path(HOSTS) |
| 412 | .path(HOST_IP); |
| 413 | |
| 414 | return created(locationBuilder.build()).build(); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Add a set of new nodes into the existing host. |
| 419 | * |
| 420 | * @param hostIp host IP address |
| 421 | * @param input kubernetes node names JSON input stream |
| 422 | * @return 200 UPDATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 423 | * is malformed |
| 424 | * @onos.rsModel K8sNodeNames |
| 425 | */ |
| 426 | @PUT |
| 427 | @Path("host/add/nodes/{hostIp}") |
| 428 | @Consumes(MediaType.APPLICATION_JSON) |
| 429 | @Produces(MediaType.APPLICATION_JSON) |
| 430 | public Response addNodesToHost(@PathParam("hostIp") String hostIp, |
| 431 | InputStream input) { |
| 432 | log.trace(String.format(MESSAGE_HOST, UPDATE)); |
| 433 | |
| 434 | Set<String> newNodeNames = readNodeNamesConfiguration(input); |
| 435 | K8sHost host = hostAdminService.host(IpAddress.valueOf(hostIp)); |
| 436 | Set<String> existNodeNames = host.nodeNames(); |
| 437 | existNodeNames.addAll(newNodeNames); |
| 438 | K8sHost updated = host.updateNodeNames(existNodeNames); |
| 439 | hostAdminService.updateHost(updated); |
| 440 | return Response.ok().build(); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Remove a set of new nodes from the existing host. |
| 445 | * |
| 446 | * @param hostIp host IP address |
| 447 | * @param input kubernetes node names JSON input stream |
| 448 | * @return 200 UPDATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 449 | * is malformed |
| 450 | * @onos.rsModel K8sNodeNames |
| 451 | */ |
| 452 | @PUT |
| 453 | @Path("host/delete/nodes/{hostIp}") |
| 454 | @Consumes(MediaType.APPLICATION_JSON) |
| 455 | @Produces(MediaType.APPLICATION_JSON) |
| 456 | public Response removeNodesFromHost(@PathParam("hostIp") String hostIp, |
| 457 | InputStream input) { |
| 458 | log.trace(String.format(MESSAGE_HOST, UPDATE)); |
| 459 | |
| 460 | Set<String> newNodeNames = readNodeNamesConfiguration(input); |
| 461 | K8sHost host = hostAdminService.host(IpAddress.valueOf(hostIp)); |
| 462 | Set<String> existNodeNames = host.nodeNames(); |
| 463 | existNodeNames.removeAll(newNodeNames); |
| 464 | K8sHost updated = host.updateNodeNames(existNodeNames); |
| 465 | hostAdminService.updateHost(updated); |
| 466 | return Response.ok().build(); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Removes a kubernetes host' config. |
| 471 | * |
| 472 | * @param hostIp host IP contained in kubernetes nodes configuration |
| 473 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and |
| 474 | * 304 NOT_MODIFIED without the updated config |
| 475 | */ |
| 476 | @DELETE |
| 477 | @Path("host/{hostIp}") |
| 478 | @Consumes(MediaType.APPLICATION_JSON) |
| 479 | @Produces(MediaType.APPLICATION_JSON) |
| 480 | public Response deleteHost(@PathParam("hostIp") String hostIp) { |
| 481 | log.trace(String.format(MESSAGE_HOST, REMOVE)); |
| 482 | |
| 483 | K8sHost existing = hostAdminService.host(IpAddress.valueOf( |
| 484 | nullIsIllegal(hostIp, HOST_IP + ERROR_MESSAGE))); |
| 485 | |
| 486 | if (existing == null) { |
| 487 | log.warn("There is no host configuration to delete : {}", hostIp); |
| 488 | return Response.notModified().build(); |
| 489 | } else { |
| 490 | hostAdminService.removeHost(IpAddress.valueOf( |
| 491 | nullIsIllegal(hostIp, HOST_IP + ERROR_MESSAGE))); |
| 492 | } |
| 493 | |
| 494 | return Response.noContent().build(); |
| 495 | } |
| 496 | |
| 497 | private Set<K8sNode> readNodeConfiguration(InputStream input) { |
| 498 | Set<K8sNode> nodeSet = Sets.newHashSet(); |
| 499 | try { |
| 500 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 501 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
| 502 | nodes.forEach(node -> { |
| 503 | try { |
| 504 | ObjectNode objectNode = node.deepCopy(); |
| 505 | K8sNode k8sNode = |
| 506 | codec(K8sNode.class).decode(objectNode, this); |
| 507 | |
| 508 | nodeSet.add(k8sNode); |
| 509 | } catch (Exception e) { |
| 510 | log.error("Exception occurred due to {}", e); |
| 511 | throw new IllegalArgumentException(); |
| 512 | } |
| 513 | }); |
| 514 | } catch (Exception e) { |
| 515 | throw new IllegalArgumentException(e); |
| 516 | } |
| 517 | |
| 518 | return nodeSet; |
| 519 | } |
| 520 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 521 | private Set<K8sApiConfig> readApiConfigConfiguration(InputStream input) { |
| 522 | Set<K8sApiConfig> configSet = Sets.newHashSet(); |
| 523 | try { |
| 524 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 525 | ArrayNode configs = (ArrayNode) jsonTree.path(API_CONFIGS); |
| 526 | configs.forEach(config -> { |
| 527 | try { |
| 528 | ObjectNode objectNode = config.deepCopy(); |
| 529 | K8sApiConfig k8sApiConfig = |
| 530 | codec(K8sApiConfig.class).decode(objectNode, this); |
| 531 | |
| 532 | configSet.add(k8sApiConfig); |
| 533 | } catch (Exception e) { |
| 534 | log.error("Exception occurred due to {}", e); |
| 535 | throw new IllegalArgumentException(); |
| 536 | } |
| 537 | }); |
| 538 | } catch (Exception e) { |
| 539 | throw new IllegalArgumentException(e); |
| 540 | } |
| 541 | |
| 542 | return configSet; |
| 543 | } |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 544 | |
| 545 | private Set<K8sHost> readHostsConfiguration(InputStream input) { |
| 546 | Set<K8sHost> hostSet = new HashSet<>(); |
| 547 | try { |
| 548 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 549 | ArrayNode hosts = (ArrayNode) jsonTree.path(HOSTS); |
| 550 | hosts.forEach(host -> { |
| 551 | try { |
| 552 | ObjectNode objectNode = host.deepCopy(); |
| 553 | K8sHost k8sHost = |
| 554 | codec(K8sHost.class).decode(objectNode, this); |
| 555 | |
| 556 | hostSet.add(k8sHost); |
| 557 | } catch (Exception e) { |
| 558 | log.error("Exception occurred due to {}", e); |
| 559 | throw new IllegalArgumentException(); |
| 560 | } |
| 561 | }); |
| 562 | } catch (Exception e) { |
| 563 | throw new IllegalArgumentException(e); |
| 564 | } |
| 565 | |
| 566 | return hostSet; |
| 567 | } |
| 568 | |
| 569 | private Set<String> readNodeNamesConfiguration(InputStream input) { |
| 570 | Set<String> nodeNames = new HashSet<>(); |
| 571 | try { |
| 572 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 573 | ArrayNode names = (ArrayNode) jsonTree.path(NODE_NAMES); |
| 574 | names.forEach(name -> { |
| 575 | try { |
| 576 | ObjectNode objectNode = name.deepCopy(); |
| 577 | nodeNames.add(objectNode.asText()); |
| 578 | } catch (Exception e) { |
| 579 | log.error("Exception occurred due to {}", e); |
| 580 | throw new IllegalArgumentException(); |
| 581 | } |
| 582 | }); |
| 583 | } catch (Exception e) { |
| 584 | throw new IllegalArgumentException(e); |
| 585 | } |
| 586 | |
| 587 | return nodeNames; |
| 588 | } |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 589 | } |