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