2 * Copyright (c) 2016 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.vpnservice.utils.hwvtep;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import com.google.common.base.Optional;
42 import com.google.common.util.concurrent.ListenableFuture;
45 * Utility class to related to Hardware VTEP devices.
47 public final class HwvtepUtils {
49 // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
50 public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
51 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
52 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
56 * Adds the logical switch into config DS.
62 * @param logicalSwitch
64 * @return the listenable future
66 public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, NodeId nodeId,
67 LogicalSwitches logicalSwitch) {
68 WriteTransaction transaction = broker.newWriteOnlyTransaction();
69 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
70 return transaction.submit();
73 public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, LogicalDatastoreType logicalDatastoreType,
75 LogicalSwitches logicalSwitch) {
76 WriteTransaction transaction = broker.newWriteOnlyTransaction();
77 putLogicalSwitch(transaction,logicalDatastoreType, nodeId, logicalSwitch);
78 return transaction.submit();
81 * Put the logical switches in the transaction.
90 public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
91 final List<LogicalSwitches> lstSwitches) {
92 if (lstSwitches != null) {
93 for (LogicalSwitches logicalSwitch : lstSwitches) {
94 putLogicalSwitch(transaction,LogicalDatastoreType.CONFIGURATION, nodeId, logicalSwitch);
100 * Put logical switch in the transaction.
106 * @param logicalSwitch
109 public static void putLogicalSwitch(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
110 final NodeId nodeId, final LogicalSwitches logicalSwitch) {
111 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
112 logicalSwitch.getHwvtepNodeName());
113 transaction.put(logicalDatastoreType, iid, logicalSwitch, true);
117 * Delete logical switch from config DS.
123 * @param logicalSwitchName
124 * the logical switch name
125 * @return the listenable future
127 public static ListenableFuture<Void> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
128 String logicalSwitchName) {
129 WriteTransaction transaction = broker.newWriteOnlyTransaction();
130 deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
131 return transaction.submit();
135 * Delete logical switch from the transaction.
141 * @param logicalSwitchName
142 * the logical switch name
144 public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
145 final String logicalSwitchName) {
146 transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
147 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
151 * Gets the logical switch.
155 * @param logicalSwitchName
156 * the logical switch name
157 * @return the logical switch
159 public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
160 String logicalSwitchName) {
161 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
162 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
163 Optional<LogicalSwitches> optLogicalSwitch = MDSALUtil.read(broker, datastoreType, iid);
164 if (optLogicalSwitch.isPresent()) {
165 return optLogicalSwitch.get();
171 * Get LogicalSwitches for a given hwVtepNodeId.
175 * @param hwVtepNodeId
176 * Hardware VTEP Node Id
179 * @return the logical switches
181 public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni) {
182 NodeId nodeId = new NodeId(hwVtepNodeId);
183 InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
184 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
186 Optional<LogicalSwitches> logicalSwitches = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
187 logicalSwitchesIdentifier);
188 if (!logicalSwitches.isPresent()) {
192 return logicalSwitches.get();
196 * Put physical locators in the transaction.
202 * @param lstPhysicalLocator
203 * the lst physical locator
205 public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
206 List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
207 if (lstPhysicalLocator != null) {
208 for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
209 putPhysicalLocator(transaction, nodeId, phyLocator);
215 * Put physical locator in the transaction.
224 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
225 final HwvtepPhysicalLocatorAugmentation phyLocator) {
226 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
228 TerminationPoint terminationPoint = new TerminationPointBuilder()
229 .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
230 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
232 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
236 * Gets the physical locator.
240 * @param datastoreType
244 * @param phyLocatorIp
246 * @return the physical locator
248 public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
249 LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) {
250 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
251 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(phyLocatorIp.getValue()));
252 InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
253 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
254 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
255 Optional<HwvtepPhysicalLocatorAugmentation> optPhyLocator = MDSALUtil.read(broker, datastoreType, iid);
256 if (optPhyLocator.isPresent()) {
257 return optPhyLocator.get();
263 * Adds the remote ucast macs into config DS.
269 * @param lstRemoteUcastMacs
270 * the lst remote ucast macs
271 * @return the listenable future
273 public static ListenableFuture<Void> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
274 List<RemoteUcastMacs> lstRemoteUcastMacs) {
275 WriteTransaction transaction = broker.newWriteOnlyTransaction();
276 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
277 return transaction.submit();
281 * Put remote ucast macs in the transaction.
287 * @param lstRemoteUcastMacs
288 * the lst remote ucast macs
290 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
291 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
292 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
293 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
294 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
300 * Put remote ucast mac in the transaction.
306 * @param remoteUcastMac
307 * the remote ucast mac
309 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
310 RemoteUcastMacs remoteUcastMac) {
311 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
312 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
313 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
314 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
318 * Delete remote ucast mac from the config DS.
326 * @return the listenable future
328 public static ListenableFuture<Void> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
329 String logicalSwitchName, MacAddress mac) {
330 WriteTransaction transaction = broker.newWriteOnlyTransaction();
331 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
332 return transaction.submit();
336 * Delete remote ucast macs from the config DS.
344 * @return the listenable future
346 public static ListenableFuture<Void> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
347 String logicalSwitchName, List<MacAddress> lstMac) {
348 WriteTransaction transaction = broker.newWriteOnlyTransaction();
349 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
350 return transaction.submit();
354 * Delete remote ucast macs from the transaction.
363 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
364 String logicalSwitchName, final List<MacAddress> lstMac) {
365 if (lstMac != null && !lstMac.isEmpty()) {
366 for (MacAddress mac : lstMac) {
367 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
373 * Delete remote ucast mac from the transaction.
382 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
383 String logialSwitchName, final MacAddress mac) {
384 transaction.delete(LogicalDatastoreType.CONFIGURATION,
385 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
389 * Adds the remote mcast macs into config DS.
395 * @param lstRemoteMcastMacs
396 * the lst remote mcast macs
397 * @return the listenable future
399 public static ListenableFuture<Void> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
400 List<RemoteMcastMacs> lstRemoteMcastMacs) {
401 WriteTransaction transaction = broker.newWriteOnlyTransaction();
402 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
403 return transaction.submit();
407 * Put remote mcast macs in the transaction.
413 * @param lstRemoteMcastMacs
414 * the lst remote mcast macs
416 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
417 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
418 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
419 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
420 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
426 * Put remote mcast mac in the transaction.
432 * @param remoteMcastMac
433 * the remote mcast mac
435 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
436 RemoteMcastMacs remoteMcastMac) {
437 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
438 remoteMcastMac.getKey());
439 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
442 public static void putRemoteMcastMac(final WriteTransaction transaction,LogicalDatastoreType logicalDatastoreType,
444 RemoteMcastMacs remoteMcastMac) {
445 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
446 remoteMcastMac.getKey());
447 transaction.put(logicalDatastoreType, iid, remoteMcastMac, true);
450 * Gets the remote mcast mac.
454 * @param datastoreType
458 * @param remoteMcastMacsKey
459 * the remote mcast macs key
460 * @return the remote mcast mac
462 public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
463 NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey) {
464 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
465 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
466 Optional<RemoteMcastMacs> optRemoteMcastMac = MDSALUtil.read(broker, datastoreType, iid);
467 if (optRemoteMcastMac.isPresent()) {
468 return optRemoteMcastMac.get();
474 * Delete remote mcast mac from config DS.
480 * @param remoteMcastMacsKey
481 * the remote mcast macs key
482 * @return the listenable future
484 public static ListenableFuture<Void> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
485 RemoteMcastMacsKey remoteMcastMacsKey) {
486 WriteTransaction transaction = broker.newWriteOnlyTransaction();
487 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
488 return transaction.submit();
492 * Delete remote mcast macs from config DS.
498 * @param lstRemoteMcastMacsKey
499 * the lst remote mcast macs key
500 * @return the listenable future
502 public static ListenableFuture<Void> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
503 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
504 WriteTransaction transaction = broker.newWriteOnlyTransaction();
505 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
506 return transaction.submit();
510 * Delete remote mcast macs from the transaction.
516 * @param lstRemoteMcastMacsKey
517 * the lst remote mcast macs key
519 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
520 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
521 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
522 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
523 deleteRemoteMcastMac(transaction, nodeId, mac);
529 * Delete remote mcast mac from the transaction.
535 * @param remoteMcastMacsKey
536 * the remote mcast macs key
538 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
539 final RemoteMcastMacsKey remoteMcastMacsKey) {
540 transaction.delete(LogicalDatastoreType.CONFIGURATION,
541 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
545 * Merge vlan bindings in the transaction.
551 * @param phySwitchName
552 * the phy switch name
555 * @param vlanBindings
558 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
559 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
560 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
561 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
565 * Merge vlan bindings in the transaction.
569 * @param physicalSwitchNodeId
570 * the physical switch node id
573 * @param vlanBindings
576 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
577 final String phyPortName, final List<VlanBindings> vlanBindings) {
578 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
579 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
581 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
582 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
583 transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
587 * Delete vlan binding from transaction.
591 * @param physicalSwitchNodeId
592 * the physical switch node id
598 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
600 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
601 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
602 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
606 * Gets the hw vtep node.
610 * @param datastoreType
614 * @return the hw vtep node
616 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId) {
617 Optional<Node> optNode = MDSALUtil.read(dataBroker, datastoreType,
618 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
619 if (optNode.isPresent()) {
620 return optNode.get();
626 * Installs a list of Mac Addresses as remote Ucast address in an external
627 * device using the hwvtep-southbound.
629 * @param deviceNodeId
630 * NodeId if the ExternalDevice where the macs must be installed
632 * @param macAddresses
633 * List of Mac addresses to be installed in the external device.
634 * @param logicalSwitchName
635 * the logical switch name
636 * @param remoteVtepIp
637 * VTEP's IP in this CSS used for the tunnel with external
640 public static ListenableFuture<Void> installUcastMacs(DataBroker broker,
641 String deviceNodeId, List<PhysAddress> macAddresses,
642 String logicalSwitchName, IpAddress remoteVtepIp) {
643 NodeId nodeId = new NodeId(deviceNodeId);
644 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
645 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(remoteVtepIp.getValue()));
646 List<RemoteUcastMacs> macs = new ArrayList<RemoteUcastMacs>();
647 for (PhysAddress mac : macAddresses) {
648 // TODO: Query ARP cache to get IP address corresponding to
650 IpAddress ipAddress = null;
651 macs.add(HwvtepSouthboundUtils.createRemoteUcastMac(nodeId, mac.getValue(), ipAddress, logicalSwitchName,
654 return HwvtepUtils.addRemoteUcastMacs(broker, nodeId, macs);