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