Java 8 migration
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cli / l2gw / L2GwValidateCli.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.netvirt.elan.cli.l2gw;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Sets;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.File;
16 import java.io.FileOutputStream;
17 import java.io.PrintWriter;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import org.apache.karaf.shell.commands.Command;
27 import org.apache.karaf.shell.console.OsgiCommandSupport;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
33 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
34 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
35 import org.opendaylight.netvirt.elan.l2gw.ha.commands.LogicalSwitchesCmd;
36 import org.opendaylight.netvirt.elan.l2gw.ha.commands.MergeCommand;
37 import org.opendaylight.netvirt.elan.l2gw.ha.commands.RemoteMcastCmd;
38 import org.opendaylight.netvirt.elan.l2gw.ha.commands.RemoteUcastCmd;
39 import org.opendaylight.netvirt.elan.l2gw.ha.commands.TerminationPointCmd;
40 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
41 import org.opendaylight.netvirt.elan.utils.ElanConstants;
42 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
43 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
44 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
69 import org.opendaylight.yangtools.yang.binding.DataObject;
70 import org.opendaylight.yangtools.yang.binding.Identifier;
71 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75 @Command(scope = "l2gw", name = "validate", description = "Validates the hwvtep nodes data")
76 public class L2GwValidateCli extends OsgiCommandSupport {
77
78     private static final Logger LOG = LoggerFactory.getLogger(L2GwValidateCli.class);
79
80     private final MergeCommand[] globalCommands = new MergeCommand[]{new LogicalSwitchesCmd(), new RemoteUcastCmd(),
81         new RemoteMcastCmd()};
82     private final MergeCommand[] physicalSwitchCommands = new MergeCommand[]{new TerminationPointCmd()};
83
84     private final Map<InstanceIdentifier<Node>, Node> operationalNodes = new HashMap<>();
85     private final Map<InstanceIdentifier<Node>, Node> configNodes = new HashMap<>();
86     private final Map<String, ElanInstance> elanInstanceMap = new HashMap<>();
87     private final Map<Uuid, L2gateway> uuidToL2Gateway = new HashMap<>();
88     private final InstanceIdentifier<Topology> topoIid = HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier();
89     private final Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> operationalNodesData =
90             new HashMap<>();
91     private final Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> configNodesData =
92             new HashMap<>();
93
94     private final DataBroker dataBroker;
95     private final L2GatewayCache l2GatewayCache;
96
97     private List<L2gateway> l2gateways;
98     private List<L2gatewayConnection> l2gatewayConnections;
99
100     private PrintWriter pw;
101
102     public L2GwValidateCli(DataBroker dataBroker, L2GatewayCache l2GatewayCache) {
103         this.dataBroker = dataBroker;
104         this.l2GatewayCache = l2GatewayCache;
105     }
106
107     @Override
108     @SuppressFBWarnings("DM_DEFAULT_ENCODING")
109     public Object doExecute() throws Exception {
110         try {
111             pw = new PrintWriter(new FileOutputStream(new File("l2gw.validation.txt")));
112             readNodes();
113             verifyHANodes();
114             verifyConfigVsOperationalDiff();
115             verifyL2GatewayConnections();
116             pw.close();
117         } catch (ReadFailedException e) {
118             session.getConsole().println("Failed with error " + e.getMessage());
119             LOG.error("Failed with error ", e);
120         }
121         return null;
122     }
123
124     private void readNodes() throws ReadFailedException {
125         try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
126             InstanceIdentifier<Topology> topoId = HwvtepSouthboundUtils.createHwvtepTopologyInstanceIdentifier();
127
128             Optional<Topology> operationalTopoOptional = tx.read(LogicalDatastoreType.OPERATIONAL, topoId).checkedGet();
129             Optional<Topology> configTopoOptional = tx.read(LogicalDatastoreType.CONFIGURATION, topoId).checkedGet();
130
131             if (operationalTopoOptional.isPresent()) {
132                 for (Node node : operationalTopoOptional.get().getNode()) {
133                     InstanceIdentifier<Node> nodeIid = topoId.child(Node.class, node.getKey());
134                     operationalNodes.put(nodeIid, node);
135                 }
136             }
137             if (configTopoOptional.isPresent()) {
138                 for (Node node : configTopoOptional.get().getNode()) {
139                     InstanceIdentifier<Node> nodeIid = topoId.child(Node.class, node.getKey());
140                     configNodes.put(nodeIid, node);
141                 }
142             }
143
144             fillNodesData(operationalNodes, operationalNodesData);
145             fillNodesData(configNodes, configNodesData);
146
147             Optional<ElanInstances> elanInstancesOptional = tx.read(LogicalDatastoreType.CONFIGURATION,
148                     InstanceIdentifier.builder(ElanInstances.class).build()).checkedGet();
149
150             if (elanInstancesOptional.isPresent() && elanInstancesOptional.get().getElanInstance() != null) {
151                 for (ElanInstance elanInstance : elanInstancesOptional.get().getElanInstance()) {
152                     elanInstanceMap.put(elanInstance.getElanInstanceName(), elanInstance);
153                 }
154             }
155             l2gatewayConnections = L2GatewayConnectionUtils.getAllL2gatewayConnections(dataBroker);
156             l2gateways = L2GatewayConnectionUtils.getL2gatewayList(dataBroker);
157             for (L2gateway l2gateway : l2gateways) {
158                 uuidToL2Gateway.put(l2gateway.getUuid(), l2gateway);
159             }
160         }
161     }
162
163     private boolean isPresent(Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> dataMap,
164                               InstanceIdentifier<Node> nodeIid, InstanceIdentifier dataIid) {
165         if (dataMap.containsKey(nodeIid)) {
166             return dataMap.get(nodeIid).containsKey(dataIid);
167         }
168         return false;
169     }
170
171     private DataObject getData(Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> dataMap,
172                                InstanceIdentifier<Node> nodeIid, InstanceIdentifier dataIid) {
173         if (dataMap.containsKey(nodeIid)) {
174             return dataMap.get(nodeIid).get(dataIid);
175         }
176         return null;
177     }
178
179     private void fillNodesData(Map<InstanceIdentifier<Node>, Node> nodes,
180                                Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> dataMap) {
181
182         for (Map.Entry<InstanceIdentifier<Node>, Node> entry : nodes.entrySet()) {
183             InstanceIdentifier<Node> nodeId = entry.getKey();
184             Node node = entry.getValue();
185             Map<InstanceIdentifier, DataObject> map = new HashMap<>();
186             dataMap.put(nodeId, map);
187             if (node.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
188                 for (MergeCommand command : globalCommands) {
189                     List<DataObject> data = command.getData(node.getAugmentation(HwvtepGlobalAugmentation.class));
190                     if (data != null) {
191                         for (DataObject dataObject : data) {
192                             map.put(command.generateId(nodeId, dataObject), dataObject);
193                         }
194                     }
195                 }
196             } else {
197                 for (MergeCommand command : physicalSwitchCommands) {
198                     List<DataObject> data = command.getData(node);
199                     if (data != null) {
200                         for (DataObject dataObject : data) {
201                             map.put(command.generateId(nodeId, dataObject), dataObject);
202                         }
203                     }
204                 }
205             }
206         }
207     }
208
209     /**
210      * Checks the diff between config and operational topology nodes and prints it to the file if any.
211      * This will tell what is present in the controller config and not in the device
212      */
213     private void verifyConfigVsOperationalDiff() {
214         for (Node cfgNode : configNodes.values()) {
215             InstanceIdentifier<Node> nodeId = topoIid.child(Node.class, cfgNode.getKey());
216             compareNodes(cfgNode, operationalNodes.get(nodeId), false, LogicalDatastoreType.CONFIGURATION);
217         }
218     }
219
220     /**
221      * Checks the diff between HA parent and child nodes.
222      * Whatever config data in parent should be present in child nodes
223      * Whatever operational data in child should be present in parent node
224      */
225     private void verifyHANodes() {
226         pw.println("Verifying HA nodes");
227         boolean parentChildComparison = true;
228         HwvtepHACache haCache = HwvtepHACache.getInstance();
229         Set<InstanceIdentifier<Node>> parentNodes = HwvtepHACache.getInstance().getHAParentNodes();
230         if (HwvtepHAUtil.isEmpty(parentNodes)) {
231             return;
232         }
233         for (InstanceIdentifier<Node> parentNodeIid : parentNodes) {
234             String parentNodeId = parentNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
235             Node parentOpNode = operationalNodes.get(parentNodeIid);
236             Node parentCfgNode = configNodes.get(parentNodeIid);
237             Set<InstanceIdentifier<Node>> childNodeIids = haCache.getChildrenForHANode(parentNodeIid);
238             if (HwvtepHAUtil.isEmpty(childNodeIids)) {
239                 pw.println("No child nodes could be found for parent node " + parentNodeId);
240                 continue;
241             }
242             for (InstanceIdentifier<Node> childNodeIid : childNodeIids) {
243                 String childNodeId = childNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
244                 if (parentOpNode != null) {
245                     compareNodes(parentOpNode, operationalNodes.get(childNodeIid), parentChildComparison,
246                             LogicalDatastoreType.OPERATIONAL);
247                 } else {
248                     pw.println("Missing parent operational node for id " + parentNodeId);
249                 }
250                 if (parentCfgNode != null) {
251                     if (configNodes.get(childNodeIid) == null) {
252                         if (containsLogicalSwitch(parentCfgNode)) {
253                             pw.println("Missing child config data " + childNodeId);
254                         }
255                     } else {
256                         compareNodes(parentCfgNode, configNodes.get(childNodeIid), parentChildComparison,
257                                 LogicalDatastoreType.CONFIGURATION);
258                     }
259                 } else {
260                     pw.println("Missing parent config node for id " + parentNodeId);
261                 }
262             }
263         }
264     }
265
266     private boolean containsLogicalSwitch(Node node) {
267         if (node == null || node.getAugmentation(HwvtepGlobalAugmentation.class) == null
268                 || HwvtepHAUtil.isEmptyList(
269                 node.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches())) {
270             return false;
271         }
272         return true;
273     }
274
275     private boolean compareNodes(Node node1, Node node2, boolean parentChildComparison,
276                                  LogicalDatastoreType datastoreType) {
277
278         if (node1 == null || node2 == null) {
279             return false;
280         }
281         InstanceIdentifier<Node> nodeIid1 = HwvtepSouthboundUtils.createInstanceIdentifier(node1.getNodeId());
282         InstanceIdentifier<Node> nodeIid2 = HwvtepSouthboundUtils.createInstanceIdentifier(node2.getNodeId());
283
284         NodeId nodeId1 = nodeIid1.firstKeyOf(Node.class).getNodeId();
285         NodeId nodeId2 = nodeIid2.firstKeyOf(Node.class).getNodeId();
286
287         PhysicalSwitchAugmentation psAug1 = node1.getAugmentation(PhysicalSwitchAugmentation.class);
288         PhysicalSwitchAugmentation psAug2 = node2.getAugmentation(PhysicalSwitchAugmentation.class);
289
290         HwvtepGlobalAugmentation aug1 = node1.getAugmentation(HwvtepGlobalAugmentation.class);
291         HwvtepGlobalAugmentation aug2 = node2.getAugmentation(HwvtepGlobalAugmentation.class);
292
293         boolean globalNodes = psAug1 == null && psAug2 == null ? true : false;
294         MergeCommand[] commands = globalNodes ? globalCommands : physicalSwitchCommands;
295
296         for (MergeCommand cmd : commands) {
297
298             List<DataObject> data1 = null;
299             List<DataObject> data2 = null;
300
301             if (globalNodes) {
302                 data1 = cmd.getData(aug1);
303                 data2 = cmd.getData(aug2);
304             } else {
305                 data1 = cmd.getData(node1);
306                 data2 = cmd.getData(node2);
307             }
308             data1 = data1 == null ? Collections.EMPTY_LIST : data1;
309             data2 = data2 == null ? Collections.EMPTY_LIST : data2;
310
311             if (parentChildComparison) {
312                 data2 = cmd.transform(nodeIid1, data2);
313             }
314             Function<DataObject, DataObject> withoutUuidTransformer = cmd::withoutUuid;
315             data1 = data1.stream().map(withoutUuidTransformer).collect(Collectors.toList());
316             data2 = data2.stream().map(withoutUuidTransformer).collect(Collectors.toList());
317
318             Map<Identifier<?>, DataObject> map1 = new HashMap<>();
319             Map<Identifier<?>, DataObject> map2 = new HashMap<>();
320             for (DataObject dataObject : data1) {
321                 map1.put(cmd.getKey(dataObject), dataObject);
322             }
323             for (DataObject dataObject : data2) {
324                 map2.put(cmd.getKey(dataObject), dataObject);
325             }
326             Set<DataObject> diff = Sets.newHashSet();
327
328             for (Entry<Identifier<?>, DataObject> entry : map1.entrySet()) {
329                 DataObject obj1 = entry.getValue();
330                 DataObject obj2 = map2.get(entry.getKey());
331                 if (obj2 == null || !cmd.areEqual(obj1, obj2)) {
332                     diff.add(obj1);
333                 }
334             }
335
336             if (!diff.isEmpty()) {
337                 if (parentChildComparison) {
338                     pw.println("Missing " + cmd.getDescription() + " child entries in " + datastoreType
339                             + " parent node " + nodeId1 + " contain " + " more entries than child "
340                             + nodeId2 + " " + diff.size());
341                 } else {
342                     pw.println("Missing " + cmd.getDescription() + " op entries config "
343                             + nodeId1 + " contain " + " more entries than operational node " + diff.size());
344                 }
345                 if (diff.size() < 10) {
346                     for (Object obj : diff) {
347                         pw.println(cmd.getKey((DataObject) obj));
348                     }
349                 }
350             }
351
352             diff = Sets.newHashSet();
353             for (Entry<Identifier<?>, DataObject> entry : map2.entrySet()) {
354                 DataObject obj1 = entry.getValue();
355                 DataObject obj2 = map1.get(entry.getKey());
356                 if (globalNodes || parentChildComparison) {
357                     if (obj2 == null || !cmd.areEqual(obj1, obj2)) {
358                         diff.add(obj1);
359                     }
360                 }
361             }
362             if (!diff.isEmpty()) {
363                 if (parentChildComparison) {
364                     pw.println("Extra " + cmd.getDescription() + " child entries in " + datastoreType + " node "
365                             + nodeId2 + " contain " + " more entries than parent node " + nodeId1 + " " + diff.size());
366                 } else {
367                     pw.println("Extra " + cmd.getDescription() + " operational node "
368                             + nodeId2 + " contain " + " more entries than config node " + diff.size());
369                 }
370                 if (diff.size() < 10) {
371                     for (Object obj : diff) {
372                         pw.println(cmd.getKey((DataObject) obj));
373                     }
374                 }
375             }
376         }
377         return true;
378     }
379
380     private void verifyL2GatewayConnections() {
381         boolean isValid = true;
382         for (L2gatewayConnection l2gatewayConnection : l2gatewayConnections) {
383
384             L2gateway l2gateway = uuidToL2Gateway.get(l2gatewayConnection.getL2gatewayId());
385             String logicalSwitchName = l2gatewayConnection.getNetworkId().getValue();
386             List<Devices> devices = l2gateway.getDevices();
387
388             for (Devices device : devices) {
389
390                 L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(device.getDeviceName());
391                 isValid = verifyL2GatewayDevice(l2gateway, device, l2GatewayDevice);
392                 if (!isValid) {
393                     continue;
394                 }
395                 NodeId nodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
396                 InstanceIdentifier<Node> nodeIid = topoIid.child(Node.class, new NodeKey(nodeId));
397
398                 isValid = verfiyLogicalSwitch(logicalSwitchName, nodeIid);
399                 if (isValid) {
400                     isValid = verifyMcastMac(logicalSwitchName, nodeIid);
401                     verifyVlanBindings(nodeIid, logicalSwitchName, device, l2gatewayConnection.getSegmentId());
402                     L2GatewayDevice elanL2gatewayDevice = ElanL2GwCacheUtils
403                             .getL2GatewayDeviceFromCache(logicalSwitchName, nodeId.getValue());
404                     if (elanL2gatewayDevice == null) {
405                         pw.println("Failed elan l2gateway device not found for network " + logicalSwitchName
406                                 + " and device " + device.getDeviceName() + " " + l2GatewayDevice.getHwvtepNodeId()
407                                 + " l2gw connection id " + l2gatewayConnection.getUuid());
408                     }
409                 }
410             }
411         }
412     }
413
414     private boolean verifyL2GatewayDevice(L2gateway l2gateway, Devices device, L2GatewayDevice l2GatewayDevice) {
415         if (l2GatewayDevice == null) {
416             pw.println("Failed l2gateway not found in cache for device " + device.getDeviceName());
417             return false;
418         }
419         if (l2GatewayDevice.getHwvtepNodeId() == null) {
420             pw.println("L2gateway cache is not updated with node id for device " + device.getDeviceName());
421             return false;
422         }
423         if (l2GatewayDevice.getTunnelIp() == null) {
424             pw.println("L2gateway cache is not updated with tunnel ip for device " + device.getDeviceName());
425             return false;
426         }
427         if (!l2GatewayDevice.getL2GatewayIds().contains(l2gateway.getUuid())) {
428             pw.println("L2gateway cache is not updated with l2gw id for device " + device.getDeviceName());
429             return false;
430         }
431         return true;
432     }
433
434     private  InstanceIdentifier<TerminationPoint> getPhysicalPortTerminationPointIid(NodeId nodeId, String portName) {
435         TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
436         InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createTerminationPointId(nodeId, tpKey);
437         return iid;
438     }
439
440     private boolean verfiyLogicalSwitch(String logicalSwitchName, InstanceIdentifier<Node> nodeIid) {
441         NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
442         InstanceIdentifier<LogicalSwitches> logicalSwitchPath = HwvtepSouthboundUtils
443                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
444
445         if (!isPresent(configNodesData, nodeIid, logicalSwitchPath)) {
446             pw.println("Failed to find config logical switch " + logicalSwitchName + " for node "
447                     + nodeId.getValue());
448             return false;
449         }
450
451         if (!isPresent(operationalNodesData, nodeIid, logicalSwitchPath)) {
452             pw.println("Failed to find operational logical switch " + logicalSwitchName + " for node "
453                     + nodeId.getValue());
454             return false;
455         }
456         return true;
457     }
458
459     private boolean verifyMcastMac(String logicalSwitchName,
460                                    InstanceIdentifier<Node> nodeIid) {
461         NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
462
463         HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(HwvtepSouthboundUtils
464                 .createLogicalSwitchesInstanceIdentifier(
465                         new NodeId(new Uri(nodeId)), new HwvtepNodeName(logicalSwitchName)));
466
467         RemoteMcastMacs remoteMcastMac = new RemoteMcastMacsBuilder()
468                 .setMacEntryKey(new MacAddress(ElanConstants.UNKNOWN_DMAC)).setLogicalSwitchRef(lsRef).build();
469         InstanceIdentifier<RemoteMcastMacs> mcastMacIid = HwvtepSouthboundUtils
470                 .createRemoteMcastMacsInstanceIdentifier(new NodeId(new Uri(nodeId)), remoteMcastMac.getKey());
471
472
473         if (!isPresent(configNodesData, nodeIid, mcastMacIid)) {
474             pw.println("Failed to find config mcast mac for logical switch " + logicalSwitchName
475                     + " node id " + nodeId.getValue());
476             return false;
477         }
478
479         if (!isPresent(operationalNodesData, nodeIid, mcastMacIid)) {
480             pw.println("Failed to find operational mcast mac for logical switch " + logicalSwitchName
481                     + " node id " + nodeId.getValue());
482             return false;
483         }
484         return true;
485     }
486
487     private boolean verifyVlanBindings(InstanceIdentifier<Node> nodeIid,
488                                        String logicalSwitchName,
489                                        Devices hwVtepDevice,
490                                        Integer defaultVlanId) {
491         boolean valid = true;
492         NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
493         if (hwVtepDevice.getInterfaces() == null) {
494             return false;
495         }
496         for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712
497                  .l2gateway.attributes.devices.Interfaces deviceInterface : hwVtepDevice.getInterfaces()) {
498
499             NodeId switchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, hwVtepDevice.getDeviceName());
500             InstanceIdentifier<Node> physicalSwitchNodeIid = topoIid.child(Node.class, new NodeKey(switchNodeId));
501             InstanceIdentifier<TerminationPoint> terminationPointIid =
502                     getPhysicalPortTerminationPointIid(switchNodeId, deviceInterface.getInterfaceName());
503
504             TerminationPoint operationalTerminationPoint = (TerminationPoint) getData(operationalNodesData,
505                     physicalSwitchNodeIid, terminationPointIid);
506             if (operationalTerminationPoint == null) {
507                 valid = false;
508                 pw.println("Failed to find the operational port " + deviceInterface.getInterfaceName()
509                         + " for node " + hwVtepDevice.getDeviceName() + " nodeid " + nodeId.getValue());
510                 continue;
511             }
512             TerminationPoint configTerminationPoint = (TerminationPoint) getData(configNodesData,
513                     physicalSwitchNodeIid, terminationPointIid);
514             if (configTerminationPoint == null) {
515                 valid = false;
516                 pw.println("Failed to find the configurational port " + deviceInterface.getInterfaceName()
517                         + " for node " + hwVtepDevice.getDeviceName() +  " for logical switch " + logicalSwitchName
518                         + " nodeid " + nodeId.getValue());
519                 continue;
520             }
521
522             List<VlanBindings> expectedVlans = new ArrayList<>();
523             if (deviceInterface.getSegmentationIds() != null && !deviceInterface.getSegmentationIds().isEmpty()) {
524                 for (Integer vlanId : deviceInterface.getSegmentationIds()) {
525                     expectedVlans.add(HwvtepSouthboundUtils.createVlanBinding(nodeId, vlanId, logicalSwitchName));
526                 }
527             } else {
528                 expectedVlans.add(HwvtepSouthboundUtils.createVlanBinding(nodeId, defaultVlanId, logicalSwitchName));
529             }
530
531             HwvtepPhysicalPortAugmentation portAugmentation = configTerminationPoint.getAugmentation(
532                     HwvtepPhysicalPortAugmentation.class);
533             if (portAugmentation == null || HwvtepHAUtil.isEmptyList(portAugmentation.getVlanBindings())) {
534                 pw.println("Failed to find the config vlan bindings for port " + deviceInterface.getInterfaceName()
535                         + " for node " + hwVtepDevice.getDeviceName() +  " for logical switch " + logicalSwitchName
536                         + " nodeid " + nodeId.getValue());
537                 valid = false;
538                 continue;
539             }
540             portAugmentation = operationalTerminationPoint.getAugmentation(HwvtepPhysicalPortAugmentation.class);
541             if (portAugmentation == null || HwvtepHAUtil.isEmptyList(portAugmentation.getVlanBindings())) {
542                 pw.println("Failed to find the operational vlan bindings for port " + deviceInterface.getInterfaceName()
543                         + " for node " + hwVtepDevice.getDeviceName() +  " for logical switch " + logicalSwitchName
544                         + " nodeid " + nodeId.getValue());
545                 valid = false;
546                 continue;
547             }
548             VlanBindings expectedBindings = !expectedVlans.isEmpty() ? expectedVlans.get(0) : null;
549             boolean foundBindings = false;
550             List<VlanBindings> vlanBindingses = configTerminationPoint.getAugmentation(
551                     HwvtepPhysicalPortAugmentation.class).getVlanBindings();
552             for (VlanBindings actual : vlanBindingses) {
553                 if (actual.equals(expectedBindings)) {
554                     foundBindings = true;
555                     break;
556                 }
557             }
558             if (!foundBindings) {
559                 pw.println("Mismatch in vlan bindings for port " + deviceInterface.getInterfaceName()
560                         + " for node " + hwVtepDevice.getDeviceName() +  " for logical switch " + logicalSwitchName
561                         + " nodeid " + nodeId.getValue());
562                 pw.println("Failed to find the vlan bindings " + expectedBindings);
563                 pw.println("Actual bindings present in config are ");
564                 for (VlanBindings actual : vlanBindingses) {
565                     pw.println(actual.toString());
566                 }
567                 valid = false;
568             }
569         }
570         return true;
571     }
572 }