blob: 971dd686275e99b1665ac43a6dbc877a82ffcfc2 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +09001/*
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 */
16package org.onosproject.k8snode.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Sets;
Jian Lie2a04ce2020-07-01 19:07:02 +090022import org.onlab.packet.IpAddress;
Jian Li3defa842019-02-12 00:31:35 +090023import org.onosproject.k8snode.api.K8sApiConfig;
24import org.onosproject.k8snode.api.K8sApiConfigAdminService;
Jian Lie2a04ce2020-07-01 19:07:02 +090025import org.onosproject.k8snode.api.K8sHost;
26import org.onosproject.k8snode.api.K8sHostAdminService;
Jian Li49109b52019-01-22 00:17:28 +090027import org.onosproject.k8snode.api.K8sNode;
28import org.onosproject.k8snode.api.K8sNodeAdminService;
Jian Lid5fed162019-06-20 18:13:48 +090029import org.onosproject.k8snode.api.K8sNodeState;
Jian Li49109b52019-01-22 00:17:28 +090030import org.onosproject.rest.AbstractWebResource;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import javax.ws.rs.Consumes;
Jian Li3defa842019-02-12 00:31:35 +090035import javax.ws.rs.DELETE;
Jian Lid5fed162019-06-20 18:13:48 +090036import javax.ws.rs.GET;
Jian Li49109b52019-01-22 00:17:28 +090037import javax.ws.rs.POST;
38import javax.ws.rs.PUT;
39import javax.ws.rs.Path;
40import javax.ws.rs.PathParam;
41import javax.ws.rs.Produces;
42import javax.ws.rs.core.Context;
43import javax.ws.rs.core.MediaType;
44import javax.ws.rs.core.Response;
45import javax.ws.rs.core.UriBuilder;
46import javax.ws.rs.core.UriInfo;
47import java.io.InputStream;
Jian Lie2a04ce2020-07-01 19:07:02 +090048import java.util.HashSet;
Jian Li49109b52019-01-22 00:17:28 +090049import java.util.Set;
50
51import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
52import static javax.ws.rs.core.Response.created;
53import static org.onlab.util.Tools.nullIsIllegal;
54import static org.onlab.util.Tools.readTreeFromStream;
Jian Li3b640af2020-01-02 23:57:13 +090055import static org.onosproject.k8snode.api.K8sNodeState.POST_ON_BOARD;
Jian Li3defa842019-02-12 00:31:35 +090056import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint;
Jian Li49109b52019-01-22 00:17:28 +090057
58/**
59 * Handles REST API call of kubernetes node config.
60 */
61
62@Path("configure")
63public 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 Lie2a04ce2020-07-01 19:07:02 +090068 private static final String MESSAGE_HOST = "Received host %s request";
Jian Li49109b52019-01-22 00:17:28 +090069 private static final String NODES = "nodes";
Jian Li3defa842019-02-12 00:31:35 +090070 private static final String API_CONFIGS = "apiConfigs";
Jian Lie2a04ce2020-07-01 19:07:02 +090071 private static final String HOSTS = "hosts";
72 private static final String NODE_NAMES = "nodeNames";
Jian Li49109b52019-01-22 00:17:28 +090073 private static final String CREATE = "CREATE";
74 private static final String UPDATE = "UPDATE";
75 private static final String NODE_ID = "NODE_ID";
Jian Lie2a04ce2020-07-01 19:07:02 +090076 private static final String HOST_IP = "HOST_IP";
Jian Li3defa842019-02-12 00:31:35 +090077 private static final String REMOVE = "REMOVE";
Jian Lid5fed162019-06-20 18:13:48 +090078 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 Li3b640af2020-01-02 23:57:13 +090082 private static final String RESULT = "Result";
Jian Li49109b52019-01-22 00:17:28 +090083
84 private static final String HOST_NAME = "hostname";
Jian Li3defa842019-02-12 00:31:35 +090085 private static final String ENDPOINT = "endpoint";
Jian Li49109b52019-01-22 00:17:28 +090086 private static final String ERROR_MESSAGE = " cannot be null";
87
Jian Li3defa842019-02-12 00:31:35 +090088 private final K8sNodeAdminService nodeAdminService = get(K8sNodeAdminService.class);
Jian Lie2a04ce2020-07-01 19:07:02 +090089 private final K8sHostAdminService hostAdminService = get(K8sHostAdminService.class);
Jian Li3defa842019-02-12 00:31:35 +090090 private final K8sApiConfigAdminService configAdminService = get(K8sApiConfigAdminService.class);
Jian Li49109b52019-01-22 00:17:28 +090091
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 Li3defa842019-02-12 00:31:35 +0900104 @Path("node")
Jian Li49109b52019-01-22 00:17:28 +0900105 @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 Li3defa842019-02-12 00:31:35 +0900111 K8sNode existing = nodeAdminService.node(node.hostname());
Jian Li49109b52019-01-22 00:17:28 +0900112 if (existing == null) {
Jian Li3defa842019-02-12 00:31:35 +0900113 nodeAdminService.createNode(node);
Jian Li49109b52019-01-22 00:17:28 +0900114 }
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 Li3defa842019-02-12 00:31:35 +0900133 @Path("node")
Jian Li49109b52019-01-22 00:17:28 +0900134 @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 Li3defa842019-02-12 00:31:35 +0900141 K8sNode existing = nodeAdminService.node(node.hostname());
Jian Li49109b52019-01-22 00:17:28 +0900142 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 Li3defa842019-02-12 00:31:35 +0900146 nodeAdminService.updateNode(node);
Jian Li49109b52019-01-22 00:17:28 +0900147 }
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 Li49109b52019-01-22 00:17:28 +0900159 */
Jian Li3defa842019-02-12 00:31:35 +0900160 @DELETE
161 @Path("node/{hostname}")
Jian Li49109b52019-01-22 00:17:28 +0900162 @Consumes(MediaType.APPLICATION_JSON)
163 @Produces(MediaType.APPLICATION_JSON)
Jian Li49109b52019-01-22 00:17:28 +0900164 public Response deleteNodes(@PathParam("hostname") String hostname) {
Jian Li3defa842019-02-12 00:31:35 +0900165 log.trace(String.format(MESSAGE_NODE, REMOVE));
Jian Li49109b52019-01-22 00:17:28 +0900166
Jian Lie2a04ce2020-07-01 19:07:02 +0900167 K8sNode existing = nodeAdminService.node(
168 nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
Jian Li49109b52019-01-22 00:17:28 +0900169
170 if (existing == null) {
171 log.warn("There is no node configuration to delete : {}", hostname);
172 return Response.notModified().build();
173 } else {
Jian Li3defa842019-02-12 00:31:35 +0900174 nodeAdminService.removeNode(hostname);
Jian Li49109b52019-01-22 00:17:28 +0900175 }
176
177 return Response.noContent().build();
178 }
179
Jian Lidc1df642020-11-25 16:49:34 +0900180 /**
Jian Lid5fed162019-06-20 18:13:48 +0900181 * Obtains the state of the kubernetes node.
182 *
183 * @param hostname hostname of the kubernetes
184 * @return the state of the kubernetes node in Json
185 */
186 @GET
187 @Produces(MediaType.APPLICATION_JSON)
188 @Path("state/{hostname}")
189 public Response stateOfNode(@PathParam("hostname") String hostname) {
190 log.trace(String.format(MESSAGE_NODE, QUERY));
191
192 K8sNode k8sNode = nodeAdminService.node(hostname);
193 String nodeState = k8sNode != null ? k8sNode.state().toString() : NOT_EXIST;
194
195 return ok(mapper().createObjectNode().put(STATE, nodeState)).build();
196 }
197
198 /**
199 * Initializes kubernetes node.
200 *
201 * @param hostname hostname of kubernetes node
202 * @return 200 OK with init result, 404 not found, 500 server error
203 */
204 @GET
205 @Produces(MediaType.APPLICATION_JSON)
206 @Path("init/node/{hostname}")
207 public Response initNode(@PathParam("hostname") String hostname) {
208 log.trace(String.format(MESSAGE_NODE, QUERY));
209
210 K8sNode k8sNode = nodeAdminService.node(hostname);
211 if (k8sNode == null) {
212 log.error("Given node {} does not exist", hostname);
213 return Response.serverError().build();
214 }
215 K8sNode updated = k8sNode.updateState(K8sNodeState.INIT);
216 nodeAdminService.updateNode(updated);
217 return ok(mapper().createObjectNode()).build();
218 }
219
220 /**
221 * Initializes all kubernetes nodes.
222 *
223 * @return 200 OK with init result, 500 server error
224 */
225 @GET
226 @Produces(MediaType.APPLICATION_JSON)
227 @Path("init/all")
228 public Response initAllNodes() {
229 log.trace(String.format(MESSAGE_NODE, QUERY));
230
231 nodeAdminService.nodes()
232 .forEach(n -> {
233 K8sNode updated = n.updateState(K8sNodeState.INIT);
234 nodeAdminService.updateNode(updated);
235 });
236
237 return ok(mapper().createObjectNode()).build();
238 }
239
240 /**
241 * Initializes kubernetes nodes which are in the stats other than COMPLETE.
242 *
243 * @return 200 OK with init result, 500 server error
244 */
245 @GET
246 @Produces(MediaType.APPLICATION_JSON)
247 @Path("init/incomplete")
248 public Response initIncompleteNodes() {
249 log.trace(String.format(MESSAGE_NODE, QUERY));
250
251 nodeAdminService.nodes().stream()
252 .filter(n -> n.state() != K8sNodeState.COMPLETE)
253 .forEach(n -> {
254 K8sNode updated = n.updateState(K8sNodeState.INIT);
255 nodeAdminService.updateNode(updated);
256 });
257
258 return ok(mapper().createObjectNode()).build();
259 }
260
Jian Li3b640af2020-01-02 23:57:13 +0900261 /**
262 * Updates a kubernetes nodes' state as post-on-board.
263 *
264 * @param hostname kubernetes node name
265 * @return 200 OK with the updated kubernetes node's config, 400 BAD_REQUEST
266 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
267 */
268 @PUT
Jian Lic9799192020-01-03 02:09:03 +0900269 @Consumes(MediaType.APPLICATION_JSON)
Jian Li3b640af2020-01-02 23:57:13 +0900270 @Produces(MediaType.APPLICATION_JSON)
271 @Path("update/postonboard/{hostname}")
272 public Response postOnBoardNode(@PathParam("hostname") String hostname) {
Jian Li0a528842020-01-03 03:27:06 +0900273 K8sNode node = nodeAdminService.node(hostname);
274 if (node != null && node.state() != POST_ON_BOARD) {
275 K8sNode updated = node.updateState(POST_ON_BOARD);
276 nodeAdminService.updateNode(updated);
277 }
Jian Li3b640af2020-01-02 23:57:13 +0900278 return Response.ok().build();
279 }
280
281 /**
282 * Indicates whether all kubernetes nodes are in post-on-board state.
283 *
284 * @return 200 OK with True, or 200 OK with False
285 */
286 @GET
287 @Produces(MediaType.APPLICATION_JSON)
288 @Path("get/postonboard/all")
289 public Response postOnBoardNodes() {
290 long numOfAllNodes = nodeAdminService.nodes().size();
291 long numOfReadyNodes = nodeAdminService.nodes().stream()
292 .filter(n -> n.state() == POST_ON_BOARD)
293 .count();
Jian Li3db2bf52020-01-03 10:29:41 +0900294 boolean result;
295 if (numOfAllNodes == 0) {
296 result = false;
297 } else {
298 result = numOfAllNodes == numOfReadyNodes;
299 }
Jian Li3b640af2020-01-02 23:57:13 +0900300
301 return ok(mapper().createObjectNode().put(RESULT, result)).build();
302 }
303
Jian Li3defa842019-02-12 00:31:35 +0900304 /**
305 * Creates a set of kubernetes API config from the JSON input stream.
306 *
307 * @param input kubernetes API configs JSON input stream
308 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
309 * is malformed
310 * @onos.rsModel K8sApiConfig
311 */
312 @POST
313 @Path("api")
314 @Consumes(MediaType.APPLICATION_JSON)
315 @Produces(MediaType.APPLICATION_JSON)
316 public Response createApiConfigs(InputStream input) {
317 log.trace(String.format(MESSAGE_NODE, CREATE));
318
319 readApiConfigConfiguration(input).forEach(config -> {
320 K8sApiConfig existing = configAdminService.apiConfig(endpoint(config));
321 if (existing == null) {
322 configAdminService.createApiConfig(config);
323 }
324 });
325
326 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
327 .path(API_CONFIGS);
328
329 return created(locationBuilder.build()).build();
330 }
331
332 /**
333 * Updates a set of kubernetes API config from the JSON input stream.
334 *
335 * @param input kubernetes API configs JSON input stream
336 * @return 200 OK with the updated kubernetes API config, 400 BAD_REQUEST
337 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
338 * @onos.rsModel K8sApiConfig
339 */
340 @PUT
341 @Path("api")
342 @Consumes(MediaType.APPLICATION_JSON)
343 @Produces(MediaType.APPLICATION_JSON)
344 public Response updateApiConfigs(InputStream input) {
345 log.trace(String.format(MESSAGE_NODE, UPDATE));
346
347 Set<K8sApiConfig> configs = readApiConfigConfiguration(input);
348 for (K8sApiConfig config: configs) {
349 K8sApiConfig existing = configAdminService.apiConfig(endpoint(config));
350 if (existing == null) {
351 log.warn("There is no API configuration to update : {}", endpoint(config));
352 return Response.notModified().build();
353 } else if (!existing.equals(config)) {
354 configAdminService.updateApiConfig(config);
355 }
356 }
357
358 return Response.ok().build();
359 }
360
361 /**
362 * Removes a kubernetes API config.
363 *
364 * @param endpoint kubernetes API endpoint
365 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
366 */
367 @DELETE
368 @Path("api/{endpoint : .+}")
369 @Consumes(MediaType.APPLICATION_JSON)
370 @Produces(MediaType.APPLICATION_JSON)
371 public Response deleteApiConfig(@PathParam("endpoint") String endpoint) {
372 log.trace(String.format(MESSAGE_NODE, REMOVE));
373
Jian Lie2a04ce2020-07-01 19:07:02 +0900374 K8sApiConfig existing = configAdminService.apiConfig(
375 nullIsIllegal(endpoint, ENDPOINT + ERROR_MESSAGE));
Jian Li3defa842019-02-12 00:31:35 +0900376
377 if (existing == null) {
378 log.warn("There is no API configuration to delete : {}", endpoint);
379 return Response.notModified().build();
380 } else {
381 configAdminService.removeApiConfig(endpoint);
382 }
383
384 return Response.noContent().build();
385 }
386
Jian Lie2a04ce2020-07-01 19:07:02 +0900387 /**
388 * Creates a set of kubernetes hosts' config from the JSON input stream.
389 *
390 * @param input kubernetes hosts JSON input stream
391 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
392 * is malformed
393 * @onos.rsModel K8sHosts
394 */
395 @POST
396 @Path("host")
397 @Consumes(MediaType.APPLICATION_JSON)
398 @Produces(MediaType.APPLICATION_JSON)
399 public Response createHosts(InputStream input) {
400 log.trace(String.format(MESSAGE_NODE, CREATE));
401
402 readHostsConfiguration(input).forEach(host -> {
403 K8sHost existing = hostAdminService.host(host.hostIp());
404 if (existing == null) {
405 hostAdminService.createHost(host);
406 }
407 });
408
409 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
410 .path(HOSTS)
411 .path(HOST_IP);
412
413 return created(locationBuilder.build()).build();
414 }
415
416 /**
417 * Add a set of new nodes into the existing host.
418 *
419 * @param hostIp host IP address
420 * @param input kubernetes node names JSON input stream
421 * @return 200 UPDATED if the JSON is correct, 400 BAD_REQUEST if the JSON
422 * is malformed
423 * @onos.rsModel K8sNodeNames
424 */
425 @PUT
426 @Path("host/add/nodes/{hostIp}")
427 @Consumes(MediaType.APPLICATION_JSON)
428 @Produces(MediaType.APPLICATION_JSON)
429 public Response addNodesToHost(@PathParam("hostIp") String hostIp,
430 InputStream input) {
431 log.trace(String.format(MESSAGE_HOST, UPDATE));
432
433 Set<String> newNodeNames = readNodeNamesConfiguration(input);
434 K8sHost host = hostAdminService.host(IpAddress.valueOf(hostIp));
435 Set<String> existNodeNames = host.nodeNames();
436 existNodeNames.addAll(newNodeNames);
437 K8sHost updated = host.updateNodeNames(existNodeNames);
438 hostAdminService.updateHost(updated);
439 return Response.ok().build();
440 }
441
442 /**
443 * Remove a set of new nodes from the existing host.
444 *
445 * @param hostIp host IP address
446 * @param input kubernetes node names JSON input stream
447 * @return 200 UPDATED if the JSON is correct, 400 BAD_REQUEST if the JSON
448 * is malformed
449 * @onos.rsModel K8sNodeNames
450 */
451 @PUT
452 @Path("host/delete/nodes/{hostIp}")
453 @Consumes(MediaType.APPLICATION_JSON)
454 @Produces(MediaType.APPLICATION_JSON)
455 public Response removeNodesFromHost(@PathParam("hostIp") String hostIp,
456 InputStream input) {
457 log.trace(String.format(MESSAGE_HOST, UPDATE));
458
459 Set<String> newNodeNames = readNodeNamesConfiguration(input);
460 K8sHost host = hostAdminService.host(IpAddress.valueOf(hostIp));
461 Set<String> existNodeNames = host.nodeNames();
462 existNodeNames.removeAll(newNodeNames);
463 K8sHost updated = host.updateNodeNames(existNodeNames);
464 hostAdminService.updateHost(updated);
465 return Response.ok().build();
466 }
467
468 /**
469 * Removes a kubernetes host' config.
470 *
471 * @param hostIp host IP contained in kubernetes nodes configuration
472 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
473 * 304 NOT_MODIFIED without the updated config
474 */
475 @DELETE
476 @Path("host/{hostIp}")
477 @Consumes(MediaType.APPLICATION_JSON)
478 @Produces(MediaType.APPLICATION_JSON)
479 public Response deleteHost(@PathParam("hostIp") String hostIp) {
480 log.trace(String.format(MESSAGE_HOST, REMOVE));
481
482 K8sHost existing = hostAdminService.host(IpAddress.valueOf(
483 nullIsIllegal(hostIp, HOST_IP + ERROR_MESSAGE)));
484
485 if (existing == null) {
486 log.warn("There is no host configuration to delete : {}", hostIp);
487 return Response.notModified().build();
488 } else {
489 hostAdminService.removeHost(IpAddress.valueOf(
490 nullIsIllegal(hostIp, HOST_IP + ERROR_MESSAGE)));
491 }
492
493 return Response.noContent().build();
494 }
495
496 private Set<K8sNode> readNodeConfiguration(InputStream input) {
497 Set<K8sNode> nodeSet = Sets.newHashSet();
498 try {
499 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
500 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
501 nodes.forEach(node -> {
502 try {
503 ObjectNode objectNode = node.deepCopy();
504 K8sNode k8sNode =
505 codec(K8sNode.class).decode(objectNode, this);
506
507 nodeSet.add(k8sNode);
508 } catch (Exception e) {
509 log.error("Exception occurred due to {}", e);
510 throw new IllegalArgumentException();
511 }
512 });
513 } catch (Exception e) {
514 throw new IllegalArgumentException(e);
515 }
516
517 return nodeSet;
518 }
519
Jian Li3defa842019-02-12 00:31:35 +0900520 private Set<K8sApiConfig> readApiConfigConfiguration(InputStream input) {
521 Set<K8sApiConfig> configSet = Sets.newHashSet();
522 try {
523 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
524 ArrayNode configs = (ArrayNode) jsonTree.path(API_CONFIGS);
525 configs.forEach(config -> {
526 try {
527 ObjectNode objectNode = config.deepCopy();
528 K8sApiConfig k8sApiConfig =
529 codec(K8sApiConfig.class).decode(objectNode, this);
530
531 configSet.add(k8sApiConfig);
532 } catch (Exception e) {
533 log.error("Exception occurred due to {}", e);
534 throw new IllegalArgumentException();
535 }
536 });
537 } catch (Exception e) {
538 throw new IllegalArgumentException(e);
539 }
540
541 return configSet;
542 }
Jian Lie2a04ce2020-07-01 19:07:02 +0900543
544 private Set<K8sHost> readHostsConfiguration(InputStream input) {
545 Set<K8sHost> hostSet = new HashSet<>();
546 try {
547 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
548 ArrayNode hosts = (ArrayNode) jsonTree.path(HOSTS);
549 hosts.forEach(host -> {
550 try {
551 ObjectNode objectNode = host.deepCopy();
552 K8sHost k8sHost =
553 codec(K8sHost.class).decode(objectNode, this);
554
555 hostSet.add(k8sHost);
556 } catch (Exception e) {
557 log.error("Exception occurred due to {}", e);
558 throw new IllegalArgumentException();
559 }
560 });
561 } catch (Exception e) {
562 throw new IllegalArgumentException(e);
563 }
564
565 return hostSet;
566 }
567
568 private Set<String> readNodeNamesConfiguration(InputStream input) {
569 Set<String> nodeNames = new HashSet<>();
570 try {
571 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
572 ArrayNode names = (ArrayNode) jsonTree.path(NODE_NAMES);
573 names.forEach(name -> {
574 try {
575 ObjectNode objectNode = name.deepCopy();
576 nodeNames.add(objectNode.asText());
577 } catch (Exception e) {
578 log.error("Exception occurred due to {}", e);
579 throw new IllegalArgumentException();
580 }
581 });
582 } catch (Exception e) {
583 throw new IllegalArgumentException(e);
584 }
585
586 return nodeNames;
587 }
Jian Li49109b52019-01-22 00:17:28 +0900588}