blob: 51641c4c683fdd0786f390ff86e3ba5ec284a690 [file] [log] [blame]
Hyunsun Moon8539b042015-11-07 22:08:43 -08001/*
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.CordVtnNode;
26
27import java.util.Collections;
28import 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")
35public 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 Moonb219fc42016-01-14 03:42:47 -080047 print("hostname=%s, ovsdb=%s, br-int=%s, phyPort=%s, localIp=%s, init=%s",
Hyunsun Moon8539b042015-11-07 22:08:43 -080048 node.hostname(),
49 node.ovsdbIp().toString() + ":" + node.ovsdbPort().toString(),
50 node.intBrId().toString(),
Hyunsun Moonb219fc42016-01-14 03:42:47 -080051 node.phyPortName(),
52 node.localIp().toString(),
Hyunsun Moon8539b042015-11-07 22:08:43 -080053 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 Moonb219fc42016-01-14 03:42:47 -080068 .put("phyPort", node.phyPortName())
69 .put("localIp", node.localIp().toString())
Hyunsun Moon8539b042015-11-07 22:08:43 -080070 .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}