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 | */ |
| 16 | |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 17 | package org.opencord.cordvtn.cli; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 24 | import org.opencord.cordvtn.impl.CordVtnNodeManager; |
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 | |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Lists all nodes registered to the service. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "cordvtn-nodes", |
| 34 | description = "Lists all nodes registered in CORD VTN service") |
| 35 | public class CordVtnNodeListCommand extends AbstractShellCommand { |
| 36 | |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 37 | private static final String COMPLETE = "COMPLETE"; |
| 38 | private static final String INCOMPLETE = "INCOMPLETE"; |
| 39 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 40 | @Override |
| 41 | protected void execute() { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 42 | CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class); |
| 43 | List<CordVtnNode> nodes = nodeManager.getNodes(); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 44 | Collections.sort(nodes, CordVtnNode.CORDVTN_NODE_COMPARATOR); |
| 45 | |
| 46 | if (outputJson()) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 47 | print("%s", json(nodeManager, nodes)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 48 | } else { |
| 49 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 50 | print("hostname=%s, hostMgmtIp=%s, dataIp=%s, br-int=%s, dataIface=%s, init=%s", |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 51 | node.hostname(), |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 52 | node.hostMgmtIp().cidr(), |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 53 | node.dataIp().cidr(), |
| 54 | node.integrationBridgeId().toString(), |
| 55 | node.dataIface(), |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 56 | getState(nodeManager, node)); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 57 | } |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 58 | print("Total %s nodes", nodeManager.getNodeCount()); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 62 | private JsonNode json(CordVtnNodeManager nodeManager, List<CordVtnNode> nodes) { |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 63 | ObjectMapper mapper = new ObjectMapper(); |
| 64 | ArrayNode result = mapper.createArrayNode(); |
| 65 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 66 | result.add(mapper.createObjectNode() |
| 67 | .put("hostname", node.hostname()) |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 68 | .put("hostManagementIp", node.hostMgmtIp().cidr()) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 69 | .put("dataPlaneIp", node.dataIp().cidr()) |
| 70 | .put("bridgeId", node.integrationBridgeId().toString()) |
| 71 | .put("dataPlaneInterface", node.dataIface()) |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 72 | .put("init", getState(nodeManager, node))); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 77 | private String getState(CordVtnNodeManager nodeManager, CordVtnNode node) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 78 | return nodeManager.isNodeInitComplete(node) ? COMPLETE : INCOMPLETE; |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 79 | } |
| 80 | } |