Remove redundant names in paths
[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.Lists;
14 import com.google.common.collect.Sets;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.PrintWriter;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
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.ReadWriteTransaction;
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         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
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     private boolean isPresent(Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> dataMap,
163                               InstanceIdentifier<Node> nodeIid, InstanceIdentifier dataIid) {
164         if (dataMap.containsKey(nodeIid)) {
165             return dataMap.get(nodeIid).containsKey(dataIid);
166         }
167         return false;
168     }
169
170     private DataObject getData(Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> dataMap,
171                                InstanceIdentifier<Node> nodeIid, InstanceIdentifier dataIid) {
172         if (dataMap.containsKey(nodeIid)) {
173             return dataMap.get(nodeIid).get(dataIid);
174         }
175         return null;
176     }
177
178     private void fillNodesData(Map<InstanceIdentifier<Node>, Node> nodes,
179                                Map<InstanceIdentifier<Node>, Map<InstanceIdentifier, DataObject>> dataMap) {
180
181         for (Map.Entry<InstanceIdentifier<Node>, Node> entry : nodes.entrySet()) {
182             InstanceIdentifier<Node> nodeId = entry.getKey();
183             Node node = entry.getValue();
184             Map<InstanceIdentifier, DataObject> map = new HashMap<>();
185             dataMap.put(nodeId, map);
186             if (node.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
187                 for (MergeCommand command : globalCommands) {
188                     List<DataObject> data = command.getData(node.getAugmentation(HwvtepGlobalAugmentation.class));
189                     if (data != null) {
190                         for (DataObject dataObject : data) {
191                             map.put(command.generateId(nodeId, dataObject), dataObject);
192                         }
193                     }
194                 }
195             } else {
196                 for (MergeCommand command : physicalSwitchCommands) {
197                     List<DataObject> data = command.getData(node);
198                     if (data != null) {
199                         for (DataObject dataObject : data) {
200                             map.put(command.generateId(nodeId, dataObject), dataObject);
201                         }
202                     }
203                 }
204             }
205         }
206     }
207
208     /**
209      * Checks the diff between config and operational topology nodes and prints it to the file if any.
210      * This will tell what is present in the controller config and not in the device
211      */
212     private void verifyConfigVsOperationalDiff() {
213         for (Node cfgNode : configNodes.values()) {
214             InstanceIdentifier<Node> nodeId = topoIid.child(Node.class, cfgNode.getKey());
215             compareNodes(cfgNode, operationalNodes.get(nodeId), false, LogicalDatastoreType.CONFIGURATION);
216         }
217     }
218
219     /**
220      * Checks the diff between HA parent and child nodes.
221      * Whatever config data in parent should be present in child nodes
222      * Whatever operational data in child should be present in parent node
223      */
224     private void verifyHANodes() {
225         pw.println("Verifying HA nodes");
226         boolean parentChildComparison = true;
227         HwvtepHACache haCache = HwvtepHACache.getInstance();
228         Set<InstanceIdentifier<Node>> parentNodes = HwvtepHACache.getInstance().getHAParentNodes();
229         if (HwvtepHAUtil.isEmpty(parentNodes)) {
230             return;
231         }
232         for (InstanceIdentifier<Node> parentNodeIid : parentNodes) {
233             String parentNodeId = parentNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
234             Node parentOpNode = operationalNodes.get(parentNodeIid);
235             Node parentCfgNode = configNodes.get(parentNodeIid);
236             Set<InstanceIdentifier<Node>> childNodeIids = haCache.getChildrenForHANode(parentNodeIid);
237             if (HwvtepHAUtil.isEmpty(childNodeIids)) {
238                 pw.println("No child nodes could be found for parent node " + parentNodeId);
239                 continue;
240             }
241             for (InstanceIdentifier<Node> childNodeIid : childNodeIids) {
242                 String childNodeId = childNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
243                 if (parentOpNode != null) {
244                     compareNodes(parentOpNode, operationalNodes.get(childNodeIid), parentChildComparison,
245                             LogicalDatastoreType.OPERATIONAL);
246                 } else {
247                     pw.println("Missing parent operational node for id " + parentNodeId);
248                 }
249                 if (parentCfgNode != null) {
250                     if (configNodes.get(childNodeIid) == null) {
251                         if (containsLogicalSwitch(parentCfgNode)) {
252                             pw.println("Missing child config data " + childNodeId);
253                         }
254                     } else {
255                         compareNodes(parentCfgNode, configNodes.get(childNodeIid), parentChildComparison,
256                                 LogicalDatastoreType.CONFIGURATION);
257                     }
258                 } else {
259                     pw.println("Missing parent config node for id " + parentNodeId);
260                 }
261             }
262         }
263     }
264
265     private boolean containsLogicalSwitch(Node node) {
266         if (node == null || node.getAugmentation(HwvtepGlobalAugmentation.class) == null
267                 || HwvtepHAUtil.isEmptyList(
268                 node.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches())) {
269             return false;
270         }
271         return true;
272     }
273
274     private boolean compareNodes(Node node1, Node node2, boolean parentChildComparison,
275                                  LogicalDatastoreType datastoreType) {
276
277         if (node1 == null || node2 == null) {
278             return false;
279         }
280         InstanceIdentifier<Node> nodeIid1 = HwvtepSouthboundUtils.createInstanceIdentifier(node1.getNodeId());
281         InstanceIdentifier<Node> nodeIid2 = HwvtepSouthboundUtils.createInstanceIdentifier(node2.getNodeId());
282
283         NodeId nodeId1 = nodeIid1.firstKeyOf(Node.class).getNodeId();
284         NodeId nodeId2 = nodeIid2.firstKeyOf(Node.class).getNodeId();
285
286         PhysicalSwitchAugmentation psAug1 = node1.getAugmentation(PhysicalSwitchAugmentation.class);
287         PhysicalSwitchAugmentation psAug2 = node2.getAugmentation(PhysicalSwitchAugmentation.class);
288
289         HwvtepGlobalAugmentation aug1 = node1.getAugmentation(HwvtepGlobalAugmentation.class);
290         HwvtepGlobalAugmentation aug2 = node2.getAugmentation(HwvtepGlobalAugmentation.class);
291
292         boolean globalNodes = psAug1 == null && psAug2 == null ? true : false;
293         MergeCommand[] commands = globalNodes ? globalCommands : physicalSwitchCommands;
294
295         for (MergeCommand cmd : commands) {
296
297             List<DataObject> data1 = null;
298             List<DataObject> data2 = null;
299
300             if (globalNodes) {
301                 data1 = cmd.getData(aug1);
302                 data2 = cmd.getData(aug2);
303             } else {
304                 data1 = cmd.getData(node1);
305                 data2 = cmd.getData(node2);
306             }
307             data1 = data1 == null ? Collections.EMPTY_LIST : data1;
308             data2 = data2 == null ? Collections.EMPTY_LIST : data2;
309
310             if (parentChildComparison) {
311                 data2 = cmd.transform(nodeIid1, data2);
312             }
313             Function<DataObject, DataObject> withoutUuidTransformer = cmd::withoutUuid;
314             data1 = Lists.transform(data1, withoutUuidTransformer);
315             data2 = Lists.transform(data2, withoutUuidTransformer);
316
317             Map<Identifier<?>, DataObject> map1 = new HashMap<>();
318             Map<Identifier<?>, DataObject> map2 = new HashMap<>();
319             for (DataObject dataObject : data1) {
320                 map1.put(cmd.getKey(dataObject), dataObject);
321             }
322             for (DataObject dataObject : data2) {
323                 map2.put(cmd.getKey(dataObject), dataObject);
324             }
325             Set<DataObject> diff = Sets.newHashSet();
326
327             for (Entry<Identifier<?>, DataObject> entry : map1.entrySet()) {
328                 DataObject obj1 = entry.getValue();
329                 DataObject obj2 = map2.get(entry.getKey());
330                 if (obj2 == null || !cmd.areEqual(obj1, obj2)) {
331                     diff.add(obj1);
332                 }
333             }
334
335             if (!diff.isEmpty()) {
336                 if (parentChildComparison) {
337                     pw.println("Missing " + cmd.getDescription() + " child entries in " + datastoreType
338                             + " parent node " + nodeId1 + " contain " + " more entries than child "
339                             + nodeId2 + " " + diff.size());
340                 } else {
341                     pw.println("Missing " + cmd.getDescription() + " op entries config "
342                             + nodeId1 + " contain " + " more entries than operational node " + diff.size());
343                 }
344                 if (diff.size() < 10) {
345                     for (Object obj : diff) {
346                         pw.println(cmd.getKey((DataObject) obj));
347                     }
348                 }
349             }
350
351             diff = Sets.newHashSet();
352             for (Entry<Identifier<?>, DataObject> entry : map2.entrySet()) {
353                 DataObject obj1 = entry.getValue();
354                 DataObject obj2 = map1.get(entry.getKey());
355                 if (globalNodes || parentChildComparison) {
356                     if (obj2 == null || !cmd.areEqual(obj1, obj2)) {
357                         diff.add(obj1);
358                     }
359                 }
360             }
361             if (!diff.isEmpty()) {
362                 if (parentChildComparison) {
363                     pw.println("Extra " + cmd.getDescription() + " child entries in " + datastoreType + " node "
364                             + nodeId2 + " contain " + " more entries than parent node " + nodeId1 + " " + diff.size());
365                 } else {
366                     pw.println("Extra " + cmd.getDescription() + " operational node "
367                             + nodeId2 + " contain " + " more entries than config node " + diff.size());
368                 }
369                 if (diff.size() < 10) {
370                     for (Object obj : diff) {
371                         pw.println(cmd.getKey((DataObject) obj));
372                     }
373                 }
374             }
375         }
376         return true;
377     }
378
379     private void verifyL2GatewayConnections() {
380         boolean isValid = true;
381         for (L2gatewayConnection l2gatewayConnection : l2gatewayConnections) {
382
383             L2gateway l2gateway = uuidToL2Gateway.get(l2gatewayConnection.getL2gatewayId());
384             String logicalSwitchName = l2gatewayConnection.getNetworkId().getValue();
385             List<Devices> devices = l2gateway.getDevices();
386
387             for (Devices device : devices) {
388
389                 L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(device.getDeviceName());
390                 isValid = verifyL2GatewayDevice(l2gateway, device, l2GatewayDevice);
391                 if (!isValid) {
392                     continue;
393                 }
394                 NodeId nodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
395                 InstanceIdentifier<Node> nodeIid = topoIid.child(Node.class, new NodeKey(nodeId));
396
397                 isValid = verfiyLogicalSwitch(logicalSwitchName, nodeIid);
398                 if (isValid) {
399                     isValid = verifyMcastMac(logicalSwitchName, l2GatewayDevice, nodeIid);
400                     verifyVlanBindings(nodeIid, logicalSwitchName, device, l2gatewayConnection.getSegmentId());
401                     L2GatewayDevice elanL2gatewayDevice = ElanL2GwCacheUtils
402                             .getL2GatewayDeviceFromCache(logicalSwitchName, nodeId.getValue());
403                     if (elanL2gatewayDevice == null) {
404                         pw.println("Failed elan l2gateway device not found for network " + logicalSwitchName
405                                 + " and device " + device.getDeviceName() + " " + l2GatewayDevice.getHwvtepNodeId()
406                                 + " l2gw connection id " + l2gatewayConnection.getUuid());
407                     }
408                 }
409             }
410         }
411     }
412
413     private boolean verifyL2GatewayDevice(L2gateway l2gateway, Devices device, L2GatewayDevice l2GatewayDevice) {
414         if (l2GatewayDevice == null) {
415             pw.println("Failed l2gateway not found in cache for device " + device.getDeviceName());
416             return false;
417         }
418         if (l2GatewayDevice.getHwvtepNodeId() == null) {
419             pw.println("L2gateway cache is not updated with node id for device " + device.getDeviceName());
420             return false;
421         }
422         if (l2GatewayDevice.getTunnelIp() == null) {
423             pw.println("L2gateway cache is not updated with tunnel ip for device " + device.getDeviceName());
424             return false;
425         }
426         if (!l2GatewayDevice.getL2GatewayIds().contains(l2gateway.getUuid())) {
427             pw.println("L2gateway cache is not updated with l2gw id for device " + device.getDeviceName());
428             return false;
429         }
430         return true;
431     }
432
433     private  InstanceIdentifier<TerminationPoint> getPhysicalPortTerminationPointIid(NodeId nodeId, String portName) {
434         TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
435         InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createTerminationPointId(nodeId, tpKey);
436         return iid;
437     }
438
439     private boolean verfiyLogicalSwitch(String logicalSwitchName, InstanceIdentifier<Node> nodeIid) {
440         NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
441         InstanceIdentifier<LogicalSwitches> logicalSwitchPath = HwvtepSouthboundUtils
442                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
443
444         if (!isPresent(configNodesData, nodeIid, logicalSwitchPath)) {
445             pw.println("Failed to find config logical switch " + logicalSwitchName + " for node "
446                     + nodeId.getValue());
447             return false;
448         }
449
450         if (!isPresent(operationalNodesData, nodeIid, logicalSwitchPath)) {
451             pw.println("Failed to find operational logical switch " + logicalSwitchName + " for node "
452                     + nodeId.getValue());
453             return false;
454         }
455         return true;
456     }
457
458     private boolean verifyMcastMac(String logicalSwitchName,
459                                    L2GatewayDevice l2GatewayDevice,
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 }