2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.genius.utils.hwvtep;
11 import static org.opendaylight.mdsal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import java.util.stream.StreamSupport;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
21 import org.opendaylight.genius.infra.Datastore;
22 import org.opendaylight.genius.infra.Datastore.Configuration;
23 import org.opendaylight.genius.infra.TypedReadTransaction;
24 import org.opendaylight.genius.infra.TypedWriteTransaction;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 * Utility class to related to Hardware VTEP devices.
58 public final class HwvtepUtils {
59 private HwvtepUtils() {
63 // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
64 public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
65 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
66 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
70 * Adds the logical switch into config DS.
76 * @param logicalSwitch
78 * @return the listenable future
79 * @deprecated Use {@link #addLogicalSwitch(TypedWriteTransaction, NodeId, LogicalSwitches)}.
82 public static FluentFuture<? extends @NonNull CommitInfo> addLogicalSwitch(DataBroker broker, NodeId nodeId,
83 LogicalSwitches logicalSwitch) {
84 WriteTransaction transaction = broker.newWriteOnlyTransaction();
85 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
86 return transaction.commit();
90 public static FluentFuture<? extends @NonNull CommitInfo> addLogicalSwitch(DataBroker broker,
91 LogicalDatastoreType logicalDatastoreType,
93 LogicalSwitches logicalSwitch) {
94 WriteTransaction transaction = broker.newWriteOnlyTransaction();
95 putLogicalSwitch(transaction,logicalDatastoreType, nodeId, logicalSwitch);
96 return transaction.commit();
100 * Adds the logical switch.
102 * @param tx The configuration transaction.
103 * @param nodeId The node identifier.
104 * @param logicalSwitch The logical switch.
106 public static void addLogicalSwitch(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
107 LogicalSwitches logicalSwitch) {
108 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
109 logicalSwitch.getHwvtepNodeName());
110 tx.put(iid, logicalSwitch, CREATE_MISSING_PARENTS);
114 * Put the logical switches in the transaction.
123 public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
124 final List<LogicalSwitches> lstSwitches) {
125 if (lstSwitches != null) {
126 for (LogicalSwitches logicalSwitch : lstSwitches) {
127 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
133 * Put logical switch in the transaction.
139 * @param logicalSwitch
142 public static void putLogicalSwitch(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
143 final NodeId nodeId, final LogicalSwitches logicalSwitch) {
144 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
145 logicalSwitch.getHwvtepNodeName());
146 transaction.put(logicalDatastoreType, iid, logicalSwitch, true);
150 * Delete logical switch from config DS.
156 * @param logicalSwitchName
157 * the logical switch name
158 * @return the listenable future
159 * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
162 public static FluentFuture<? extends @NonNull CommitInfo> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
163 String logicalSwitchName) {
164 WriteTransaction transaction = broker.newWriteOnlyTransaction();
165 deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
166 return transaction.commit();
170 * Delete logical switch from the transaction.
176 * @param logicalSwitchName
177 * the logical switch name
178 * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
181 public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
182 final String logicalSwitchName) {
183 transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
184 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
188 * Deletes the given logical switch.
190 * @param tx The transaction.
191 * @param nodeId The node identifier.
192 * @param logicalSwitchName The logical switch name.
194 public static void deleteLogicalSwitch(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
195 String logicalSwitchName) {
196 tx.delete(HwvtepSouthboundUtils
197 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
201 * Gets the logical switch.
205 * @param logicalSwitchName
206 * the logical switch name
207 * @return the logical switch
208 * @deprecated Use {@link #getLogicalSwitch(TypedReadTransaction, NodeId, String)}.
211 public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
212 String logicalSwitchName) throws ExecutionException, InterruptedException {
213 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
214 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
215 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
219 * Retrieves the logical switch.
221 * @param tx The transaction to use.
222 * @param nodeId The node identifier.
223 * @param logicalSwitchName The logical switch name.
224 * @return The logical switch, if any.
227 public static LogicalSwitches getLogicalSwitch(TypedReadTransaction<Configuration> tx, NodeId nodeId,
228 String logicalSwitchName) {
229 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
230 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
232 return tx.read(iid).get().orElse(null);
233 } catch (InterruptedException | ExecutionException e) {
234 throw new RuntimeException("Error reading logical switch " + iid, e);
239 * Gets physical port termination point.
243 * @param datastoreType
246 * the physical switch node id
248 * port name under physical switch node id
249 * @return the physical port termination point
251 public static TerminationPoint getPhysicalPortTerminationPoint(DataBroker broker,
252 LogicalDatastoreType datastoreType, NodeId nodeId, String portName) throws ExecutionException,
253 InterruptedException {
254 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
255 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createTerminationPointId(nodeId, tpKey);
256 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
260 * Get LogicalSwitches for a given hwVtepNodeId.
264 * @param hwVtepNodeId
265 * Hardware VTEP Node Id
268 * @return the logical switches
270 public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni)
271 throws ExecutionException, InterruptedException {
272 NodeId nodeId = new NodeId(hwVtepNodeId);
273 InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
274 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
276 return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION,
277 logicalSwitchesIdentifier).orElse(null);
281 * Put physical locators in the transaction.
287 * @param lstPhysicalLocator
288 * the lst physical locator
290 public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
291 List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
292 if (lstPhysicalLocator != null) {
293 for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
294 putPhysicalLocator(transaction, nodeId, phyLocator);
300 * Put physical locator in the transaction.
309 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
310 final HwvtepPhysicalLocatorAugmentation phyLocator) {
311 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
313 TerminationPoint terminationPoint = new TerminationPointBuilder()
314 .withKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
315 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
317 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
321 * Gets the physical locator.
325 * @param datastoreType
329 * @param phyLocatorIp
331 * @return the physical locator
333 public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
334 LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) throws
335 ExecutionException, InterruptedException {
336 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
337 .createHwvtepPhysicalLocatorAugmentation(phyLocatorIp);
338 InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
339 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
340 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
341 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
345 * Adds the remote ucast macs into config DS.
351 * @param lstRemoteUcastMacs
352 * the lst remote ucast macs
353 * @return the listenable future
354 * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
357 public static FluentFuture<? extends @NonNull CommitInfo> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
358 List<RemoteUcastMacs> lstRemoteUcastMacs) {
359 WriteTransaction transaction = broker.newWriteOnlyTransaction();
360 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
361 return transaction.commit();
365 * Adds the given remote unicast MACs.
367 * @param tx The transaction to use.
368 * @param nodeId The node identifier.
369 * @param remoteUcastMacs The MACs to add.
371 public static void addRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
372 Iterable<RemoteUcastMacs> remoteUcastMacs) {
373 if (remoteUcastMacs != null) {
374 remoteUcastMacs.forEach(remoteUcastMac -> addRemoteUcastMac(tx, nodeId, remoteUcastMac));
379 * Adds the given remote unicast MAC.
381 * @param tx The transaction to use.
382 * @param nodeId The node identifier.
383 * @param remoteUcastMac The MAC to add.
385 public static void addRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
386 RemoteUcastMacs remoteUcastMac) {
387 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
388 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
389 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
390 tx.put(iid, remoteUcastMac, CREATE_MISSING_PARENTS);
394 * Put remote ucast macs in the transaction.
400 * @param lstRemoteUcastMacs
401 * the lst remote ucast macs
402 * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
405 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
406 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
407 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
408 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
409 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
415 * Put remote ucast mac in the transaction.
421 * @param remoteUcastMac
422 * the remote ucast mac
423 * @deprecated Use {@link #addRemoteUcastMac(TypedWriteTransaction, NodeId, RemoteUcastMacs)}.
426 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
427 RemoteUcastMacs remoteUcastMac) {
428 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
429 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
430 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
431 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
435 * Delete remote ucast mac from the config DS.
443 * @return the listenable future
444 * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
447 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
448 String logicalSwitchName, MacAddress mac) {
449 WriteTransaction transaction = broker.newWriteOnlyTransaction();
450 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
451 return transaction.commit();
455 * Delete remote ucast mac from the transaction.
463 * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
466 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
467 String logialSwitchName, final MacAddress mac) {
468 transaction.delete(LogicalDatastoreType.CONFIGURATION,
469 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
473 * Deletes the given remote unicast MAC.
475 * @param tx The transaction to use.
476 * @param nodeId The node identifier.
477 * @param logicalSwitchName The logical switch name.
478 * @param macAddress The MAC.
480 public static void deleteRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
481 String logicalSwitchName, MacAddress macAddress) {
482 tx.delete(HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logicalSwitchName, macAddress));
486 * Delete remote ucast macs from the config DS.
494 * @return the listenable future
495 * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
498 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
499 String logicalSwitchName, List<MacAddress> lstMac) {
500 WriteTransaction transaction = broker.newWriteOnlyTransaction();
501 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
502 return transaction.commit();
506 * Delete remote ucast macs from the transaction.
514 * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
517 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
518 String logicalSwitchName, final List<MacAddress> lstMac) {
519 if (lstMac != null && !lstMac.isEmpty()) {
520 for (MacAddress mac : lstMac) {
521 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
527 * Deletes the given remote unicast MACs.
529 * @param tx The transaction to use.
530 * @param nodeId The node identifier.
531 * @param logicalSwitchName The logical switch name.
532 * @param macAddresses The MAC addresses.
534 public static void deleteRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
535 String logicalSwitchName, Iterable<MacAddress> macAddresses) {
536 if (macAddresses != null) {
537 macAddresses.forEach(macAddress -> deleteRemoteUcastMac(tx, nodeId, logicalSwitchName, macAddress));
542 * Adds the remote mcast macs into config DS.
548 * @param lstRemoteMcastMacs
549 * the lst remote mcast macs
550 * @return the listenable future
552 public static FluentFuture<? extends @NonNull CommitInfo> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
553 List<RemoteMcastMacs> lstRemoteMcastMacs) {
554 WriteTransaction transaction = broker.newWriteOnlyTransaction();
555 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
556 return transaction.commit();
560 * Put remote mcast macs in the transaction.
566 * @param lstRemoteMcastMacs
567 * the lst remote mcast macs
569 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
570 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
571 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
572 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
573 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
579 * Put remote mcast mac in the transaction.
585 * @param remoteMcastMac
586 * the remote mcast mac
587 * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
590 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
591 RemoteMcastMacs remoteMcastMac) {
592 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
593 remoteMcastMac.key());
594 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
598 * Adds a remote multicast MAC.
600 * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
603 public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
605 RemoteMcastMacs remoteMcastMac) {
606 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
607 remoteMcastMac.key());
608 transaction.put(logicalDatastoreType, iid, remoteMcastMac, true);
612 * Store a remote multicast MAC.
614 * @param tx The transaction.
615 * @param nodeId The node identifier.
616 * @param remoteMcastMac The remote multicast MAC.
618 public static void addRemoteMcastMac(final TypedWriteTransaction<? extends Datastore> tx, final NodeId nodeId,
619 RemoteMcastMacs remoteMcastMac) {
620 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
621 remoteMcastMac.key());
622 tx.put(iid, remoteMcastMac, CREATE_MISSING_PARENTS);
626 * Gets the remote mcast mac.
630 * @param datastoreType
634 * @param remoteMcastMacsKey
635 * the remote mcast macs key
636 * @return the remote mcast mac
637 * @deprecated Use {@link #getRemoteMcastMac(TypedReadTransaction, NodeId, RemoteMcastMacsKey)}.
640 public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
641 NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey)
642 throws ExecutionException, InterruptedException {
643 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
644 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
645 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
649 * Retrieve a remote multicast MAC.
651 * @param tx The transction to use.
652 * @param nodeId The node identifier.
653 * @param remoteMcastMacsKey The MAC key.
654 * @return The MAC, if any ({@code null} if there is none).
657 public static RemoteMcastMacs getRemoteMcastMac(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId,
658 RemoteMcastMacsKey remoteMcastMacsKey) {
659 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
660 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
662 return tx.read(iid).get().orElse(null);
663 } catch (InterruptedException | ExecutionException e) {
664 throw new RuntimeException("Error reading remote multicast MAC " + iid, e);
669 * Delete remote mcast mac from config DS.
675 * @param remoteMcastMacsKey
676 * the remote mcast macs key
677 * @return the listenable future
678 * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
681 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
682 RemoteMcastMacsKey remoteMcastMacsKey) {
683 WriteTransaction transaction = broker.newWriteOnlyTransaction();
684 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
685 return transaction.commit();
689 * Delete remote mcast mac from the transaction.
695 * @param remoteMcastMacsKey
696 * the remote mcast macs key
697 * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
700 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
701 final RemoteMcastMacsKey remoteMcastMacsKey) {
702 transaction.delete(LogicalDatastoreType.CONFIGURATION,
703 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
707 * Deletes the given remote multicast MAC.
709 * @param tx The configuration transaction.
710 * @param nodeId The node identifier.
711 * @param remoteMcastMacsKey The remote multicast MAC key.
713 public static void deleteRemoteMcastMac(TypedWriteTransaction<Configuration> tx, final NodeId nodeId,
714 final RemoteMcastMacsKey remoteMcastMacsKey) {
715 tx.delete(HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
719 * Delete remote mcast macs from config DS.
725 * @param lstRemoteMcastMacsKey
726 * the lst remote mcast macs key
727 * @return the listenable future
729 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
730 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
731 WriteTransaction transaction = broker.newWriteOnlyTransaction();
732 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
733 return transaction.commit();
737 * Delete remote mcast macs from the transaction.
743 * @param lstRemoteMcastMacsKey
744 * the lst remote mcast macs key
746 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
747 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
748 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
749 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
750 deleteRemoteMcastMac(transaction, nodeId, mac);
756 * Merge vlan bindings in the transaction.
762 * @param phySwitchName
763 * the phy switch name
766 * @param vlanBindings
768 * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, String, List)}.
771 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
772 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
773 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
774 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
778 * Merges the given VLAN bindings.
780 * @param tx The transaction to use.
781 * @param nodeId The node identifier.
782 * @param phySwitchName The physical switch name.
783 * @param phyPortName The physical port name.
784 * @param vlanBindings The VLAN bindings.
786 public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
787 String phySwitchName, String phyPortName, List<VlanBindings> vlanBindings) {
788 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
789 mergeVlanBindings(tx, physicalSwitchNodeId, phyPortName, vlanBindings);
793 * Merge vlan bindings in the transaction.
797 * @param physicalSwitchNodeId
798 * the physical switch node id
801 * @param vlanBindings
803 * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, List)}.
806 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
807 final String phyPortName, final List<VlanBindings> vlanBindings) {
808 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
809 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
811 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
812 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
813 transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
817 * Merges the given VLAN bindings.
819 * @param tx The transaction to use.
820 * @param physicalSwitchNodeId The physical switch’s node identifier.
821 * @param phyPortName The physical port name.
822 * @param vlanBindings The VLAN bindings.
824 public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
825 String phyPortName, List<VlanBindings> vlanBindings) {
826 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
827 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
829 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
830 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
831 tx.merge(iid, phyPortAug, CREATE_MISSING_PARENTS);
835 * Delete vlan binding from transaction.
839 * @param physicalSwitchNodeId
840 * the physical switch node id
845 * @deprecated Use {@link #deleteVlanBinding(TypedWriteTransaction, NodeId, String, Integer)}.
848 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
850 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
851 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
852 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
856 * Deletes the given VLAN binding.
858 * @param tx The transaction to use.
859 * @param physicalSwitchNodeId The physical switch’s node identifier.
860 * @param phyPortName The physical port name.
861 * @param vlanId The VLAN identifier.
863 public static void deleteVlanBinding(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
864 String phyPortName, Integer vlanId) {
865 tx.delete(HwvtepSouthboundUtils.createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId));
869 * Gets the hw vtep node.
873 * @param datastoreType
877 * @return the hw vtep node
878 * @deprecated Use {@link #getHwVtepNode(TypedReadTransaction, NodeId)}.
881 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId)
882 throws ExecutionException, InterruptedException {
883 return SingleTransactionDataBroker.syncReadOptional(dataBroker, datastoreType,
884 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).orElse(null);
888 * Retrieves the hardware VTEP node.
890 * @param tx The transaction.
891 * @param nodeId The node identifier.
892 * @return The hardware VTEP node.
894 public static Node getHwVtepNode(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
896 return tx.read(HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).get().orElse(null);
897 } catch (InterruptedException | ExecutionException e) {
898 throw new RuntimeException("Failed to read hwvtep node", e);
903 * Installs a list of Mac Addresses as remote Ucast address in an external
904 * device using the hwvtep-southbound.
906 * @param deviceNodeId
907 * NodeId if the ExternalDevice where the macs must be installed
909 * @param macAddresses
910 * List of Mac addresses to be installed in the external device.
911 * @param logicalSwitchName
912 * the logical switch name
913 * @param remoteVtepIp
914 * VTEP's IP in this OVS used for the tunnel with external
916 * @deprecated Use {@link #addUcastMacs(TypedWriteTransaction, String, Iterable, String, IpAddress)}.
919 public static FluentFuture<? extends @NonNull CommitInfo> installUcastMacs(DataBroker broker,
920 String deviceNodeId, List<PhysAddress> macAddresses,
921 String logicalSwitchName, IpAddress remoteVtepIp) {
922 NodeId nodeId = new NodeId(deviceNodeId);
923 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
924 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
925 List<RemoteUcastMacs> macs = new ArrayList<>();
926 for (PhysAddress mac : macAddresses) {
927 // TODO: Query ARP cache to get IP address corresponding to
929 //IpAddress ipAddress = null;
930 macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
931 IetfYangUtil.INSTANCE.canonizePhysAddress(mac).getValue(), /*ipAddress*/ null, logicalSwitchName,
934 return addRemoteUcastMacs(broker, nodeId, macs);
940 * @param tx The transaction to use.
941 * @param deviceNodeId The device’s node identifier.
942 * @param macAddresses The MAC addresses.
943 * @param logicalSwitchName The logical switch name.
944 * @param remoteVtepIp The remote VTEP IP address.
946 public static void addUcastMacs(TypedWriteTransaction<Configuration> tx, String deviceNodeId,
947 Iterable<PhysAddress> macAddresses, String logicalSwitchName, IpAddress remoteVtepIp) {
948 NodeId nodeId = new NodeId(deviceNodeId);
949 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
950 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
951 // TODO: Query ARP cache to get IP address corresponding to the MAC
952 StreamSupport.stream(macAddresses.spliterator(), false)
953 .map(macAddress -> HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
954 IetfYangUtil.INSTANCE.canonizePhysAddress(macAddress).getValue(), /*ipAddress*/ null, logicalSwitchName,
956 .forEach(mac -> addRemoteUcastMac(tx, nodeId, mac));
960 * Retrieves the database version.
962 * @deprecated Use {@link #getDbVersion(TypedReadTransaction, NodeId)}.
965 public static String getDbVersion(DataBroker broker, NodeId nodeId) throws ExecutionException,
966 InterruptedException {
967 Node hwvtepNode = getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, nodeId);
968 String dbVersion = "";
969 if (hwvtepNode != null) {
970 dbVersion = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();
976 * Retrieves the database version, as indicated by the hardware VTEP node.
978 * @param tx The transaction.
979 * @param nodeId The node identifier.
980 * @return The database version.
982 public static String getDbVersion(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
983 Node hwvtepNode = getHwVtepNode(tx, nodeId);
984 return hwvtepNode == null ? "" : hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();