blob: fea3d870d5fba25208e6c42eae607aea471aabb0 [file] [log] [blame]
Hyunsun Moon4edb0172015-11-07 22:08:43 -08001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2015-present Open Networking Foundation
Hyunsun Moon4edb0172015-11-07 22:08:43 -08003 *
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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.cli;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080017
Hyunsun Moon6066bd32016-10-24 15:35:34 -070018import com.fasterxml.jackson.core.JsonProcessingException;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080019import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080020import com.fasterxml.jackson.databind.node.ArrayNode;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090021import com.google.common.collect.Lists;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080022import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090024import org.opencord.cordvtn.api.node.CordVtnNodeService;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070025import org.opencord.cordvtn.api.node.CordVtnNode;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080026
Hyunsun Moon851e57f2017-02-23 19:38:40 +090027import java.util.Comparator;
Hyunsun Moon4edb0172015-11-07 22:08:43 -080028import java.util.List;
29
Hyunsun Moon6066bd32016-10-24 15:35:34 -070030import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
31
Hyunsun Moon4edb0172015-11-07 22:08:43 -080032/**
33 * Lists all nodes registered to the service.
34 */
35@Command(scope = "onos", name = "cordvtn-nodes",
36 description = "Lists all nodes registered in CORD VTN service")
37public class CordVtnNodeListCommand extends AbstractShellCommand {
38
Hyunsun Moon6066bd32016-10-24 15:35:34 -070039 private static final String FORMAT = "%-30s%-20s%-20s%-15s%-24s%s";
Hyunsun Moon126171d2016-02-09 01:55:48 -080040
Hyunsun Moon4edb0172015-11-07 22:08:43 -080041 @Override
42 protected void execute() {
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090043 CordVtnNodeService nodeService = AbstractShellCommand.get(CordVtnNodeService.class);
44 List<CordVtnNode> nodes = Lists.newArrayList(nodeService.nodes());
Hyunsun Moon851e57f2017-02-23 19:38:40 +090045 nodes.sort(Comparator.comparing(CordVtnNode::hostname));
Hyunsun Moon4edb0172015-11-07 22:08:43 -080046
47 if (outputJson()) {
Hyunsun Moon6066bd32016-10-24 15:35:34 -070048 try {
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090049 print("%s", mapper().writeValueAsString(json(nodes)));
Hyunsun Moon6066bd32016-10-24 15:35:34 -070050 } catch (JsonProcessingException e) {
51 print("Failed to list networks in JSON format");
52 }
Hyunsun Moon4edb0172015-11-07 22:08:43 -080053 } else {
Hyunsun Moon6066bd32016-10-24 15:35:34 -070054 print(FORMAT, "Hostname", "Management IP", "Data IP", "Data Iface",
55 "Br-int", "State");
56
Hyunsun Moon4edb0172015-11-07 22:08:43 -080057 for (CordVtnNode node : nodes) {
Hyunsun Moon6066bd32016-10-24 15:35:34 -070058 print(FORMAT, node.hostname(),
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090059 node.hostManagementIp().cidr(),
Hyunsun Moon81a13562016-08-04 13:48:08 -070060 node.dataIp().cidr(),
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090061 node.dataInterface(),
Hyunsun Moon6066bd32016-10-24 15:35:34 -070062 node.integrationBridgeId().toString(),
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090063 node.state().name());
Hyunsun Moon4edb0172015-11-07 22:08:43 -080064 }
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090065 print("Total %s nodes", nodes.size());
Hyunsun Moon4edb0172015-11-07 22:08:43 -080066 }
67 }
68
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090069 private JsonNode json(List<CordVtnNode> nodes) {
Hyunsun Moon6066bd32016-10-24 15:35:34 -070070 ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
Hyunsun Moon4edb0172015-11-07 22:08:43 -080071 for (CordVtnNode node : nodes) {
Hyunsun Moon6066bd32016-10-24 15:35:34 -070072 result.add(mapper().createObjectNode()
Hyunsun Moon4edb0172015-11-07 22:08:43 -080073 .put("hostname", node.hostname())
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090074 .put("managementIp", node.hostManagementIp().cidr())
Hyunsun Moon6066bd32016-10-24 15:35:34 -070075 .put("dataIp", node.dataIp().cidr())
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090076 .put("dataInterface", node.dataInterface())
Hyunsun Moon81a13562016-08-04 13:48:08 -070077 .put("bridgeId", node.integrationBridgeId().toString())
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090078 .put("state", node.state().name()));
Hyunsun Moon4edb0172015-11-07 22:08:43 -080079 }
80 return result;
81 }
Hyunsun Moon4edb0172015-11-07 22:08:43 -080082}