Integrate CLI on a proper management interface
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / cli / HwvtepCacheDisplayCmd.java
1 /*
2  * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.ovsdb.hwvtepsouthbound.cli;
9
10 import java.io.PrintStream;
11 import java.util.Map;
12 import org.apache.karaf.shell.api.action.Action;
13 import org.apache.karaf.shell.api.action.Argument;
14 import org.apache.karaf.shell.api.action.Command;
15 import org.apache.karaf.shell.api.action.lifecycle.Reference;
16 import org.apache.karaf.shell.api.action.lifecycle.Service;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundProviderInfo;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
33 import org.opendaylight.yangtools.yang.binding.Identifiable;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 @Service
37 @Command(scope = "hwvtep", name = "cache", description = "Disply hwvtep cache")
38 public class HwvtepCacheDisplayCmd implements Action {
39
40     @Argument(name = "nodeid", description = "Node Id",
41             required = false, multiValued = false)
42     private String nodeid;
43
44     @Reference
45     private HwvtepSouthboundProviderInfo hwvtepSouthboundProvider;
46
47     private static final TopologyId HWVTEP_TOPOLOGY_ID = new TopologyId(new Uri("hwvtep:1"));
48     private static final String SEPERATOR = "#######################################################";
49     private static final String SECTION_SEPERATOR = "==================================================="
50             + "=============================";
51
52
53     @Override
54     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
55     public Object execute() throws Exception {
56         Map<InstanceIdentifier<Node>, HwvtepDeviceInfo> allConnectedInstances =
57                 hwvtepSouthboundProvider.getAllConnectedInstances();
58         if (nodeid == null) {
59             allConnectedInstances.entrySet().forEach(entry -> {
60                 System.out.println(SEPERATOR + " START " + SEPERATOR);
61                 print(entry.getKey(), entry.getValue());
62                 System.out.println(SEPERATOR + " END " + SEPERATOR);
63                 System.out.println();
64                 System.out.println();
65             });
66         } else {
67             System.out.println(SEPERATOR + " START " + SEPERATOR);
68             print(getIid(), allConnectedInstances.get(getIid()));
69             System.out.println(SEPERATOR + " END " + SEPERATOR);
70         }
71         return null;
72     }
73
74     private InstanceIdentifier<Node> getIid() {
75         NodeId nodeId = new NodeId(new Uri(nodeid));
76         NodeKey nodeKey = new NodeKey(nodeId);
77         TopologyKey topoKey = new TopologyKey(HWVTEP_TOPOLOGY_ID);
78         return InstanceIdentifier.builder(NetworkTopology.class)
79                 .child(Topology.class, topoKey)
80                 .child(Node.class, nodeKey)
81                 .build();
82     }
83
84     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
85     private static void print(InstanceIdentifier<Node> iid, HwvtepDeviceInfo deviceInfo) {
86         PrintStream printStream = System.out;
87         printStream.print("Printing for Node :  ");
88         printStream.println(iid.firstKeyOf(Node.class).getNodeId().getValue());
89
90         printStream.println(SECTION_SEPERATOR);
91         printStream.println("Config data");
92         printStream.println(SECTION_SEPERATOR);
93         deviceInfo.getConfigData().entrySet().forEach(entry -> {
94             printEntry(printStream, entry);
95         });
96
97         printStream.println(SECTION_SEPERATOR);
98         printStream.println("Oper data");
99         printStream.println(SECTION_SEPERATOR);
100         deviceInfo.getOperData().entrySet().forEach(entry -> {
101             printEntry(printStream, entry);
102         });
103
104         printStream.println(SECTION_SEPERATOR);
105         printStream.println("Uuid data");
106         printStream.println(SECTION_SEPERATOR);
107         deviceInfo.getUuidData().entrySet().forEach(entry -> {
108             printEntryUUID(printStream, entry);
109         });
110         printStream.println(SECTION_SEPERATOR);
111         printStream.println(SECTION_SEPERATOR);
112
113     }
114
115     private static void printEntry(PrintStream console, Map.Entry<Class<? extends Identifiable>,
116             Map<InstanceIdentifier, HwvtepDeviceInfo.DeviceData>> entry) {
117         Class<? extends Identifiable> cls = entry.getKey();
118         Map<InstanceIdentifier, HwvtepDeviceInfo.DeviceData> map = entry.getValue();
119         String clsName = cls.getSimpleName();
120         console.println(clsName + " - ");
121         map.values().forEach(deviceData -> {
122             printTable(console, clsName, deviceData);
123         });
124     }
125
126     private static void printTable(PrintStream console, String clsName, HwvtepDeviceInfo.DeviceData deviceData) {
127         console.print("    ");
128         if (clsName.equals("LogicalSwitches")) {
129             printLogicalSwitches(console, deviceData);
130         } else if (clsName.equals("RemoteMcastMacs")) {
131             printRemoteMcasts(console, deviceData);
132         } else if (clsName.equals("RemoteUcastMacs")) {
133             printRemoteUcasts(console, deviceData);
134         } else if (clsName.equals("TerminationPoint") || clsName.equals("VlanBindings")) {
135             printTerminationPoint(console, deviceData);
136         } else if (clsName.equals("Node")) {
137             printNode(console, deviceData);
138         } else {
139             printCommon(console, deviceData);
140         }
141
142         if (deviceData.getData() == null && deviceData.getStatus() != HwvtepDeviceInfo.DeviceDataStatus.IN_TRANSIT) {
143             console.print("data null unexpected ");
144         }
145         console.print(" ");
146         console.print(deviceData.getStatus());
147         console.print(" ");
148         console.println(deviceData.getUuid());
149     }
150
151     private static void printLogicalSwitches(PrintStream console, HwvtepDeviceInfo.DeviceData deviceData) {
152         InstanceIdentifier<LogicalSwitches> ls = deviceData.getKey();
153         console.print(ls.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue());
154     }
155
156     private static void printRemoteMcasts(PrintStream console, HwvtepDeviceInfo.DeviceData deviceData) {
157         InstanceIdentifier<RemoteMcastMacs> remoteMcastMacsIid = deviceData.getKey();
158         String macAddress = remoteMcastMacsIid.firstKeyOf(RemoteMcastMacs.class).getMacEntryKey().getValue();
159         String logicalSwitchRef = remoteMcastMacsIid.firstKeyOf(RemoteMcastMacs.class).getLogicalSwitchRef().getValue()
160                 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
161         StringBuilder macEntryDetails = new StringBuilder(macAddress).append("   LogicalSwitchRef  ")
162                 .append(logicalSwitchRef);
163         console.print(macEntryDetails);
164     }
165
166     private static void printRemoteUcasts(PrintStream console, HwvtepDeviceInfo.DeviceData deviceData) {
167         InstanceIdentifier<RemoteUcastMacs> remoteUcastMacsIid = deviceData.getKey();
168         String macAddress = remoteUcastMacsIid.firstKeyOf(RemoteUcastMacs.class).getMacEntryKey().getValue();
169         String logicalSwitchRef = remoteUcastMacsIid.firstKeyOf(RemoteUcastMacs.class).getLogicalSwitchRef().getValue()
170                 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
171         StringBuilder macEntryDetails = new StringBuilder(macAddress).append("   LogicalSwitchRef  ")
172                 .append(logicalSwitchRef);
173         console.print(macEntryDetails);
174     }
175
176     private static void printTerminationPoint(PrintStream console, HwvtepDeviceInfo.DeviceData deviceData) {
177         InstanceIdentifier<TerminationPoint> terminationPointIid = deviceData.getKey();
178         console.print(terminationPointIid.firstKeyOf(TerminationPoint.class).getTpId().getValue());
179         try {
180             PhysicalPort physicalPort = (PhysicalPort) deviceData.getData();
181             console.print("    " + physicalPort.getVlanBindingsColumn().getData().keySet());
182         } catch (ClassCastException exp) {
183             //This is the Karaf cli display command , ignore the exception.
184         }
185     }
186
187     private static void printNode(PrintStream console, HwvtepDeviceInfo.DeviceData deviceData) {
188         InstanceIdentifier<Node> ls = deviceData.getKey();
189         console.print(ls.firstKeyOf(Node.class).getNodeId().getValue());
190     }
191
192     private static void printCommon(PrintStream console, HwvtepDeviceInfo.DeviceData deviceData) {
193         console.print(deviceData.getKey());
194         console.print(" ");
195         if (deviceData.getData() == null && deviceData.getStatus() != HwvtepDeviceInfo.DeviceDataStatus.IN_TRANSIT) {
196             console.print("data null unexpected ");
197         }
198         console.print(deviceData.getStatus());
199         console.print(" ");
200         console.println(deviceData.getUuid());
201     }
202
203     private static void printEntryUUID(PrintStream console, Map.Entry<Class<? extends Identifiable>, Map<UUID,
204             HwvtepDeviceInfo.DeviceData>> entry) {
205         Class<? extends Identifiable> cls = entry.getKey();
206         Map<UUID, HwvtepDeviceInfo.DeviceData> map = entry.getValue();
207         String clsName = cls.getSimpleName();
208         console.println(clsName + " - ");
209         map.values().forEach(deviceData -> {
210             printTable(console, clsName, deviceData);
211         });
212     }
213 }