MDSAL-API Migration
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / utils / hwvtep / HwvtepUtils.java
1 /*
2  * Copyright (c) 2016, 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.genius.utils.hwvtep;
10
11 import static org.opendaylight.mdsal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
12
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import java.util.stream.StreamSupport;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
21 import org.opendaylight.genius.infra.Datastore;
22 import org.opendaylight.genius.infra.Datastore.Configuration;
23 import org.opendaylight.genius.infra.TypedReadTransaction;
24 import org.opendaylight.genius.infra.TypedWriteTransaction;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54
55 /**
56  * Utility class to related to Hardware VTEP devices.
57  */
58 public final class HwvtepUtils {
59     private HwvtepUtils() {
60
61     }
62
63     // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
64     public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
65         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
66                 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
67     }
68
69     /**
70      * Adds the logical switch into config DS.
71      *
72      * @param broker
73      *            the broker
74      * @param nodeId
75      *            the node id
76      * @param logicalSwitch
77      *            the logical switch
78      * @return the listenable future
79      * @deprecated Use {@link #addLogicalSwitch(TypedWriteTransaction, NodeId, LogicalSwitches)}.
80      */
81     @Deprecated
82     public static FluentFuture<? extends @NonNull CommitInfo> addLogicalSwitch(DataBroker broker, NodeId nodeId,
83                                                       LogicalSwitches logicalSwitch) {
84         WriteTransaction transaction = broker.newWriteOnlyTransaction();
85         putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
86         return transaction.commit();
87     }
88
89     @Deprecated
90     public static FluentFuture<? extends @NonNull CommitInfo> addLogicalSwitch(DataBroker broker,
91                                                           LogicalDatastoreType logicalDatastoreType,
92                                                           NodeId nodeId,
93                                                           LogicalSwitches logicalSwitch) {
94         WriteTransaction transaction = broker.newWriteOnlyTransaction();
95         putLogicalSwitch(transaction,logicalDatastoreType, nodeId, logicalSwitch);
96         return transaction.commit();
97     }
98
99     /**
100      * Adds the logical switch.
101      *
102      * @param tx The configuration transaction.
103      * @param nodeId The node identifier.
104      * @param logicalSwitch The logical switch.
105      */
106     public static void addLogicalSwitch(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
107         LogicalSwitches logicalSwitch) {
108         InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
109             logicalSwitch.getHwvtepNodeName());
110         tx.put(iid, logicalSwitch, CREATE_MISSING_PARENTS);
111     }
112
113     /**
114      * Put the logical switches in the transaction.
115      *
116      * @param transaction
117      *            the transaction
118      * @param nodeId
119      *            the node id
120      * @param lstSwitches
121      *            the lst switches
122      */
123     public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
124                                           final List<LogicalSwitches> lstSwitches) {
125         if (lstSwitches != null) {
126             for (LogicalSwitches logicalSwitch : lstSwitches) {
127                 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
128             }
129         }
130     }
131
132     /**
133      * Put logical switch in the transaction.
134      *
135      * @param transaction
136      *            the transaction
137      * @param nodeId
138      *            the node id
139      * @param logicalSwitch
140      *            the logical switch
141      */
142     public static void putLogicalSwitch(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
143                                         final NodeId nodeId, final LogicalSwitches logicalSwitch) {
144         InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
145                 logicalSwitch.getHwvtepNodeName());
146         transaction.put(logicalDatastoreType, iid, logicalSwitch, true);
147     }
148
149     /**
150      * Delete logical switch from config DS.
151      *
152      * @param broker
153      *            the broker
154      * @param nodeId
155      *            the node id
156      * @param logicalSwitchName
157      *            the logical switch name
158      * @return the listenable future
159      * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
160      */
161     @Deprecated
162     public static FluentFuture<? extends @NonNull CommitInfo> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
163                                                              String logicalSwitchName) {
164         WriteTransaction transaction = broker.newWriteOnlyTransaction();
165         deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
166         return transaction.commit();
167     }
168
169     /**
170      * Delete logical switch from the transaction.
171      *
172      * @param transaction
173      *            the transaction
174      * @param nodeId
175      *            the node id
176      * @param logicalSwitchName
177      *            the logical switch name
178      * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
179      */
180     @Deprecated
181     public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
182                                            final String logicalSwitchName) {
183         transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
184                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
185     }
186
187     /**
188      * Deletes the given logical switch.
189      *
190      * @param tx The transaction.
191      * @param nodeId The node identifier.
192      * @param logicalSwitchName The logical switch name.
193      */
194     public static void deleteLogicalSwitch(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
195         String logicalSwitchName) {
196         tx.delete(HwvtepSouthboundUtils
197             .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
198     }
199
200     /**
201      * Gets the logical switch.
202      *
203      * @param nodeId
204      *            the node id
205      * @param logicalSwitchName
206      *            the logical switch name
207      * @return the logical switch
208      * @deprecated Use {@link #getLogicalSwitch(TypedReadTransaction, NodeId, String)}.
209      */
210     @Deprecated
211     public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
212         String logicalSwitchName) throws ExecutionException, InterruptedException {
213         final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
214                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
215         return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
216     }
217
218     /**
219      * Retrieves the logical switch.
220      *
221      * @param tx The transaction to use.
222      * @param nodeId The node identifier.
223      * @param logicalSwitchName The logical switch name.
224      * @return The logical switch, if any.
225      */
226     @Nullable
227     public static LogicalSwitches getLogicalSwitch(TypedReadTransaction<Configuration> tx, NodeId nodeId,
228         String logicalSwitchName) {
229         final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
230             .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
231         try {
232             return tx.read(iid).get().orElse(null);
233         } catch (InterruptedException | ExecutionException e) {
234             throw new RuntimeException("Error reading logical switch " + iid, e);
235         }
236     }
237
238     /**
239      * Gets physical port termination point.
240      *
241      * @param broker
242      *          the broker
243      * @param datastoreType
244      *          the datastore type
245      * @param nodeId
246      *          the physical switch node id
247      * @param portName
248      *          port name under physical switch node id
249      * @return the physical port termination point
250      */
251     public static TerminationPoint getPhysicalPortTerminationPoint(DataBroker broker,
252             LogicalDatastoreType datastoreType, NodeId nodeId, String portName) throws ExecutionException,
253             InterruptedException {
254         TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
255         InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createTerminationPointId(nodeId, tpKey);
256         return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
257     }
258
259     /**
260      * Get LogicalSwitches for a given hwVtepNodeId.
261      *
262      * @param broker
263      *            the broker
264      * @param hwVtepNodeId
265      *            Hardware VTEP Node Id
266      * @param vni
267      *            virtual network id
268      * @return the logical switches
269      */
270     public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni)
271             throws ExecutionException, InterruptedException {
272         NodeId nodeId = new NodeId(hwVtepNodeId);
273         InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
274                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
275
276         return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION,
277                 logicalSwitchesIdentifier).orElse(null);
278     }
279
280     /**
281      * Put physical locators in the transaction.
282      *
283      * @param transaction
284      *            the transaction
285      * @param nodeId
286      *            the node id
287      * @param lstPhysicalLocator
288      *            the lst physical locator
289      */
290     public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
291                                            List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
292         if (lstPhysicalLocator != null) {
293             for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
294                 putPhysicalLocator(transaction, nodeId, phyLocator);
295             }
296         }
297     }
298
299     /**
300      * Put physical locator in the transaction.
301      *
302      * @param transaction
303      *            the transaction
304      * @param nodeId
305      *            the node id
306      * @param phyLocator
307      *            the phy locator
308      */
309     public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
310                                           final HwvtepPhysicalLocatorAugmentation phyLocator) {
311         InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
312                 phyLocator);
313         TerminationPoint terminationPoint = new TerminationPointBuilder()
314                 .withKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
315                 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
316
317         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
318     }
319
320     /**
321      * Gets the physical locator.
322      *
323      * @param broker
324      *            the broker
325      * @param datastoreType
326      *            the datastore type
327      * @param nodeId
328      *            the node id
329      * @param phyLocatorIp
330      *            the phy locator ip
331      * @return the physical locator
332      */
333     public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
334             LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) throws
335             ExecutionException, InterruptedException {
336         HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
337                 .createHwvtepPhysicalLocatorAugmentation(phyLocatorIp);
338         InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
339                 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
340                 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
341         return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
342     }
343
344     /**
345      * Adds the remote ucast macs into config DS.
346      *
347      * @param broker
348      *            the broker
349      * @param nodeId
350      *            the node id
351      * @param lstRemoteUcastMacs
352      *            the lst remote ucast macs
353      * @return the listenable future
354      * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
355      */
356     @Deprecated
357     public static FluentFuture<? extends @NonNull CommitInfo> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
358                                                             List<RemoteUcastMacs> lstRemoteUcastMacs) {
359         WriteTransaction transaction = broker.newWriteOnlyTransaction();
360         putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
361         return transaction.commit();
362     }
363
364     /**
365      * Adds the given remote unicast MACs.
366      *
367      * @param tx The transaction to use.
368      * @param nodeId The node identifier.
369      * @param remoteUcastMacs The MACs to add.
370      */
371     public static void addRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
372         Iterable<RemoteUcastMacs> remoteUcastMacs) {
373         if (remoteUcastMacs != null) {
374             remoteUcastMacs.forEach(remoteUcastMac -> addRemoteUcastMac(tx, nodeId, remoteUcastMac));
375         }
376     }
377
378     /**
379      * Adds the given remote unicast MAC.
380      *
381      * @param tx The transaction to use.
382      * @param nodeId The node identifier.
383      * @param remoteUcastMac The MAC to add.
384      */
385     public static void addRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
386         RemoteUcastMacs remoteUcastMac) {
387         InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
388             .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
389                 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
390         tx.put(iid, remoteUcastMac, CREATE_MISSING_PARENTS);
391     }
392
393     /**
394      * Put remote ucast macs in the transaction.
395      *
396      * @param transaction
397      *            the transaction
398      * @param nodeId
399      *            the node id
400      * @param lstRemoteUcastMacs
401      *            the lst remote ucast macs
402      * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
403      */
404     @Deprecated
405     public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
406                                           final List<RemoteUcastMacs> lstRemoteUcastMacs) {
407         if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
408             for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
409                 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
410             }
411         }
412     }
413
414     /**
415      * Put remote ucast mac in the transaction.
416      *
417      * @param transaction
418      *            the transaction
419      * @param nodeId
420      *            the node id
421      * @param remoteUcastMac
422      *            the remote ucast mac
423      * @deprecated Use {@link #addRemoteUcastMac(TypedWriteTransaction, NodeId, RemoteUcastMacs)}.
424      */
425     @Deprecated
426     public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
427                                          RemoteUcastMacs remoteUcastMac) {
428         InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
429                 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
430                         new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
431         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
432     }
433
434     /**
435      * Delete remote ucast mac from the config DS.
436      *
437      * @param broker
438      *            the broker
439      * @param nodeId
440      *            the node id
441      * @param mac
442      *            the mac
443      * @return the listenable future
444      * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
445      */
446     @Deprecated
447     public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
448                                                               String logicalSwitchName, MacAddress mac) {
449         WriteTransaction transaction = broker.newWriteOnlyTransaction();
450         deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
451         return transaction.commit();
452     }
453
454     /**
455      * Delete remote ucast mac from the transaction.
456      *
457      * @param transaction
458      *            the transaction
459      * @param nodeId
460      *            the node id
461      * @param mac
462      *            the mac
463      * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
464      */
465     @Deprecated
466     public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
467                                             String logialSwitchName, final MacAddress mac) {
468         transaction.delete(LogicalDatastoreType.CONFIGURATION,
469                 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
470     }
471
472     /**
473      * Deletes the given remote unicast MAC.
474      *
475      * @param tx The transaction to use.
476      * @param nodeId The node identifier.
477      * @param logicalSwitchName The logical switch name.
478      * @param macAddress The MAC.
479      */
480     public static void deleteRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
481         String logicalSwitchName, MacAddress macAddress) {
482         tx.delete(HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logicalSwitchName, macAddress));
483     }
484
485     /**
486      * Delete remote ucast macs from the config DS.
487      *
488      * @param broker
489      *            the broker
490      * @param nodeId
491      *            the node id
492      * @param lstMac
493      *            the lst mac
494      * @return the listenable future
495      * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
496      */
497     @Deprecated
498     public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
499                                                                String logicalSwitchName, List<MacAddress> lstMac) {
500         WriteTransaction transaction = broker.newWriteOnlyTransaction();
501         deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
502         return transaction.commit();
503     }
504
505     /**
506      * Delete remote ucast macs from the transaction.
507      *
508      * @param transaction
509      *            the transaction
510      * @param nodeId
511      *            the node id
512      * @param lstMac
513      *            the lst mac
514      * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
515      */
516     @Deprecated
517     public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
518                                              String logicalSwitchName, final List<MacAddress> lstMac) {
519         if (lstMac != null && !lstMac.isEmpty()) {
520             for (MacAddress mac : lstMac) {
521                 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
522             }
523         }
524     }
525
526     /**
527      * Deletes the given remote unicast MACs.
528      *
529      * @param tx The transaction to use.
530      * @param nodeId The node identifier.
531      * @param logicalSwitchName The logical switch name.
532      * @param macAddresses The MAC addresses.
533      */
534     public static void deleteRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
535         String logicalSwitchName, Iterable<MacAddress> macAddresses) {
536         if (macAddresses != null) {
537             macAddresses.forEach(macAddress -> deleteRemoteUcastMac(tx, nodeId, logicalSwitchName, macAddress));
538         }
539     }
540
541     /**
542      * Adds the remote mcast macs into config DS.
543      *
544      * @param broker
545      *            the broker
546      * @param nodeId
547      *            the node id
548      * @param lstRemoteMcastMacs
549      *            the lst remote mcast macs
550      * @return the listenable future
551      */
552     public static FluentFuture<? extends @NonNull CommitInfo> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
553                                                             List<RemoteMcastMacs> lstRemoteMcastMacs) {
554         WriteTransaction transaction = broker.newWriteOnlyTransaction();
555         putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
556         return transaction.commit();
557     }
558
559     /**
560      * Put remote mcast macs in the transaction.
561      *
562      * @param transaction
563      *            the transaction
564      * @param nodeId
565      *            the node id
566      * @param lstRemoteMcastMacs
567      *            the lst remote mcast macs
568      */
569     public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
570                                           final List<RemoteMcastMacs> lstRemoteMcastMacs) {
571         if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
572             for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
573                 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
574             }
575         }
576     }
577
578     /**
579      * Put remote mcast mac in the transaction.
580      *
581      * @param transaction
582      *            the transaction
583      * @param nodeId
584      *            the node id
585      * @param remoteMcastMac
586      *            the remote mcast mac
587      * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
588      */
589     @Deprecated
590     public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
591                                          RemoteMcastMacs remoteMcastMac) {
592         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
593                 remoteMcastMac.key());
594         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
595     }
596
597     /**
598      * Adds a remote multicast MAC.
599      *
600      * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
601      */
602     @Deprecated
603     public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
604                                          final NodeId nodeId,
605                                          RemoteMcastMacs remoteMcastMac) {
606         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
607                 remoteMcastMac.key());
608         transaction.put(logicalDatastoreType, iid, remoteMcastMac, true);
609     }
610
611     /**
612      * Store a remote multicast MAC.
613      *
614      * @param tx The transaction.
615      * @param nodeId The node identifier.
616      * @param remoteMcastMac The remote multicast MAC.
617      */
618     public static void addRemoteMcastMac(final TypedWriteTransaction<? extends Datastore> tx, final NodeId nodeId,
619         RemoteMcastMacs remoteMcastMac) {
620         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
621             remoteMcastMac.key());
622         tx.put(iid, remoteMcastMac, CREATE_MISSING_PARENTS);
623     }
624
625     /**
626      * Gets the remote mcast mac.
627      *
628      * @param broker
629      *            the broker
630      * @param datastoreType
631      *            the datastore type
632      * @param nodeId
633      *            the node id
634      * @param remoteMcastMacsKey
635      *            the remote mcast macs key
636      * @return the remote mcast mac
637      * @deprecated Use {@link #getRemoteMcastMac(TypedReadTransaction, NodeId, RemoteMcastMacsKey)}.
638      */
639     @Deprecated
640     public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
641                                                     NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey)
642             throws ExecutionException, InterruptedException {
643         final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
644                 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
645         return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
646     }
647
648     /**
649      * Retrieve a remote multicast MAC.
650      *
651      * @param tx The transction to use.
652      * @param nodeId The node identifier.
653      * @param remoteMcastMacsKey The MAC key.
654      * @return The MAC, if any ({@code null} if there is none).
655      */
656     @Nullable
657     public static RemoteMcastMacs getRemoteMcastMac(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId,
658         RemoteMcastMacsKey remoteMcastMacsKey) {
659         final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
660             .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
661         try {
662             return tx.read(iid).get().orElse(null);
663         } catch (InterruptedException | ExecutionException e) {
664             throw new RuntimeException("Error reading remote multicast MAC " + iid, e);
665         }
666     }
667
668     /**
669      * Delete remote mcast mac from config DS.
670      *
671      * @param broker
672      *            the broker
673      * @param nodeId
674      *            the node id
675      * @param remoteMcastMacsKey
676      *            the remote mcast macs key
677      * @return the listenable future
678      * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
679      */
680     @Deprecated
681     public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
682                                                               RemoteMcastMacsKey remoteMcastMacsKey) {
683         WriteTransaction transaction = broker.newWriteOnlyTransaction();
684         deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
685         return transaction.commit();
686     }
687
688     /**
689      * Delete remote mcast mac from the transaction.
690      *
691      * @param transaction
692      *            the transaction
693      * @param nodeId
694      *            the node id
695      * @param remoteMcastMacsKey
696      *            the remote mcast macs key
697      * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
698      */
699     @Deprecated
700     public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
701                                             final RemoteMcastMacsKey remoteMcastMacsKey) {
702         transaction.delete(LogicalDatastoreType.CONFIGURATION,
703                 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
704     }
705
706     /**
707      * Deletes the given remote multicast MAC.
708      *
709      * @param tx The configuration transaction.
710      * @param nodeId The node identifier.
711      * @param remoteMcastMacsKey The remote multicast MAC key.
712      */
713     public static void deleteRemoteMcastMac(TypedWriteTransaction<Configuration> tx, final NodeId nodeId,
714         final RemoteMcastMacsKey remoteMcastMacsKey) {
715         tx.delete(HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
716     }
717
718     /**
719      * Delete remote mcast macs from config DS.
720      *
721      * @param broker
722      *            the broker
723      * @param nodeId
724      *            the node id
725      * @param lstRemoteMcastMacsKey
726      *            the lst remote mcast macs key
727      * @return the listenable future
728      */
729     public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
730                                                                List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
731         WriteTransaction transaction = broker.newWriteOnlyTransaction();
732         deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
733         return transaction.commit();
734     }
735
736     /**
737      * Delete remote mcast macs from the transaction.
738      *
739      * @param transaction
740      *            the transaction
741      * @param nodeId
742      *            the node id
743      * @param lstRemoteMcastMacsKey
744      *            the lst remote mcast macs key
745      */
746     public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
747                                              final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
748         if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
749             for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
750                 deleteRemoteMcastMac(transaction, nodeId, mac);
751             }
752         }
753     }
754
755     /**
756      * Merge vlan bindings in the transaction.
757      *
758      * @param transaction
759      *            the transaction
760      * @param nodeId
761      *            the node id
762      * @param phySwitchName
763      *            the phy switch name
764      * @param phyPortName
765      *            the phy port name
766      * @param vlanBindings
767      *            the vlan bindings
768      * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, String, List)}.
769      */
770     @Deprecated
771     public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
772             final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
773         NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
774         mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
775     }
776
777     /**
778      * Merges the given VLAN bindings.
779      *
780      * @param tx The transaction to use.
781      * @param nodeId The node identifier.
782      * @param phySwitchName The physical switch name.
783      * @param phyPortName The physical port name.
784      * @param vlanBindings The VLAN bindings.
785      */
786     public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
787         String phySwitchName, String phyPortName, List<VlanBindings> vlanBindings) {
788         NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
789         mergeVlanBindings(tx, physicalSwitchNodeId, phyPortName, vlanBindings);
790     }
791
792     /**
793      * Merge vlan bindings in the transaction.
794      *
795      * @param transaction
796      *            the transaction
797      * @param physicalSwitchNodeId
798      *            the physical switch node id
799      * @param phyPortName
800      *            the phy port name
801      * @param vlanBindings
802      *            the vlan bindings
803      * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, List)}.
804      */
805     @Deprecated
806     public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
807                                          final String phyPortName, final List<VlanBindings> vlanBindings) {
808         HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
809                 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
810
811         final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
812                 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
813         transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
814     }
815
816     /**
817      * Merges the given VLAN bindings.
818      *
819      * @param tx The transaction to use.
820      * @param physicalSwitchNodeId The physical switch’s node identifier.
821      * @param phyPortName The physical port name.
822      * @param vlanBindings The VLAN bindings.
823      */
824     public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
825         String phyPortName, List<VlanBindings> vlanBindings) {
826         HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
827             .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
828
829         final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
830             .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
831         tx.merge(iid, phyPortAug, CREATE_MISSING_PARENTS);
832     }
833
834     /**
835      * Delete vlan binding from transaction.
836      *
837      * @param transaction
838      *            the transaction
839      * @param physicalSwitchNodeId
840      *            the physical switch node id
841      * @param phyPortName
842      *            the phy port name
843      * @param vlanId
844      *            the vlan id
845      * @deprecated Use {@link #deleteVlanBinding(TypedWriteTransaction, NodeId, String, Integer)}.
846      */
847     @Deprecated
848     public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
849                                          Integer vlanId) {
850         InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
851                 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
852         transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
853     }
854
855     /**
856      * Deletes the given VLAN binding.
857      *
858      * @param tx The transaction to use.
859      * @param physicalSwitchNodeId The physical switch’s node identifier.
860      * @param phyPortName The physical port name.
861      * @param vlanId The VLAN identifier.
862      */
863     public static void deleteVlanBinding(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
864         String phyPortName, Integer vlanId) {
865         tx.delete(HwvtepSouthboundUtils.createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId));
866     }
867
868     /**
869      * Gets the hw vtep node.
870      *
871      * @param dataBroker
872      *            the data broker
873      * @param datastoreType
874      *            the datastore type
875      * @param nodeId
876      *            the node id
877      * @return the hw vtep node
878      * @deprecated Use {@link #getHwVtepNode(TypedReadTransaction, NodeId)}.
879      */
880     @Deprecated
881     public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId)
882             throws ExecutionException, InterruptedException {
883         return SingleTransactionDataBroker.syncReadOptional(dataBroker, datastoreType,
884                 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).orElse(null);
885     }
886
887     /**
888      * Retrieves the hardware VTEP node.
889      *
890      * @param tx The transaction.
891      * @param nodeId The node identifier.
892      * @return The hardware VTEP node.
893      */
894     public static Node getHwVtepNode(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
895         try {
896             return tx.read(HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).get().orElse(null);
897         } catch (InterruptedException | ExecutionException e) {
898             throw new RuntimeException("Failed to read hwvtep node", e);
899         }
900     }
901
902     /**
903      * Installs a list of Mac Addresses as remote Ucast address in an external
904      * device using the hwvtep-southbound.
905      *
906      * @param deviceNodeId
907      *            NodeId if the ExternalDevice where the macs must be installed
908      *            in.
909      * @param macAddresses
910      *            List of Mac addresses to be installed in the external device.
911      * @param logicalSwitchName
912      *            the logical switch name
913      * @param remoteVtepIp
914      *            VTEP's IP in this OVS used for the tunnel with external
915      *            device.
916      * @deprecated Use {@link #addUcastMacs(TypedWriteTransaction, String, Iterable, String, IpAddress)}.
917      */
918     @Deprecated
919     public static FluentFuture<? extends @NonNull CommitInfo> installUcastMacs(DataBroker broker,
920                                                           String deviceNodeId, List<PhysAddress> macAddresses,
921                                                           String logicalSwitchName, IpAddress remoteVtepIp) {
922         NodeId nodeId = new NodeId(deviceNodeId);
923         HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
924                 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
925         List<RemoteUcastMacs> macs = new ArrayList<>();
926         for (PhysAddress mac : macAddresses) {
927             // TODO: Query ARP cache to get IP address corresponding to
928             // the MAC
929             //IpAddress ipAddress = null;
930             macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
931                 IetfYangUtil.INSTANCE.canonizePhysAddress(mac).getValue(), /*ipAddress*/ null, logicalSwitchName,
932                 phyLocatorAug));
933         }
934         return addRemoteUcastMacs(broker, nodeId, macs);
935     }
936
937     /**
938      * Adds unicast MACs.
939      *
940      * @param tx The transaction to use.
941      * @param deviceNodeId The device’s node identifier.
942      * @param macAddresses The MAC addresses.
943      * @param logicalSwitchName The logical switch name.
944      * @param remoteVtepIp The remote VTEP IP address.
945      */
946     public static void addUcastMacs(TypedWriteTransaction<Configuration> tx, String deviceNodeId,
947         Iterable<PhysAddress> macAddresses, String logicalSwitchName, IpAddress remoteVtepIp) {
948         NodeId nodeId = new NodeId(deviceNodeId);
949         HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
950             .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
951         // TODO: Query ARP cache to get IP address corresponding to the MAC
952         StreamSupport.stream(macAddresses.spliterator(), false)
953             .map(macAddress -> HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
954                 IetfYangUtil.INSTANCE.canonizePhysAddress(macAddress).getValue(), /*ipAddress*/ null, logicalSwitchName,
955                 phyLocatorAug))
956             .forEach(mac -> addRemoteUcastMac(tx, nodeId, mac));
957     }
958
959     /**
960      * Retrieves the database version.
961      *
962      * @deprecated Use {@link #getDbVersion(TypedReadTransaction, NodeId)}.
963      */
964     @Deprecated
965     public static String getDbVersion(DataBroker broker, NodeId nodeId) throws ExecutionException,
966             InterruptedException {
967         Node hwvtepNode = getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, nodeId);
968         String dbVersion = "";
969         if (hwvtepNode != null) {
970             dbVersion = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();
971         }
972         return dbVersion;
973     }
974
975     /**
976      * Retrieves the database version, as indicated by the hardware VTEP node.
977      *
978      * @param tx The transaction.
979      * @param nodeId The node identifier.
980      * @return The database version.
981      */
982     public static String getDbVersion(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
983         Node hwvtepNode = getHwVtepNode(tx, nodeId);
984         return hwvtepNode == null ? "" :  hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();
985     }
986
987 }