f4d6b4473ea07cff04b01b02c251e692c66098c0
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / utils / hwvtep / HwvtepUtils.java
1 /*
2  * Copyright (c) 2016 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.vpnservice.utils.hwvtep;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 import com.google.common.base.Optional;
42 import com.google.common.util.concurrent.ListenableFuture;
43
44 /**
45  * Utility class to related to Hardware VTEP devices.
46  */
47 public final class HwvtepUtils {
48
49     // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
50     public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
51         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
52                 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
53     }
54
55     /**
56      * Adds the logical switch into config DS.
57      *
58      * @param broker
59      *            the broker
60      * @param nodeId
61      *            the node id
62      * @param logicalSwitch
63      *            the logical switch
64      * @return the listenable future
65      */
66     public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, NodeId nodeId,
67             LogicalSwitches logicalSwitch) {
68         WriteTransaction transaction = broker.newWriteOnlyTransaction();
69         putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
70         return transaction.submit();
71     }
72
73     public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, LogicalDatastoreType logicalDatastoreType,
74                                                           NodeId nodeId,
75                                                           LogicalSwitches logicalSwitch) {
76         WriteTransaction transaction = broker.newWriteOnlyTransaction();
77         putLogicalSwitch(transaction,logicalDatastoreType, nodeId, logicalSwitch);
78         return transaction.submit();
79     }
80     /**
81      * Put the logical switches in the transaction.
82      *
83      * @param transaction
84      *            the transaction
85      * @param nodeId
86      *            the node id
87      * @param lstSwitches
88      *            the lst switches
89      */
90     public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
91             final List<LogicalSwitches> lstSwitches) {
92         if (lstSwitches != null) {
93             for (LogicalSwitches logicalSwitch : lstSwitches) {
94                 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
95             }
96         }
97     }
98
99     /**
100      * Put logical switch in the transaction.
101      *
102      * @param transaction
103      *            the transaction
104      * @param nodeId
105      *            the node id
106      * @param logicalSwitch
107      *            the logical switch
108      */
109     public static void putLogicalSwitch(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
110                                         final NodeId nodeId, final LogicalSwitches logicalSwitch) {
111         InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
112                 logicalSwitch.getHwvtepNodeName());
113         transaction.put(logicalDatastoreType, iid, logicalSwitch, true);
114     }
115
116     /**
117      * Delete logical switch from config DS.
118      *
119      * @param broker
120      *            the broker
121      * @param nodeId
122      *            the node id
123      * @param logicalSwitchName
124      *            the logical switch name
125      * @return the listenable future
126      */
127     public static ListenableFuture<Void> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
128             String logicalSwitchName) {
129         WriteTransaction transaction = broker.newWriteOnlyTransaction();
130         deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
131         return transaction.submit();
132     }
133
134     /**
135      * Delete logical switch from the transaction.
136      *
137      * @param transaction
138      *            the transaction
139      * @param nodeId
140      *            the node id
141      * @param logicalSwitchName
142      *            the logical switch name
143      */
144     public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
145             final String logicalSwitchName) {
146         transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
147                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
148     }
149
150     /**
151      * Gets the logical switch.
152      *
153      * @param nodeId
154      *            the node id
155      * @param logicalSwitchName
156      *            the logical switch name
157      * @return the logical switch
158      */
159     public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
160             String logicalSwitchName) {
161         final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
162                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
163         Optional<LogicalSwitches> optLogicalSwitch = MDSALUtil.read(broker, datastoreType, iid);
164         if (optLogicalSwitch.isPresent()) {
165             return optLogicalSwitch.get();
166         }
167         return null;
168     }
169
170     /**
171      * Get LogicalSwitches for a given hwVtepNodeId.
172      *
173      * @param broker
174      *            the broker
175      * @param hwVtepNodeId
176      *            Hardware VTEP Node Id
177      * @param vni
178      *            virtual network id
179      * @return the logical switches
180      */
181     public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni) {
182         NodeId nodeId = new NodeId(hwVtepNodeId);
183         InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
184                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
185
186         Optional<LogicalSwitches> logicalSwitches = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
187                 logicalSwitchesIdentifier);
188         if (!logicalSwitches.isPresent()) {
189             return null;
190         }
191
192         return logicalSwitches.get();
193     }
194
195     /**
196      * Put physical locators in the transaction.
197      *
198      * @param transaction
199      *            the transaction
200      * @param nodeId
201      *            the node id
202      * @param lstPhysicalLocator
203      *            the lst physical locator
204      */
205     public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
206             List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
207         if (lstPhysicalLocator != null) {
208             for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
209                 putPhysicalLocator(transaction, nodeId, phyLocator);
210             }
211         }
212     }
213
214     /**
215      * Put physical locator in the transaction.
216      *
217      * @param transaction
218      *            the transaction
219      * @param nodeId
220      *            the node id
221      * @param phyLocator
222      *            the phy locator
223      */
224     public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
225             final HwvtepPhysicalLocatorAugmentation phyLocator) {
226         InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
227                 phyLocator);
228         TerminationPoint terminationPoint = new TerminationPointBuilder()
229                 .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
230                 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
231
232         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
233     }
234
235     /**
236      * Gets the physical locator.
237      *
238      * @param broker
239      *            the broker
240      * @param datastoreType
241      *            the datastore type
242      * @param nodeId
243      *            the node id
244      * @param phyLocatorIp
245      *            the phy locator ip
246      * @return the physical locator
247      */
248     public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
249             LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) {
250         HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
251                 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(phyLocatorIp.getValue()));
252         InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
253                 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
254                 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
255         Optional<HwvtepPhysicalLocatorAugmentation> optPhyLocator = MDSALUtil.read(broker, datastoreType, iid);
256         if (optPhyLocator.isPresent()) {
257             return optPhyLocator.get();
258         }
259         return null;
260     }
261
262     /**
263      * Adds the remote ucast macs into config DS.
264      *
265      * @param broker
266      *            the broker
267      * @param nodeId
268      *            the node id
269      * @param lstRemoteUcastMacs
270      *            the lst remote ucast macs
271      * @return the listenable future
272      */
273     public static ListenableFuture<Void> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
274             List<RemoteUcastMacs> lstRemoteUcastMacs) {
275         WriteTransaction transaction = broker.newWriteOnlyTransaction();
276         putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
277         return transaction.submit();
278     }
279
280     /**
281      * Put remote ucast macs in the transaction.
282      *
283      * @param transaction
284      *            the transaction
285      * @param nodeId
286      *            the node id
287      * @param lstRemoteUcastMacs
288      *            the lst remote ucast macs
289      */
290     public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
291             final List<RemoteUcastMacs> lstRemoteUcastMacs) {
292         if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
293             for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
294                 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
295             }
296         }
297     }
298
299     /**
300      * Put remote ucast mac in the transaction.
301      *
302      * @param transaction
303      *            the transaction
304      * @param nodeId
305      *            the node id
306      * @param remoteUcastMac
307      *            the remote ucast mac
308      */
309     public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
310             RemoteUcastMacs remoteUcastMac) {
311         InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
312                 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
313                         new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
314         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
315     }
316
317     /**
318      * Delete remote ucast mac from the config DS.
319      *
320      * @param broker
321      *            the broker
322      * @param nodeId
323      *            the node id
324      * @param mac
325      *            the mac
326      * @return the listenable future
327      */
328     public static ListenableFuture<Void> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
329             String logicalSwitchName, MacAddress mac) {
330         WriteTransaction transaction = broker.newWriteOnlyTransaction();
331         deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
332         return transaction.submit();
333     }
334
335     /**
336      * Delete remote ucast macs from the config DS.
337      *
338      * @param broker
339      *            the broker
340      * @param nodeId
341      *            the node id
342      * @param lstMac
343      *            the lst mac
344      * @return the listenable future
345      */
346     public static ListenableFuture<Void> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
347             String logicalSwitchName, List<MacAddress> lstMac) {
348         WriteTransaction transaction = broker.newWriteOnlyTransaction();
349         deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
350         return transaction.submit();
351     }
352
353     /**
354      * Delete remote ucast macs from the transaction.
355      *
356      * @param transaction
357      *            the transaction
358      * @param nodeId
359      *            the node id
360      * @param lstMac
361      *            the lst mac
362      */
363     public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
364             String logicalSwitchName, final List<MacAddress> lstMac) {
365         if (lstMac != null && !lstMac.isEmpty()) {
366             for (MacAddress mac : lstMac) {
367                 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
368             }
369         }
370     }
371
372     /**
373      * Delete remote ucast mac from the transaction.
374      *
375      * @param transaction
376      *            the transaction
377      * @param nodeId
378      *            the node id
379      * @param mac
380      *            the mac
381      */
382     public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
383             String logialSwitchName, final MacAddress mac) {
384         transaction.delete(LogicalDatastoreType.CONFIGURATION,
385                 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
386     }
387
388     /**
389      * Adds the remote mcast macs into config DS.
390      *
391      * @param broker
392      *            the broker
393      * @param nodeId
394      *            the node id
395      * @param lstRemoteMcastMacs
396      *            the lst remote mcast macs
397      * @return the listenable future
398      */
399     public static ListenableFuture<Void> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
400             List<RemoteMcastMacs> lstRemoteMcastMacs) {
401         WriteTransaction transaction = broker.newWriteOnlyTransaction();
402         putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
403         return transaction.submit();
404     }
405
406     /**
407      * Put remote mcast macs in the transaction.
408      *
409      * @param transaction
410      *            the transaction
411      * @param nodeId
412      *            the node id
413      * @param lstRemoteMcastMacs
414      *            the lst remote mcast macs
415      */
416     public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
417             final List<RemoteMcastMacs> lstRemoteMcastMacs) {
418         if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
419             for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
420                 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
421             }
422         }
423     }
424
425     /**
426      * Put remote mcast mac in the transaction.
427      *
428      * @param transaction
429      *            the transaction
430      * @param nodeId
431      *            the node id
432      * @param remoteMcastMac
433      *            the remote mcast mac
434      */
435     public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
436             RemoteMcastMacs remoteMcastMac) {
437         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
438                 remoteMcastMac.getKey());
439         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
440     }
441
442     public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
443                                          final NodeId nodeId,
444                                          RemoteMcastMacs remoteMcastMac) {
445         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
446                 remoteMcastMac.getKey());
447         transaction.put(logicalDatastoreType, iid, remoteMcastMac, true);
448     }
449     /**
450      * Gets the remote mcast mac.
451      *
452      * @param broker
453      *            the broker
454      * @param datastoreType
455      *            the datastore type
456      * @param nodeId
457      *            the node id
458      * @param remoteMcastMacsKey
459      *            the remote mcast macs key
460      * @return the remote mcast mac
461      */
462     public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
463             NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey) {
464         final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
465                 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
466         Optional<RemoteMcastMacs> optRemoteMcastMac = MDSALUtil.read(broker, datastoreType, iid);
467         if (optRemoteMcastMac.isPresent()) {
468             return optRemoteMcastMac.get();
469         }
470         return null;
471     }
472
473     /**
474      * Delete remote mcast mac from config DS.
475      *
476      * @param broker
477      *            the broker
478      * @param nodeId
479      *            the node id
480      * @param remoteMcastMacsKey
481      *            the remote mcast macs key
482      * @return the listenable future
483      */
484     public static ListenableFuture<Void> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
485             RemoteMcastMacsKey remoteMcastMacsKey) {
486         WriteTransaction transaction = broker.newWriteOnlyTransaction();
487         deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
488         return transaction.submit();
489     }
490
491     /**
492      * Delete remote mcast macs from config DS.
493      *
494      * @param broker
495      *            the broker
496      * @param nodeId
497      *            the node id
498      * @param lstRemoteMcastMacsKey
499      *            the lst remote mcast macs key
500      * @return the listenable future
501      */
502     public static ListenableFuture<Void> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
503             List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
504         WriteTransaction transaction = broker.newWriteOnlyTransaction();
505         deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
506         return transaction.submit();
507     }
508
509     /**
510      * Delete remote mcast macs from the transaction.
511      *
512      * @param transaction
513      *            the transaction
514      * @param nodeId
515      *            the node id
516      * @param lstRemoteMcastMacsKey
517      *            the lst remote mcast macs key
518      */
519     public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
520             final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
521         if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
522             for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
523                 deleteRemoteMcastMac(transaction, nodeId, mac);
524             }
525         }
526     }
527
528     /**
529      * Delete remote mcast mac from the transaction.
530      *
531      * @param transaction
532      *            the transaction
533      * @param nodeId
534      *            the node id
535      * @param remoteMcastMacsKey
536      *            the remote mcast macs key
537      */
538     public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
539             final RemoteMcastMacsKey remoteMcastMacsKey) {
540         transaction.delete(LogicalDatastoreType.CONFIGURATION,
541                 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
542     }
543
544     /**
545      * Merge vlan bindings in the transaction.
546      *
547      * @param transaction
548      *            the transaction
549      * @param nodeId
550      *            the node id
551      * @param phySwitchName
552      *            the phy switch name
553      * @param phyPortName
554      *            the phy port name
555      * @param vlanBindings
556      *            the vlan bindings
557      */
558     public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
559             final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
560         NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
561         mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
562     }
563
564     /**
565      * Merge vlan bindings in the transaction.
566      *
567      * @param transaction
568      *            the transaction
569      * @param physicalSwitchNodeId
570      *            the physical switch node id
571      * @param phyPortName
572      *            the phy port name
573      * @param vlanBindings
574      *            the vlan bindings
575      */
576     public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
577             final String phyPortName, final List<VlanBindings> vlanBindings) {
578         HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
579                 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
580
581         final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
582                 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
583         transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
584     }
585
586     /**
587      * Delete vlan binding from transaction.
588      *
589      * @param transaction
590      *            the transaction
591      * @param physicalSwitchNodeId
592      *            the physical switch node id
593      * @param phyPortName
594      *            the phy port name
595      * @param vlanId
596      *            the vlan id
597      */
598     public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
599             Integer vlanId) {
600         InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
601                 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
602         transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
603     }
604
605     /**
606      * Gets the hw vtep node.
607      *
608      * @param dataBroker
609      *            the data broker
610      * @param datastoreType
611      *            the datastore type
612      * @param nodeId
613      *            the node id
614      * @return the hw vtep node
615      */
616     public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId) {
617         Optional<Node> optNode = MDSALUtil.read(dataBroker, datastoreType,
618                 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
619         if (optNode.isPresent()) {
620             return optNode.get();
621         }
622         return null;
623     }
624
625     /**
626      * Installs a list of Mac Addresses as remote Ucast address in an external
627      * device using the hwvtep-southbound.
628      *
629      * @param deviceNodeId
630      *            NodeId if the ExternalDevice where the macs must be installed
631      *            in.
632      * @param macAddresses
633      *            List of Mac addresses to be installed in the external device.
634      * @param logicalSwitchName
635      *            the logical switch name
636      * @param remoteVtepIp
637      *            VTEP's IP in this CSS used for the tunnel with external
638      *            device.
639      */
640     public static ListenableFuture<Void> installUcastMacs(DataBroker broker,
641                                                           String deviceNodeId, List<PhysAddress> macAddresses,
642                                                           String logicalSwitchName, IpAddress remoteVtepIp) {
643         NodeId nodeId = new NodeId(deviceNodeId);
644         HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
645                 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(remoteVtepIp.getValue()));
646         List<RemoteUcastMacs> macs = new ArrayList<RemoteUcastMacs>();
647         for (PhysAddress mac : macAddresses) {
648             // TODO: Query ARP cache to get IP address corresponding to
649             // the MAC
650             IpAddress ipAddress = null;
651             macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId, mac.getValue(), ipAddress, logicalSwitchName,
652                     phyLocatorAug));
653         }
654         return HwvtepUtils.addRemoteUcastMacs(broker, nodeId, macs);
655     }
656
657 }