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 com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Locale;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.mdsalutil.MDSALUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 * Utility class to related to Hardware VTEP devices.
47 public final class HwvtepUtils {
49 private HwvtepUtils() { }
51 // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
52 public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
53 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
54 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
58 * Adds the logical switch into config DS.
64 * @param logicalSwitch
66 * @return the listenable future
68 public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, NodeId nodeId,
69 LogicalSwitches logicalSwitch) {
70 WriteTransaction transaction = broker.newWriteOnlyTransaction();
71 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
72 return transaction.submit();
75 public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, LogicalDatastoreType logicalDatastoreType,
77 LogicalSwitches logicalSwitch) {
78 WriteTransaction transaction = broker.newWriteOnlyTransaction();
79 putLogicalSwitch(transaction,logicalDatastoreType, nodeId, logicalSwitch);
80 return transaction.submit();
84 * Put the logical switches in the transaction.
93 public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
94 final List<LogicalSwitches> lstSwitches) {
95 if (lstSwitches != null) {
96 for (LogicalSwitches logicalSwitch : lstSwitches) {
97 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
103 * Put logical switch in the transaction.
109 * @param logicalSwitch
112 public static void putLogicalSwitch(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
113 final NodeId nodeId, final LogicalSwitches logicalSwitch) {
114 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
115 logicalSwitch.getHwvtepNodeName());
116 transaction.put(logicalDatastoreType, iid, logicalSwitch, true);
120 * Delete logical switch from config DS.
126 * @param logicalSwitchName
127 * the logical switch name
128 * @return the listenable future
130 public static ListenableFuture<Void> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
131 String logicalSwitchName) {
132 WriteTransaction transaction = broker.newWriteOnlyTransaction();
133 deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
134 return transaction.submit();
138 * Delete logical switch from the transaction.
144 * @param logicalSwitchName
145 * the logical switch name
147 public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
148 final String logicalSwitchName) {
149 transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
150 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
154 * Gets the logical switch.
158 * @param logicalSwitchName
159 * the logical switch name
160 * @return the logical switch
162 public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
163 String logicalSwitchName) {
164 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
165 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
166 return MDSALUtil.read(broker, datastoreType, iid).orNull();
170 * Gets physical port termination point.
174 * @param datastoreType
177 * the physical switch node id
179 * port name under physical switch node id
180 * @return the physical port termination point
182 public static TerminationPoint getPhysicalPortTerminationPoint(DataBroker broker,
183 LogicalDatastoreType datastoreType, NodeId nodeId, String portName) {
184 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
185 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createTerminationPointId(nodeId, tpKey);
186 return MDSALUtil.read(broker, datastoreType, iid).orNull();
190 * Get LogicalSwitches for a given hwVtepNodeId.
194 * @param hwVtepNodeId
195 * Hardware VTEP Node Id
198 * @return the logical switches
200 public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni) {
201 NodeId nodeId = new NodeId(hwVtepNodeId);
202 InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
203 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
205 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, logicalSwitchesIdentifier).orNull();
209 * Put physical locators in the transaction.
215 * @param lstPhysicalLocator
216 * the lst physical locator
218 public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
219 List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
220 if (lstPhysicalLocator != null) {
221 for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
222 putPhysicalLocator(transaction, nodeId, phyLocator);
228 * Put physical locator in the transaction.
237 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
238 final HwvtepPhysicalLocatorAugmentation phyLocator) {
239 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
241 TerminationPoint terminationPoint = new TerminationPointBuilder()
242 .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
243 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
245 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
249 * Gets the physical locator.
253 * @param datastoreType
257 * @param phyLocatorIp
259 * @return the physical locator
261 public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
262 LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) {
263 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
264 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(phyLocatorIp.getValue()));
265 InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
266 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
267 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
268 return MDSALUtil.read(broker, datastoreType, iid).orNull();
272 * Adds the remote ucast macs into config DS.
278 * @param lstRemoteUcastMacs
279 * the lst remote ucast macs
280 * @return the listenable future
282 public static ListenableFuture<Void> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
283 List<RemoteUcastMacs> lstRemoteUcastMacs) {
284 WriteTransaction transaction = broker.newWriteOnlyTransaction();
285 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
286 return transaction.submit();
290 * Put remote ucast macs in the transaction.
296 * @param lstRemoteUcastMacs
297 * the lst remote ucast macs
299 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
300 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
301 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
302 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
303 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
309 * Put remote ucast mac in the transaction.
315 * @param remoteUcastMac
316 * the remote ucast mac
318 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
319 RemoteUcastMacs remoteUcastMac) {
320 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
321 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
322 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
323 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
327 * Delete remote ucast mac from the config DS.
335 * @return the listenable future
337 public static ListenableFuture<Void> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
338 String logicalSwitchName, MacAddress mac) {
339 WriteTransaction transaction = broker.newWriteOnlyTransaction();
340 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
341 return transaction.submit();
345 * Delete remote ucast mac from the transaction.
354 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
355 String logialSwitchName, final MacAddress mac) {
356 transaction.delete(LogicalDatastoreType.CONFIGURATION,
357 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
361 * Delete remote ucast macs from the config DS.
369 * @return the listenable future
371 public static ListenableFuture<Void> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
372 String logicalSwitchName, List<MacAddress> lstMac) {
373 WriteTransaction transaction = broker.newWriteOnlyTransaction();
374 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
375 return transaction.submit();
379 * Delete remote ucast macs from the transaction.
388 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
389 String logicalSwitchName, final List<MacAddress> lstMac) {
390 if (lstMac != null && !lstMac.isEmpty()) {
391 for (MacAddress mac : lstMac) {
392 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
398 * Adds the remote mcast macs into config DS.
404 * @param lstRemoteMcastMacs
405 * the lst remote mcast macs
406 * @return the listenable future
408 public static ListenableFuture<Void> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
409 List<RemoteMcastMacs> lstRemoteMcastMacs) {
410 WriteTransaction transaction = broker.newWriteOnlyTransaction();
411 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
412 return transaction.submit();
416 * Put remote mcast macs in the transaction.
422 * @param lstRemoteMcastMacs
423 * the lst remote mcast macs
425 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
426 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
427 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
428 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
429 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
435 * Put remote mcast mac in the transaction.
441 * @param remoteMcastMac
442 * the remote mcast mac
444 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
445 RemoteMcastMacs remoteMcastMac) {
446 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
447 remoteMcastMac.getKey());
448 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
451 public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
453 RemoteMcastMacs remoteMcastMac) {
454 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
455 remoteMcastMac.getKey());
456 transaction.put(logicalDatastoreType, iid, remoteMcastMac, true);
460 * Gets the remote mcast mac.
464 * @param datastoreType
468 * @param remoteMcastMacsKey
469 * the remote mcast macs key
470 * @return the remote mcast mac
472 public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
473 NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey) {
474 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
475 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
476 return MDSALUtil.read(broker, datastoreType, iid).orNull();
480 * Delete remote mcast mac from config DS.
486 * @param remoteMcastMacsKey
487 * the remote mcast macs key
488 * @return the listenable future
490 public static ListenableFuture<Void> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
491 RemoteMcastMacsKey remoteMcastMacsKey) {
492 WriteTransaction transaction = broker.newWriteOnlyTransaction();
493 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
494 return transaction.submit();
498 * Delete remote mcast mac from the transaction.
504 * @param remoteMcastMacsKey
505 * the remote mcast macs key
507 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
508 final RemoteMcastMacsKey remoteMcastMacsKey) {
509 transaction.delete(LogicalDatastoreType.CONFIGURATION,
510 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
514 * Delete remote mcast macs from config DS.
520 * @param lstRemoteMcastMacsKey
521 * the lst remote mcast macs key
522 * @return the listenable future
524 public static ListenableFuture<Void> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
525 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
526 WriteTransaction transaction = broker.newWriteOnlyTransaction();
527 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
528 return transaction.submit();
532 * Delete remote mcast macs from the transaction.
538 * @param lstRemoteMcastMacsKey
539 * the lst remote mcast macs key
541 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
542 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
543 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
544 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
545 deleteRemoteMcastMac(transaction, nodeId, mac);
551 * Merge vlan bindings in the transaction.
557 * @param phySwitchName
558 * the phy switch name
561 * @param vlanBindings
564 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
565 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
566 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
567 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
571 * Merge vlan bindings in the transaction.
575 * @param physicalSwitchNodeId
576 * the physical switch node id
579 * @param vlanBindings
582 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
583 final String phyPortName, final List<VlanBindings> vlanBindings) {
584 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
585 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
587 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
588 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
589 transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
593 * Delete vlan binding from transaction.
597 * @param physicalSwitchNodeId
598 * the physical switch node id
604 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
606 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
607 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
608 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
612 * Gets the hw vtep node.
616 * @param datastoreType
620 * @return the hw vtep node
622 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId) {
623 return MDSALUtil.read(dataBroker, datastoreType,
624 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)).orNull();
628 * Installs a list of Mac Addresses as remote Ucast address in an external
629 * device using the hwvtep-southbound.
631 * @param deviceNodeId
632 * NodeId if the ExternalDevice where the macs must be installed
634 * @param macAddresses
635 * List of Mac addresses to be installed in the external device.
636 * @param logicalSwitchName
637 * the logical switch name
638 * @param remoteVtepIp
639 * VTEP's IP in this OVS used for the tunnel with external
642 public static ListenableFuture<Void> installUcastMacs(DataBroker broker,
643 String deviceNodeId, List<PhysAddress> macAddresses,
644 String logicalSwitchName, IpAddress remoteVtepIp) {
645 NodeId nodeId = new NodeId(deviceNodeId);
646 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
647 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(remoteVtepIp.getValue()));
648 List<RemoteUcastMacs> macs = new ArrayList<>();
649 for (PhysAddress mac : macAddresses) {
650 // TODO: Query ARP cache to get IP address corresponding to
652 //IpAddress ipAddress = null;
653 macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId, mac.getValue().toLowerCase(Locale.getDefault()),
654 /*ipAddress*/ null, logicalSwitchName, phyLocatorAug));
656 return HwvtepUtils.addRemoteUcastMacs(broker, nodeId, macs);
659 public static String getDbVersion(DataBroker broker, NodeId nodeId) {
660 Node hwvtepNode = getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, nodeId);
661 String dbVersion = "";
662 if (hwvtepNode != null) {
663 dbVersion = hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getDbVersion();