blob: 7d125ca6001d079d985292fa7170c75200b20ebc [file] [log] [blame]
Hyunsun Moon4f02b3a2015-10-18 18:23:15 -07001/*
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
17package org.onosproject.cordvtn.cli;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cordvtn.CordVtnService;
25import org.onosproject.cordvtn.OvsdbNode;
26
27import java.util.Collections;
28import 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")
35public 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}