Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.cordvtn.cli; |
| 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; |
| 24 | import org.onosproject.cordvtn.CordVtnService; |
| 25 | import org.onosproject.cordvtn.CordVtnNode; |
| 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 | |
| 37 | @Override |
| 38 | protected void execute() { |
| 39 | CordVtnService service = AbstractShellCommand.get(CordVtnService.class); |
| 40 | List<CordVtnNode> nodes = service.getNodes(); |
| 41 | Collections.sort(nodes, CordVtnNode.CORDVTN_NODE_COMPARATOR); |
| 42 | |
| 43 | if (outputJson()) { |
| 44 | print("%s", json(service, nodes)); |
| 45 | } else { |
| 46 | for (CordVtnNode node : nodes) { |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 47 | print("hostname=%s, ovsdb=%s, br-int=%s, phyPort=%s, localIp=%s, init=%s", |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 48 | node.hostname(), |
| 49 | node.ovsdbIp().toString() + ":" + node.ovsdbPort().toString(), |
| 50 | node.intBrId().toString(), |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 51 | node.phyPortName(), |
| 52 | node.localIp().toString(), |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 53 | getState(service, node)); |
| 54 | } |
| 55 | print("Total %s nodes", service.getNodeCount()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private JsonNode json(CordVtnService service, List<CordVtnNode> nodes) { |
| 60 | ObjectMapper mapper = new ObjectMapper(); |
| 61 | ArrayNode result = mapper.createArrayNode(); |
| 62 | for (CordVtnNode node : nodes) { |
| 63 | String ipPort = node.ovsdbIp().toString() + ":" + node.ovsdbPort().toString(); |
| 64 | result.add(mapper.createObjectNode() |
| 65 | .put("hostname", node.hostname()) |
| 66 | .put("ovsdb", ipPort) |
| 67 | .put("brInt", node.intBrId().toString()) |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 68 | .put("phyPort", node.phyPortName()) |
| 69 | .put("localIp", node.localIp().toString()) |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 70 | .put("init", getState(service, node))); |
| 71 | } |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | private String getState(CordVtnService service, CordVtnNode node) { |
| 76 | return service.getNodeInitState(node) ? "COMPLETE" : "INCOMPLETE"; |
| 77 | } |
| 78 | } |