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