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.
137 * @param logicalDatastoreType the LogicalDatastoreType
140 * @param logicalSwitch
143 public static void putLogicalSwitch(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
144 final NodeId nodeId, final LogicalSwitches logicalSwitch) {
145 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
146 logicalSwitch.getHwvtepNodeName());
147 transaction.put(logicalDatastoreType, iid, logicalSwitch, true);
151 * Delete logical switch from config DS.
157 * @param logicalSwitchName
158 * the logical switch name
159 * @return the listenable future
160 * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
163 public static FluentFuture<? extends @NonNull CommitInfo> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
164 String logicalSwitchName) {
165 WriteTransaction transaction = broker.newWriteOnlyTransaction();
166 deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
167 return transaction.commit();
171 * Delete logical switch from the transaction.
177 * @param logicalSwitchName
178 * the logical switch name
179 * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
182 public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
183 final String logicalSwitchName) {
184 transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
185 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
189 * Deletes the given logical switch.
191 * @param tx The transaction.
192 * @param nodeId The node identifier.
193 * @param logicalSwitchName The logical switch name.
195 public static void deleteLogicalSwitch(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
196 String logicalSwitchName) {
197 tx.delete(HwvtepSouthboundUtils
198 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
202 * Gets the logical switch.
204 * @param broker the broker
205 * @param datastoreType the datastore type
208 * @param logicalSwitchName
209 * the logical switch name
210 * @return the logical switch
211 * @deprecated Use {@link #getLogicalSwitch(TypedReadTransaction, NodeId, String)}.
212 * @throws ExecutionException in case of a technical (!) error while reading
213 * @throws InterruptedException if the transaction is interrupted.
216 public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
217 String logicalSwitchName) throws ExecutionException, InterruptedException {
218 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
219 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
220 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
224 * Retrieves the logical switch.
226 * @param tx The transaction to use.
227 * @param nodeId The node identifier.
228 * @param logicalSwitchName The logical switch name.
229 * @return The logical switch, if any.
232 public static LogicalSwitches getLogicalSwitch(TypedReadTransaction<Configuration> tx, NodeId nodeId,
233 String logicalSwitchName) {
234 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
235 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
237 return tx.read(iid).get().orElse(null);
238 } catch (InterruptedException | ExecutionException e) {
239 throw new RuntimeException("Error reading logical switch " + iid, e);
244 * Gets physical port termination point.
248 * @param datastoreType
251 * the physical switch node id
253 * port name under physical switch node id
254 * @return the physical port termination point
255 * @throws ExecutionException in case of a technical (!) error while reading
256 * @throws InterruptedException if the transaction is interrupted.
258 public static TerminationPoint getPhysicalPortTerminationPoint(DataBroker broker,
259 LogicalDatastoreType datastoreType, NodeId nodeId, String portName) throws ExecutionException,
260 InterruptedException {
261 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
262 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createTerminationPointId(nodeId, tpKey);
263 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
267 * Get LogicalSwitches for a given hwVtepNodeId.
271 * @param hwVtepNodeId
272 * Hardware VTEP Node Id
275 * @return the logical switches
276 * @throws ExecutionException in case of a technical (!) error while reading
277 * @throws InterruptedException if the transaction is interrupted.
279 public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni)
280 throws ExecutionException, InterruptedException {
281 NodeId nodeId = new NodeId(hwVtepNodeId);
282 InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
283 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
285 return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION,
286 logicalSwitchesIdentifier).orElse(null);
290 * Put physical locators in the transaction.
296 * @param lstPhysicalLocator
297 * the lst physical locator
299 public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
300 List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
301 if (lstPhysicalLocator != null) {
302 for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
303 putPhysicalLocator(transaction, nodeId, phyLocator);
309 * Put physical locator in the transaction.
318 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
319 final HwvtepPhysicalLocatorAugmentation phyLocator) {
320 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
322 TerminationPoint terminationPoint = new TerminationPointBuilder()
323 .withKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
324 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
326 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
330 * Gets the physical locator.
334 * @param datastoreType
338 * @param phyLocatorIp
340 * @return the physical locator
341 * @throws ExecutionException in case of a technical (!) error while reading
342 * @throws InterruptedException if the transaction is interrupted.
344 public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
345 LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) throws
346 ExecutionException, InterruptedException {
347 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
348 .createHwvtepPhysicalLocatorAugmentation(phyLocatorIp);
349 InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
350 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
351 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
352 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
356 * Adds the remote ucast macs into config DS.
362 * @param lstRemoteUcastMacs
363 * the lst remote ucast macs
364 * @return the listenable future
365 * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
368 public static FluentFuture<? extends @NonNull CommitInfo> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
369 List<RemoteUcastMacs> lstRemoteUcastMacs) {
370 WriteTransaction transaction = broker.newWriteOnlyTransaction();
371 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
372 return transaction.commit();
376 * Adds the given remote unicast MACs.
378 * @param tx The transaction to use.
379 * @param nodeId The node identifier.
380 * @param remoteUcastMacs The MACs to add.
382 public static void addRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
383 Iterable<RemoteUcastMacs> remoteUcastMacs) {
384 if (remoteUcastMacs != null) {
385 remoteUcastMacs.forEach(remoteUcastMac -> addRemoteUcastMac(tx, nodeId, remoteUcastMac));
390 * Adds the given remote unicast MAC.
392 * @param tx The transaction to use.
393 * @param nodeId The node identifier.
394 * @param remoteUcastMac The MAC to add.
396 public static void addRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
397 RemoteUcastMacs remoteUcastMac) {
398 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
399 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
400 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
401 tx.put(iid, remoteUcastMac, CREATE_MISSING_PARENTS);
405 * Put remote ucast macs in the transaction.
411 * @param lstRemoteUcastMacs
412 * the lst remote ucast macs
413 * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
416 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
417 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
418 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
419 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
420 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
426 * Put remote ucast mac in the transaction.
432 * @param remoteUcastMac
433 * the remote ucast mac
434 * @deprecated Use {@link #addRemoteUcastMac(TypedWriteTransaction, NodeId, RemoteUcastMacs)}.
437 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
438 RemoteUcastMacs remoteUcastMac) {
439 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
440 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
441 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
442 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
446 * Delete remote ucast mac from the config DS.
452 * @param logicalSwitchName the logical switch name
455 * @return the listenable future
456 * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
459 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
460 String logicalSwitchName, MacAddress mac) {
461 WriteTransaction transaction = broker.newWriteOnlyTransaction();
462 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
463 return transaction.commit();
467 * Delete remote ucast mac from the transaction.
473 * @param logialSwitchName the logical switch name
476 * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
479 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
480 String logialSwitchName, final MacAddress mac) {
481 transaction.delete(LogicalDatastoreType.CONFIGURATION,
482 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
486 * Deletes the given remote unicast MAC.
488 * @param tx The transaction to use.
489 * @param nodeId The node identifier.
490 * @param logicalSwitchName The logical switch name.
491 * @param macAddress The MAC.
493 public static void deleteRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
494 String logicalSwitchName, MacAddress macAddress) {
495 tx.delete(HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logicalSwitchName, macAddress));
499 * Delete remote ucast macs from the config DS.
505 * @param logicalSwitchName the logical switch name
508 * @return the listenable future
509 * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
512 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
513 String logicalSwitchName, List<MacAddress> lstMac) {
514 WriteTransaction transaction = broker.newWriteOnlyTransaction();
515 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
516 return transaction.commit();
520 * Delete remote ucast macs from the transaction.
526 * @param logicalSwitchName the logical switch name
529 * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
532 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
533 String logicalSwitchName, final List<MacAddress> lstMac) {
534 if (lstMac != null && !lstMac.isEmpty()) {
535 for (MacAddress mac : lstMac) {
536 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
542 * Deletes the given remote unicast MACs.
544 * @param tx The transaction to use.
545 * @param nodeId The node identifier.
546 * @param logicalSwitchName The logical switch name.
547 * @param macAddresses The MAC addresses.
549 public static void deleteRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
550 String logicalSwitchName, Iterable<MacAddress> macAddresses) {
551 if (macAddresses != null) {
552 macAddresses.forEach(macAddress -> deleteRemoteUcastMac(tx, nodeId, logicalSwitchName, macAddress));
557 * Adds the remote mcast macs into config DS.
563 * @param lstRemoteMcastMacs
564 * the lst remote mcast macs
565 * @return the listenable future
567 public static FluentFuture<? extends @NonNull CommitInfo> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
568 List<RemoteMcastMacs> lstRemoteMcastMacs) {
569 WriteTransaction transaction = broker.newWriteOnlyTransaction();
570 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
571 return transaction.commit();
575 * Put remote mcast macs in the transaction.
581 * @param lstRemoteMcastMacs
582 * the lst remote mcast macs
584 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
585 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
586 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
587 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
588 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
594 * Put remote mcast mac in the transaction.
600 * @param remoteMcastMac
601 * the remote mcast mac
602 * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
605 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
606 RemoteMcastMacs remoteMcastMac) {
607 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
608 remoteMcastMac.key());
609 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
613 * Adds a remote multicast MAC.
615 * @param transaction the transaction
616 * @param logicalDatastoreType the LogicalDatastoreType
617 * @param nodeId the node id
618 * @param remoteMcastMac the remote mcast mac
619 * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
622 public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
624 RemoteMcastMacs remoteMcastMac) {
625 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
626 remoteMcastMac.key());
627 transaction.put(logicalDatastoreType, iid, remoteMcastMac, true);
631 * Store a remote multicast MAC.
633 * @param tx The transaction.
634 * @param nodeId The node identifier.
635 * @param remoteMcastMac The remote multicast MAC.
637 public static void addRemoteMcastMac(final TypedWriteTransaction<? extends Datastore> tx, final NodeId nodeId,
638 RemoteMcastMacs remoteMcastMac) {
639 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
640 remoteMcastMac.key());
641 tx.put(iid, remoteMcastMac, CREATE_MISSING_PARENTS);
645 * Gets the remote mcast mac.
649 * @param datastoreType
653 * @param remoteMcastMacsKey
654 * the remote mcast macs key
655 * @return the remote mcast mac
656 * @deprecated Use {@link #getRemoteMcastMac(TypedReadTransaction, NodeId, RemoteMcastMacsKey)}.
657 * @throws ExecutionException in case of a technical (!) error while reading
658 * @throws InterruptedException if the transaction is interrupted.
661 public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
662 NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey)
663 throws ExecutionException, InterruptedException {
664 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
665 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
666 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
670 * Retrieve a remote multicast MAC.
672 * @param tx The transction to use.
673 * @param nodeId The node identifier.
674 * @param remoteMcastMacsKey The MAC key.
675 * @return The MAC, if any ({@code null} if there is none).
678 public static RemoteMcastMacs getRemoteMcastMac(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId,
679 RemoteMcastMacsKey remoteMcastMacsKey) {
680 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
681 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
683 return tx.read(iid).get().orElse(null);
684 } catch (InterruptedException | ExecutionException e) {
685 throw new RuntimeException("Error reading remote multicast MAC " + iid, e);
690 * Delete remote mcast mac from config DS.
696 * @param remoteMcastMacsKey
697 * the remote mcast macs key
698 * @return the listenable future
699 * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
702 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
703 RemoteMcastMacsKey remoteMcastMacsKey) {
704 WriteTransaction transaction = broker.newWriteOnlyTransaction();
705 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
706 return transaction.commit();
710 * Delete remote mcast mac from the transaction.
716 * @param remoteMcastMacsKey
717 * the remote mcast macs key
718 * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
721 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
722 final RemoteMcastMacsKey remoteMcastMacsKey) {
723 transaction.delete(LogicalDatastoreType.CONFIGURATION,
724 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
728 * Deletes the given remote multicast MAC.
730 * @param tx The configuration transaction.
731 * @param nodeId The node identifier.
732 * @param remoteMcastMacsKey The remote multicast MAC key.
734 public static void deleteRemoteMcastMac(TypedWriteTransaction<Configuration> tx, final NodeId nodeId,
735 final RemoteMcastMacsKey remoteMcastMacsKey) {
736 tx.delete(HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
740 * Delete remote mcast macs from config DS.
746 * @param lstRemoteMcastMacsKey
747 * the lst remote mcast macs key
748 * @return the listenable future
750 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
751 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
752 WriteTransaction transaction = broker.newWriteOnlyTransaction();
753 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
754 return transaction.commit();
758 * Delete remote mcast macs from the transaction.
764 * @param lstRemoteMcastMacsKey
765 * the lst remote mcast macs key
767 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
768 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
769 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
770 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
771 deleteRemoteMcastMac(transaction, nodeId, mac);
777 * Merge vlan bindings in the transaction.
783 * @param phySwitchName
784 * the phy switch name
787 * @param vlanBindings
789 * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, String, List)}.
792 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
793 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
794 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
795 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
799 * Merges the given VLAN bindings.
801 * @param tx The transaction to use.
802 * @param nodeId The node identifier.
803 * @param phySwitchName The physical switch name.
804 * @param phyPortName The physical port name.
805 * @param vlanBindings The VLAN bindings.
807 public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
808 String phySwitchName, String phyPortName, List<VlanBindings> vlanBindings) {
809 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
810 mergeVlanBindings(tx, physicalSwitchNodeId, phyPortName, vlanBindings);
814 * Merge vlan bindings in the transaction.
818 * @param physicalSwitchNodeId
819 * the physical switch node id
822 * @param vlanBindings
824 * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, List)}.
827 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
828 final String phyPortName, final List<VlanBindings> vlanBindings) {
829 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
830 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
832 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
833 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
834 transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
838 * Merges the given VLAN bindings.
840 * @param tx The transaction to use.
841 * @param physicalSwitchNodeId The physical switch’s node identifier.
842 * @param phyPortName The physical port name.
843 * @param vlanBindings The VLAN bindings.
845 public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
846 String phyPortName, List<VlanBindings> vlanBindings) {
847 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
848 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
850 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
851 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
852 tx.merge(iid, phyPortAug, CREATE_MISSING_PARENTS);
856 * Delete vlan binding from transaction.
860 * @param physicalSwitchNodeId
861 * the physical switch node id
866 * @deprecated Use {@link #deleteVlanBinding(TypedWriteTransaction, NodeId, String, Integer)}.
869 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
871 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
872 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
873 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
877 * Deletes the given VLAN binding.
879 * @param tx The transaction to use.
880 * @param physicalSwitchNodeId The physical switch’s node identifier.
881 * @param phyPortName The physical port name.
882 * @param vlanId The VLAN identifier.
884 public static void deleteVlanBinding(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
885 String phyPortName, Integer vlanId) {
886 tx.delete(HwvtepSouthboundUtils.createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId));
890 * Gets the hw vtep node.
894 * @param datastoreType
898 * @return the hw vtep node
899 * @deprecated Use {@link #getHwVtepNode(TypedReadTransaction, NodeId)}.
900 * @throws ExecutionException in case of a technical (!) error while reading
901 * @throws InterruptedException if the transaction is interrupted.
904 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId)
905 throws ExecutionException, InterruptedException {
906 return SingleTransactionDataBroker.syncReadOptional(dataBroker, datastoreType,
907 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).orElse(null);
911 * Retrieves the hardware VTEP node.
913 * @param tx The transaction.
914 * @param nodeId The node identifier.
915 * @return The hardware VTEP node.
917 public static Node getHwVtepNode(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
919 return tx.read(HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).get().orElse(null);
920 } catch (InterruptedException | ExecutionException e) {
921 throw new RuntimeException("Failed to read hwvtep node", e);
926 * Installs a list of Mac Addresses as remote Ucast address in an external
927 * device using the hwvtep-southbound.
929 * @param broker the databroker
930 * @param deviceNodeId
931 * NodeId if the ExternalDevice where the macs must be installed
933 * @param macAddresses
934 * List of Mac addresses to be installed in the external device.
935 * @param logicalSwitchName
936 * the logical switch name
937 * @param remoteVtepIp
938 * VTEP's IP in this OVS used for the tunnel with external
940 * @deprecated Use {@link #addUcastMacs(TypedWriteTransaction, String, Iterable, String, IpAddress)}.
941 * @return future if present
944 public static FluentFuture<? extends @NonNull CommitInfo> installUcastMacs(DataBroker broker,
945 String deviceNodeId, List<PhysAddress> macAddresses,
946 String logicalSwitchName, IpAddress remoteVtepIp) {
947 NodeId nodeId = new NodeId(deviceNodeId);
948 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
949 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
950 List<RemoteUcastMacs> macs = new ArrayList<>();
951 for (PhysAddress mac : macAddresses) {
952 // TODO: Query ARP cache to get IP address corresponding to
954 //IpAddress ipAddress = null;
955 macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
956 IetfYangUtil.INSTANCE.canonizePhysAddress(mac).getValue(), /*ipAddress*/ null, logicalSwitchName,
959 return addRemoteUcastMacs(broker, nodeId, macs);
965 * @param tx The transaction to use.
966 * @param deviceNodeId The device’s node identifier.
967 * @param macAddresses The MAC addresses.
968 * @param logicalSwitchName The logical switch name.
969 * @param remoteVtepIp The remote VTEP IP address.
971 public static void addUcastMacs(TypedWriteTransaction<Configuration> tx, String deviceNodeId,
972 Iterable<PhysAddress> macAddresses, String logicalSwitchName, IpAddress remoteVtepIp) {
973 NodeId nodeId = new NodeId(deviceNodeId);
974 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
975 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
976 // TODO: Query ARP cache to get IP address corresponding to the MAC
977 StreamSupport.stream(macAddresses.spliterator(), false)
978 .map(macAddress -> HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
979 IetfYangUtil.INSTANCE.canonizePhysAddress(macAddress).getValue(), /*ipAddress*/ null, logicalSwitchName,
981 .forEach(mac -> addRemoteUcastMac(tx, nodeId, mac));
985 * Retrieves the database version.
987 * @param broker the broker
988 * @param nodeId the node id
989 * @deprecated Use {@link #getDbVersion(TypedReadTransaction, NodeId)}.
991 * @throws ExecutionException in case of a technical (!) error while reading
992 * @throws InterruptedException if the transaction is interrupted.
995 public static String getDbVersion(DataBroker broker, NodeId nodeId) throws ExecutionException,
996 InterruptedException {
997 Node hwvtepNode = getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, nodeId);
998 String dbVersion = "";
999 if (hwvtepNode != null) {
1000 dbVersion = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();
1006 * Retrieves the database version, as indicated by the hardware VTEP node.
1008 * @param tx The transaction.
1009 * @param nodeId The node identifier.
1010 * @return The database version.
1012 public static String getDbVersion(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
1013 Node hwvtepNode = getHwVtepNode(tx, nodeId);
1014 return hwvtepNode == null ? "" : hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();