Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.cli; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 17 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 24 | import org.opencord.cordvtn.api.node.CordVtnNodeService; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 25 | import org.opencord.cordvtn.api.node.CordVtnNode; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 26 | |
Hyunsun Moon | 851e57f | 2017-02-23 19:38:40 +0900 | [diff] [blame] | 27 | import java.util.Comparator; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 30 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 31 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 32 | /** |
| 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") |
| 37 | public class CordVtnNodeListCommand extends AbstractShellCommand { |
| 38 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 39 | private static final String FORMAT = "%-30s%-20s%-20s%-15s%-24s%s"; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 40 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 41 | @Override |
| 42 | protected void execute() { |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 43 | CordVtnNodeService nodeService = AbstractShellCommand.get(CordVtnNodeService.class); |
| 44 | List<CordVtnNode> nodes = Lists.newArrayList(nodeService.nodes()); |
Hyunsun Moon | 851e57f | 2017-02-23 19:38:40 +0900 | [diff] [blame] | 45 | nodes.sort(Comparator.comparing(CordVtnNode::hostname)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 46 | |
| 47 | if (outputJson()) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 48 | try { |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 49 | print("%s", mapper().writeValueAsString(json(nodes))); |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 50 | } catch (JsonProcessingException e) { |
| 51 | print("Failed to list networks in JSON format"); |
| 52 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 53 | } else { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 54 | print(FORMAT, "Hostname", "Management IP", "Data IP", "Data Iface", |
| 55 | "Br-int", "State"); |
| 56 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 57 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 58 | print(FORMAT, node.hostname(), |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 59 | node.hostManagementIp().cidr(), |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 60 | node.dataIp().cidr(), |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 61 | node.dataInterface(), |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 62 | node.integrationBridgeId().toString(), |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 63 | node.state().name()); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 64 | } |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 65 | print("Total %s nodes", nodes.size()); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 69 | private JsonNode json(List<CordVtnNode> nodes) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 70 | ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode(); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 71 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 72 | result.add(mapper().createObjectNode() |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 73 | .put("hostname", node.hostname()) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 74 | .put("managementIp", node.hostManagementIp().cidr()) |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 75 | .put("dataIp", node.dataIp().cidr()) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 76 | .put("dataInterface", node.dataInterface()) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 77 | .put("bridgeId", node.integrationBridgeId().toString()) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 78 | .put("state", node.state().name())); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 79 | } |
| 80 | return result; |
| 81 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 82 | } |