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