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
8 package org.opendaylight.genius.utils.hwvtep;
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;
53 * Utility class to related to Hardware VTEP devices.
55 public final class HwvtepUtils {
56 private HwvtepUtils() {
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);
67 * Adds the logical switch into config DS.
73 * @param logicalSwitch
75 * @return the listenable future
76 * @deprecated Use {@link #addLogicalSwitch(TypedWriteTransaction, NodeId, LogicalSwitches)}.
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();
87 public static FluentFuture<? extends @NonNull CommitInfo> addLogicalSwitch(DataBroker broker,
88 LogicalDatastoreType logicalDatastoreType,
90 LogicalSwitches logicalSwitch) {
91 WriteTransaction transaction = broker.newWriteOnlyTransaction();
92 putLogicalSwitch(transaction,logicalDatastoreType, nodeId, logicalSwitch);
93 return transaction.commit();
97 * Adds the logical switch.
99 * @param tx The configuration transaction.
100 * @param nodeId The node identifier.
101 * @param logicalSwitch The logical switch.
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);
111 * Put the logical switches in the transaction.
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);
130 * Put logical switch in the transaction.
134 * @param logicalDatastoreType the LogicalDatastoreType
137 * @param logicalSwitch
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);
148 * Delete logical switch from config DS.
154 * @param logicalSwitchName
155 * the logical switch name
156 * @return the listenable future
157 * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
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();
168 * Delete logical switch from the transaction.
174 * @param logicalSwitchName
175 * the logical switch name
176 * @deprecated Use {@link #deleteLogicalSwitch(TypedWriteTransaction, NodeId, String)}.
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)));
186 * Deletes the given logical switch.
188 * @param tx The transaction.
189 * @param nodeId The node identifier.
190 * @param logicalSwitchName The logical switch name.
192 public static void deleteLogicalSwitch(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
193 String logicalSwitchName) {
194 tx.delete(HwvtepSouthboundUtils
195 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
199 * Gets the logical switch.
201 * @param broker the broker
202 * @param datastoreType the datastore type
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.
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);
221 * Retrieves the logical switch.
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.
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));
234 return tx.read(iid).get().orElse(null);
235 } catch (InterruptedException | ExecutionException e) {
236 throw new RuntimeException("Error reading logical switch " + iid, e);
241 * Gets physical port termination point.
245 * @param datastoreType
248 * the physical switch node id
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.
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);
264 * Get LogicalSwitches for a given hwVtepNodeId.
268 * @param hwVtepNodeId
269 * Hardware VTEP Node 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.
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));
282 return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION,
283 logicalSwitchesIdentifier).orElse(null);
287 * Put physical locators in the transaction.
293 * @param lstPhysicalLocator
294 * the lst physical locator
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);
306 * Put physical locator in the transaction.
315 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
316 final HwvtepPhysicalLocatorAugmentation phyLocator) {
317 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
319 transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, new TerminationPointBuilder()
320 .withKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
321 .addAugmentation(phyLocator)
326 * Gets the physical locator.
330 * @param datastoreType
334 * @param phyLocatorIp
336 * @return the physical locator
337 * @throws ExecutionException in case of a technical (!) error while reading
338 * @throws InterruptedException if the transaction is interrupted.
340 public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
341 LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) throws
342 ExecutionException, InterruptedException {
343 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
344 .createHwvtepPhysicalLocatorAugmentation(phyLocatorIp);
345 InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
346 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
347 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
348 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
352 * Adds the remote ucast macs into config DS.
358 * @param lstRemoteUcastMacs
359 * the lst remote ucast macs
360 * @return the listenable future
361 * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
364 public static FluentFuture<? extends @NonNull CommitInfo> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
365 List<RemoteUcastMacs> lstRemoteUcastMacs) {
366 WriteTransaction transaction = broker.newWriteOnlyTransaction();
367 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
368 return transaction.commit();
372 * Adds the given remote unicast MACs.
374 * @param tx The transaction to use.
375 * @param nodeId The node identifier.
376 * @param remoteUcastMacs The MACs to add.
378 public static void addRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
379 Iterable<RemoteUcastMacs> remoteUcastMacs) {
380 if (remoteUcastMacs != null) {
381 remoteUcastMacs.forEach(remoteUcastMac -> addRemoteUcastMac(tx, nodeId, remoteUcastMac));
386 * Adds the given remote unicast MAC.
388 * @param tx The transaction to use.
389 * @param nodeId The node identifier.
390 * @param remoteUcastMac The MAC to add.
392 public static void addRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
393 RemoteUcastMacs remoteUcastMac) {
394 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
395 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
396 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
397 tx.mergeParentStructurePut(iid, remoteUcastMac);
401 * Put remote ucast macs in the transaction.
407 * @param lstRemoteUcastMacs
408 * the lst remote ucast macs
409 * @deprecated Use {@link #addRemoteUcastMacs(TypedWriteTransaction, NodeId, Iterable)}.
412 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
413 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
414 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
415 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
416 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
422 * Put remote ucast mac in the transaction.
428 * @param remoteUcastMac
429 * the remote ucast mac
430 * @deprecated Use {@link #addRemoteUcastMac(TypedWriteTransaction, NodeId, RemoteUcastMacs)}.
433 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
434 RemoteUcastMacs remoteUcastMac) {
435 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
436 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
437 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
438 transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac);
442 * Delete remote ucast mac from the config DS.
448 * @param logicalSwitchName the logical switch name
451 * @return the listenable future
452 * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
455 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
456 String logicalSwitchName, MacAddress mac) {
457 WriteTransaction transaction = broker.newWriteOnlyTransaction();
458 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
459 return transaction.commit();
463 * Delete remote ucast mac from the transaction.
469 * @param logialSwitchName the logical switch name
472 * @deprecated Use {@link #deleteRemoteUcastMac(TypedWriteTransaction, NodeId, String, MacAddress)}.
475 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
476 String logialSwitchName, final MacAddress mac) {
477 transaction.delete(LogicalDatastoreType.CONFIGURATION,
478 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
482 * Deletes the given remote unicast MAC.
484 * @param tx The transaction to use.
485 * @param nodeId The node identifier.
486 * @param logicalSwitchName The logical switch name.
487 * @param macAddress The MAC.
489 public static void deleteRemoteUcastMac(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
490 String logicalSwitchName, MacAddress macAddress) {
491 tx.delete(HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logicalSwitchName, macAddress));
495 * Delete remote ucast macs from the config DS.
501 * @param logicalSwitchName the logical switch name
504 * @return the listenable future
505 * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
508 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
509 String logicalSwitchName, List<MacAddress> lstMac) {
510 WriteTransaction transaction = broker.newWriteOnlyTransaction();
511 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
512 return transaction.commit();
516 * Delete remote ucast macs from the transaction.
522 * @param logicalSwitchName the logical switch name
525 * @deprecated Use {@link #deleteRemoteUcastMacs(TypedWriteTransaction, NodeId, String, Iterable)}.
528 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
529 String logicalSwitchName, final List<MacAddress> lstMac) {
530 if (lstMac != null && !lstMac.isEmpty()) {
531 for (MacAddress mac : lstMac) {
532 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
538 * Deletes the given remote unicast MACs.
540 * @param tx The transaction to use.
541 * @param nodeId The node identifier.
542 * @param logicalSwitchName The logical switch name.
543 * @param macAddresses The MAC addresses.
545 public static void deleteRemoteUcastMacs(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
546 String logicalSwitchName, Iterable<MacAddress> macAddresses) {
547 if (macAddresses != null) {
548 macAddresses.forEach(macAddress -> deleteRemoteUcastMac(tx, nodeId, logicalSwitchName, macAddress));
553 * Adds the remote mcast macs into config DS.
559 * @param lstRemoteMcastMacs
560 * the lst remote mcast macs
561 * @return the listenable future
563 public static FluentFuture<? extends @NonNull CommitInfo> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
564 List<RemoteMcastMacs> lstRemoteMcastMacs) {
565 WriteTransaction transaction = broker.newWriteOnlyTransaction();
566 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
567 return transaction.commit();
571 * Put remote mcast macs in the transaction.
577 * @param lstRemoteMcastMacs
578 * the lst remote mcast macs
580 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
581 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
582 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
583 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
584 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
590 * Put remote mcast mac in the transaction.
596 * @param remoteMcastMac
597 * the remote mcast mac
598 * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
601 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
602 RemoteMcastMacs remoteMcastMac) {
603 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
604 remoteMcastMac.key());
605 transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac);
609 * Adds a remote multicast MAC.
611 * @param transaction the transaction
612 * @param logicalDatastoreType the LogicalDatastoreType
613 * @param nodeId the node id
614 * @param remoteMcastMac the remote mcast mac
615 * @deprecated Use {@link #addRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacs)}.
618 public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
620 RemoteMcastMacs remoteMcastMac) {
621 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
622 remoteMcastMac.key());
623 transaction.mergeParentStructurePut(logicalDatastoreType, iid, remoteMcastMac);
627 * Store a remote multicast MAC.
629 * @param tx The transaction.
630 * @param nodeId The node identifier.
631 * @param remoteMcastMac The remote multicast MAC.
633 public static void addRemoteMcastMac(final TypedWriteTransaction<? extends Datastore> tx, final NodeId nodeId,
634 RemoteMcastMacs remoteMcastMac) {
635 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
636 remoteMcastMac.key());
637 tx.mergeParentStructurePut(iid, remoteMcastMac);
641 * Gets the remote mcast mac.
645 * @param datastoreType
649 * @param remoteMcastMacsKey
650 * the remote mcast macs key
651 * @return the remote mcast mac
652 * @deprecated Use {@link #getRemoteMcastMac(TypedReadTransaction, NodeId, RemoteMcastMacsKey)}.
653 * @throws ExecutionException in case of a technical (!) error while reading
654 * @throws InterruptedException if the transaction is interrupted.
657 public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
658 NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey)
659 throws ExecutionException, InterruptedException {
660 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
661 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
662 return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, iid).orElse(null);
666 * Retrieve a remote multicast MAC.
668 * @param tx The transction to use.
669 * @param nodeId The node identifier.
670 * @param remoteMcastMacsKey The MAC key.
671 * @return The MAC, if any ({@code null} if there is none).
674 public static RemoteMcastMacs getRemoteMcastMac(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId,
675 RemoteMcastMacsKey remoteMcastMacsKey) {
676 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
677 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
679 return tx.read(iid).get().orElse(null);
680 } catch (InterruptedException | ExecutionException e) {
681 throw new RuntimeException("Error reading remote multicast MAC " + iid, e);
686 * Delete remote mcast mac from config DS.
692 * @param remoteMcastMacsKey
693 * the remote mcast macs key
694 * @return the listenable future
695 * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
698 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
699 RemoteMcastMacsKey remoteMcastMacsKey) {
700 WriteTransaction transaction = broker.newWriteOnlyTransaction();
701 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
702 return transaction.commit();
706 * Delete remote mcast mac from the transaction.
712 * @param remoteMcastMacsKey
713 * the remote mcast macs key
714 * @deprecated Use {@link #deleteRemoteMcastMac(TypedWriteTransaction, NodeId, RemoteMcastMacsKey)}.
717 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
718 final RemoteMcastMacsKey remoteMcastMacsKey) {
719 transaction.delete(LogicalDatastoreType.CONFIGURATION,
720 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
724 * Deletes the given remote multicast MAC.
726 * @param tx The configuration transaction.
727 * @param nodeId The node identifier.
728 * @param remoteMcastMacsKey The remote multicast MAC key.
730 public static void deleteRemoteMcastMac(TypedWriteTransaction<Configuration> tx, final NodeId nodeId,
731 final RemoteMcastMacsKey remoteMcastMacsKey) {
732 tx.delete(HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
736 * Delete remote mcast macs from config DS.
742 * @param lstRemoteMcastMacsKey
743 * the lst remote mcast macs key
744 * @return the listenable future
746 public static FluentFuture<? extends @NonNull CommitInfo> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
747 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
748 WriteTransaction transaction = broker.newWriteOnlyTransaction();
749 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
750 return transaction.commit();
754 * Delete remote mcast macs from the transaction.
760 * @param lstRemoteMcastMacsKey
761 * the lst remote mcast macs key
763 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
764 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
765 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
766 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
767 deleteRemoteMcastMac(transaction, nodeId, mac);
773 * Merge vlan bindings in the transaction.
779 * @param phySwitchName
780 * the phy switch name
783 * @param vlanBindings
785 * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, String, List)}.
788 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
789 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
790 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
791 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
795 * Merges the given VLAN bindings.
797 * @param tx The transaction to use.
798 * @param nodeId The node identifier.
799 * @param phySwitchName The physical switch name.
800 * @param phyPortName The physical port name.
801 * @param vlanBindings The VLAN bindings.
803 public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId nodeId,
804 String phySwitchName, String phyPortName, List<VlanBindings> vlanBindings) {
805 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
806 mergeVlanBindings(tx, physicalSwitchNodeId, phyPortName, vlanBindings);
810 * Merge vlan bindings in the transaction.
814 * @param physicalSwitchNodeId
815 * the physical switch node id
818 * @param vlanBindings
820 * @deprecated Use {@link #mergeVlanBindings(TypedWriteTransaction, NodeId, String, List)}.
823 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
824 final String phyPortName, final List<VlanBindings> vlanBindings) {
825 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
826 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
828 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
829 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
830 transaction.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug);
834 * Merges the given VLAN bindings.
836 * @param tx The transaction to use.
837 * @param physicalSwitchNodeId The physical switch’s node identifier.
838 * @param phyPortName The physical port name.
839 * @param vlanBindings The VLAN bindings.
841 public static void mergeVlanBindings(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
842 String phyPortName, List<VlanBindings> vlanBindings) {
843 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
844 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
846 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
847 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
848 tx.mergeParentStructureMerge(iid, phyPortAug);
852 * Delete vlan binding from transaction.
856 * @param physicalSwitchNodeId
857 * the physical switch node id
862 * @deprecated Use {@link #deleteVlanBinding(TypedWriteTransaction, NodeId, String, Integer)}.
865 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
867 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
868 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
869 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
873 * Deletes the given VLAN binding.
875 * @param tx The transaction to use.
876 * @param physicalSwitchNodeId The physical switch’s node identifier.
877 * @param phyPortName The physical port name.
878 * @param vlanId The VLAN identifier.
880 public static void deleteVlanBinding(TypedWriteTransaction<Configuration> tx, NodeId physicalSwitchNodeId,
881 String phyPortName, Integer vlanId) {
882 tx.delete(HwvtepSouthboundUtils.createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId));
886 * Gets the hw vtep node.
890 * @param datastoreType
894 * @return the hw vtep node
895 * @deprecated Use {@link #getHwVtepNode(TypedReadTransaction, NodeId)}.
896 * @throws ExecutionException in case of a technical (!) error while reading
897 * @throws InterruptedException if the transaction is interrupted.
900 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId)
901 throws ExecutionException, InterruptedException {
902 return SingleTransactionDataBroker.syncReadOptional(dataBroker, datastoreType,
903 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).orElse(null);
907 * Retrieves the hardware VTEP node.
909 * @param tx The transaction.
910 * @param nodeId The node identifier.
911 * @return The hardware VTEP node.
913 public static Node getHwVtepNode(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
915 return tx.read(HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).get().orElse(null);
916 } catch (InterruptedException | ExecutionException e) {
917 throw new RuntimeException("Failed to read hwvtep node", e);
922 * Installs a list of Mac Addresses as remote Ucast address in an external
923 * device using the hwvtep-southbound.
925 * @param broker the databroker
926 * @param deviceNodeId
927 * NodeId if the ExternalDevice where the macs must be installed
929 * @param macAddresses
930 * List of Mac addresses to be installed in the external device.
931 * @param logicalSwitchName
932 * the logical switch name
933 * @param remoteVtepIp
934 * VTEP's IP in this OVS used for the tunnel with external
936 * @deprecated Use {@link #addUcastMacs(TypedWriteTransaction, String, Iterable, String, IpAddress)}.
937 * @return future if present
940 public static FluentFuture<? extends @NonNull CommitInfo> installUcastMacs(DataBroker broker,
941 String deviceNodeId, List<PhysAddress> macAddresses,
942 String logicalSwitchName, IpAddress remoteVtepIp) {
943 NodeId nodeId = new NodeId(deviceNodeId);
944 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
945 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
946 List<RemoteUcastMacs> macs = new ArrayList<>();
947 for (PhysAddress mac : macAddresses) {
948 // TODO: Query ARP cache to get IP address corresponding to
950 //IpAddress ipAddress = null;
951 macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
952 IetfYangUtil.INSTANCE.canonizePhysAddress(mac).getValue(), /*ipAddress*/ null, logicalSwitchName,
955 return addRemoteUcastMacs(broker, nodeId, macs);
961 * @param tx The transaction to use.
962 * @param deviceNodeId The device’s node identifier.
963 * @param macAddresses The MAC addresses.
964 * @param logicalSwitchName The logical switch name.
965 * @param remoteVtepIp The remote VTEP IP address.
967 public static void addUcastMacs(TypedWriteTransaction<Configuration> tx, String deviceNodeId,
968 Iterable<PhysAddress> macAddresses, String logicalSwitchName, IpAddress remoteVtepIp) {
969 NodeId nodeId = new NodeId(deviceNodeId);
970 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
971 .createHwvtepPhysicalLocatorAugmentation(remoteVtepIp);
972 // TODO: Query ARP cache to get IP address corresponding to the MAC
973 StreamSupport.stream(macAddresses.spliterator(), false)
974 .map(macAddress -> HwvtepSouthboundUtils.createRemoteUcastMac(nodeId,
975 IetfYangUtil.INSTANCE.canonizePhysAddress(macAddress).getValue(), /*ipAddress*/ null, logicalSwitchName,
977 .forEach(mac -> addRemoteUcastMac(tx, nodeId, mac));
981 * Retrieves the database version.
983 * @param broker the broker
984 * @param nodeId the node id
985 * @deprecated Use {@link #getDbVersion(TypedReadTransaction, NodeId)}.
987 * @throws ExecutionException in case of a technical (!) error while reading
988 * @throws InterruptedException if the transaction is interrupted.
991 public static String getDbVersion(DataBroker broker, NodeId nodeId) throws ExecutionException,
992 InterruptedException {
993 Node hwvtepNode = getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, nodeId);
994 String dbVersion = "";
995 if (hwvtepNode != null) {
996 dbVersion = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();
1002 * Retrieves the database version, as indicated by the hardware VTEP node.
1004 * @param tx The transaction.
1005 * @param nodeId The node identifier.
1006 * @return The database version.
1008 public static String getDbVersion(TypedReadTransaction<? extends Datastore> tx, NodeId nodeId) {
1009 Node hwvtepNode = getHwVtepNode(tx, nodeId);
1010 return hwvtepNode == null ? "" : hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getDbVersion();