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