Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 23 | import org.opencord.cordvtn.impl.CordVtnNodeManager; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 24 | import org.opencord.cordvtn.api.node.CordVtnNode; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 25 | |
| 26 | import java.util.Collections; |
| 27 | import java.util.List; |
| 28 | |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 29 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 30 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Lists all nodes registered to the service. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "cordvtn-nodes", |
| 35 | description = "Lists all nodes registered in CORD VTN service") |
| 36 | public class CordVtnNodeListCommand extends AbstractShellCommand { |
| 37 | |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 38 | private static final String COMPLETE = "COMPLETE"; |
| 39 | private static final String INCOMPLETE = "INCOMPLETE"; |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 40 | private static final String FORMAT = "%-30s%-20s%-20s%-15s%-24s%s"; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 41 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 42 | @Override |
| 43 | protected void execute() { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 44 | CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class); |
| 45 | List<CordVtnNode> nodes = nodeManager.getNodes(); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 46 | Collections.sort(nodes, CordVtnNode.CORDVTN_NODE_COMPARATOR); |
| 47 | |
| 48 | if (outputJson()) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 49 | try { |
| 50 | print("%s", mapper().writeValueAsString(json(nodeManager, nodes))); |
| 51 | } catch (JsonProcessingException e) { |
| 52 | print("Failed to list networks in JSON format"); |
| 53 | } |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 54 | } else { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 55 | print(FORMAT, "Hostname", "Management IP", "Data IP", "Data Iface", |
| 56 | "Br-int", "State"); |
| 57 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 58 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 59 | print(FORMAT, node.hostname(), |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 60 | node.hostMgmtIp().cidr(), |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 61 | node.dataIp().cidr(), |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 62 | node.dataIface(), |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 63 | node.integrationBridgeId().toString(), |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 64 | getState(nodeManager, node)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 65 | } |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 66 | print("Total %s nodes", nodeManager.getNodeCount()); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 70 | private JsonNode json(CordVtnNodeManager nodeManager, List<CordVtnNode> nodes) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 71 | ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode(); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 72 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 73 | result.add(mapper().createObjectNode() |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 74 | .put("hostname", node.hostname()) |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 75 | .put("managementIp", node.hostMgmtIp().cidr()) |
| 76 | .put("dataIp", node.dataIp().cidr()) |
| 77 | .put("dataInterface", node.dataIface()) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 78 | .put("bridgeId", node.integrationBridgeId().toString()) |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame^] | 79 | .put("state", getState(nodeManager, node))); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 84 | private String getState(CordVtnNodeManager nodeManager, CordVtnNode node) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 85 | return nodeManager.isNodeInitComplete(node) ? COMPLETE : INCOMPLETE; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 86 | } |
| 87 | } |