Hyunsun Moon | 4f02b3a | 2015-10-18 18:23:15 -0700 | [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.OvsdbNode; |
| 26 | |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Lists all OVSDB nodes. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "ovsdbs", |
| 34 | description = "Lists all OVSDB nodes registered in cordvtn application") |
| 35 | public class OvsdbNodeListCommand extends AbstractShellCommand { |
| 36 | |
| 37 | @Override |
| 38 | protected void execute() { |
| 39 | CordVtnService service = AbstractShellCommand.get(CordVtnService.class); |
| 40 | List<OvsdbNode> ovsdbs = service.getNodes(); |
| 41 | Collections.sort(ovsdbs, OvsdbNode.OVSDB_NODE_COMPARATOR); |
| 42 | |
| 43 | if (outputJson()) { |
| 44 | print("%s", json(service, ovsdbs)); |
| 45 | } else { |
| 46 | for (OvsdbNode ovsdb : ovsdbs) { |
| 47 | print("host=%s, address=%s, br-int=%s, state=%s", |
| 48 | ovsdb.host(), |
| 49 | ovsdb.ip().toString() + ":" + ovsdb.port().toString(), |
| 50 | ovsdb.intBrId().toString(), |
| 51 | getState(service, ovsdb)); |
| 52 | } |
| 53 | print("Total %s nodes", service.getNodeCount()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private JsonNode json(CordVtnService service, List<OvsdbNode> ovsdbs) { |
| 58 | ObjectMapper mapper = new ObjectMapper(); |
| 59 | ArrayNode result = mapper.createArrayNode(); |
| 60 | for (OvsdbNode ovsdb : ovsdbs) { |
| 61 | String ipPort = ovsdb.ip().toString() + ":" + ovsdb.port().toString(); |
| 62 | result.add(mapper.createObjectNode() |
| 63 | .put("host", ovsdb.host()) |
| 64 | .put("address", ipPort) |
| 65 | .put("brInt", ovsdb.intBrId().toString()) |
| 66 | .put("state", getState(service, ovsdb))); |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | private String getState(CordVtnService service, OvsdbNode ovsdb) { |
| 72 | return service.isNodeConnected(ovsdb) ? "CONNECTED" : "DISCONNECTED"; |
| 73 | } |
| 74 | } |