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.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import com.google.common.base.Optional;
40 import com.google.common.util.concurrent.ListenableFuture;
43 * Utility class to related to Hardware VTEP devices.
45 public final class HwvtepUtils {
47 // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
48 public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
49 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
50 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
54 * Adds the logical switch into config DS.
60 * @param logicalSwitch
62 * @return the listenable future
64 public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, NodeId nodeId,
65 LogicalSwitches logicalSwitch) {
66 WriteTransaction transaction = broker.newWriteOnlyTransaction();
67 putLogicalSwitch(transaction, nodeId, logicalSwitch);
68 return transaction.submit();
72 * Put the logical switches in the transaction.
81 public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
82 final List<LogicalSwitches> lstSwitches) {
83 if (lstSwitches != null) {
84 for (LogicalSwitches logicalSwitch : lstSwitches) {
85 putLogicalSwitch(transaction, nodeId, logicalSwitch);
91 * Put logical switch in the transaction.
97 * @param logicalSwitch
100 public static void putLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
101 final LogicalSwitches logicalSwitch) {
102 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
103 logicalSwitch.getHwvtepNodeName());
104 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, logicalSwitch, true);
108 * Delete logical switch from config DS.
114 * @param logicalSwitchName
115 * the logical switch name
116 * @return the listenable future
118 public static ListenableFuture<Void> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
119 String logicalSwitchName) {
120 WriteTransaction transaction = broker.newWriteOnlyTransaction();
121 deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
122 return transaction.submit();
126 * Delete logical switch from the transaction.
132 * @param logicalSwitchName
133 * the logical switch name
135 public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
136 final String logicalSwitchName) {
137 transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
138 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
142 * Gets the logical switch.
146 * @param logicalSwitchName
147 * the logical switch name
148 * @return the logical switch
150 public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
151 String logicalSwitchName) {
152 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
153 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
154 Optional<LogicalSwitches> optLogicalSwitch = MDSALUtil.read(broker, datastoreType, iid);
155 if (optLogicalSwitch.isPresent()) {
156 return optLogicalSwitch.get();
162 * Get LogicalSwitches for a given hwVtepNodeId.
166 * @param hwVtepNodeId
167 * Hardware VTEP Node Id
170 * @return the logical switches
172 public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni) {
173 NodeId nodeId = new NodeId(hwVtepNodeId);
174 InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
175 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
177 Optional<LogicalSwitches> logicalSwitches = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
178 logicalSwitchesIdentifier);
179 if (!logicalSwitches.isPresent()) {
183 return logicalSwitches.get();
187 * Put physical locators in the transaction.
193 * @param lstPhysicalLocator
194 * the lst physical locator
196 public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
197 List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
198 if (lstPhysicalLocator != null) {
199 for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
200 putPhysicalLocator(transaction, nodeId, phyLocator);
206 * Put physical locator in the transaction.
215 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
216 final HwvtepPhysicalLocatorAugmentation phyLocator) {
217 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
219 TerminationPoint terminationPoint = new TerminationPointBuilder()
220 .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
221 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
223 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
227 * Gets the physical locator.
231 * @param datastoreType
235 * @param phyLocatorIp
237 * @return the physical locator
239 public static HwvtepPhysicalLocatorAugmentation getPhysicalLocator(DataBroker broker,
240 LogicalDatastoreType datastoreType, NodeId nodeId, final IpAddress phyLocatorIp) {
241 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
242 .createHwvtepPhysicalLocatorAugmentation(String.valueOf(phyLocatorIp.getValue()));
243 InstanceIdentifier<HwvtepPhysicalLocatorAugmentation> iid = HwvtepSouthboundUtils
244 .createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug)
245 .augmentation(HwvtepPhysicalLocatorAugmentation.class);
246 Optional<HwvtepPhysicalLocatorAugmentation> optPhyLocator = MDSALUtil.read(broker, datastoreType, iid);
247 if (optPhyLocator.isPresent()) {
248 return optPhyLocator.get();
254 * Adds the remote ucast macs into config DS.
260 * @param lstRemoteUcastMacs
261 * the lst remote ucast macs
262 * @return the listenable future
264 public static ListenableFuture<Void> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
265 List<RemoteUcastMacs> lstRemoteUcastMacs) {
266 WriteTransaction transaction = broker.newWriteOnlyTransaction();
267 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
268 return transaction.submit();
272 * Put remote ucast macs in the transaction.
278 * @param lstRemoteUcastMacs
279 * the lst remote ucast macs
281 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
282 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
283 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
284 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
285 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
291 * Put remote ucast mac in the transaction.
297 * @param remoteUcastMac
298 * the remote ucast mac
300 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
301 RemoteUcastMacs remoteUcastMac) {
302 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId)
303 .augmentation(HwvtepGlobalAugmentation.class).child(RemoteUcastMacs.class,
304 new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
305 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
309 * Delete remote ucast mac from the config DS.
317 * @return the listenable future
319 public static ListenableFuture<Void> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
320 String logicalSwitchName, MacAddress mac) {
321 WriteTransaction transaction = broker.newWriteOnlyTransaction();
322 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
323 return transaction.submit();
327 * Delete remote ucast macs from the config DS.
335 * @return the listenable future
337 public static ListenableFuture<Void> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
338 String logicalSwitchName, List<MacAddress> lstMac) {
339 WriteTransaction transaction = broker.newWriteOnlyTransaction();
340 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
341 return transaction.submit();
345 * Delete remote ucast macs from the transaction.
354 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
355 String logicalSwitchName, final List<MacAddress> lstMac) {
356 if (lstMac != null && !lstMac.isEmpty()) {
357 for (MacAddress mac : lstMac) {
358 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
364 * Delete remote ucast mac from the transaction.
373 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
374 String logialSwitchName, final MacAddress mac) {
375 transaction.delete(LogicalDatastoreType.CONFIGURATION,
376 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
380 * Adds the remote mcast macs into config DS.
386 * @param lstRemoteMcastMacs
387 * the lst remote mcast macs
388 * @return the listenable future
390 public static ListenableFuture<Void> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
391 List<RemoteMcastMacs> lstRemoteMcastMacs) {
392 WriteTransaction transaction = broker.newWriteOnlyTransaction();
393 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
394 return transaction.submit();
398 * Put remote mcast macs in the transaction.
404 * @param lstRemoteMcastMacs
405 * the lst remote mcast macs
407 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
408 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
409 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
410 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
411 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
417 * Put remote mcast mac in the transaction.
423 * @param remoteMcastMac
424 * the remote mcast mac
426 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
427 RemoteMcastMacs remoteMcastMac) {
428 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
429 remoteMcastMac.getKey());
430 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
434 * Gets the remote mcast mac.
438 * @param datastoreType
442 * @param remoteMcastMacsKey
443 * the remote mcast macs key
444 * @return the remote mcast mac
446 public static RemoteMcastMacs getRemoteMcastMac(DataBroker broker, LogicalDatastoreType datastoreType,
447 NodeId nodeId, RemoteMcastMacsKey remoteMcastMacsKey) {
448 final InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils
449 .createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey);
450 Optional<RemoteMcastMacs> optRemoteMcastMac = MDSALUtil.read(broker, datastoreType, iid);
451 if (optRemoteMcastMac.isPresent()) {
452 return optRemoteMcastMac.get();
458 * Delete remote mcast mac from config DS.
464 * @param remoteMcastMacsKey
465 * the remote mcast macs key
466 * @return the listenable future
468 public static ListenableFuture<Void> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
469 RemoteMcastMacsKey remoteMcastMacsKey) {
470 WriteTransaction transaction = broker.newWriteOnlyTransaction();
471 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
472 return transaction.submit();
476 * Delete remote mcast macs from config DS.
482 * @param lstRemoteMcastMacsKey
483 * the lst remote mcast macs key
484 * @return the listenable future
486 public static ListenableFuture<Void> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
487 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
488 WriteTransaction transaction = broker.newWriteOnlyTransaction();
489 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
490 return transaction.submit();
494 * Delete remote mcast macs from the transaction.
500 * @param lstRemoteMcastMacsKey
501 * the lst remote mcast macs key
503 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
504 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
505 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
506 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
507 deleteRemoteMcastMac(transaction, nodeId, mac);
513 * Delete remote mcast mac from the transaction.
519 * @param remoteMcastMacsKey
520 * the remote mcast macs key
522 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
523 final RemoteMcastMacsKey remoteMcastMacsKey) {
524 transaction.delete(LogicalDatastoreType.CONFIGURATION,
525 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
529 * Merge vlan bindings in the transaction.
535 * @param phySwitchName
536 * the phy switch name
539 * @param vlanBindings
542 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
543 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
544 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
545 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
549 * Merge vlan bindings in the transaction.
553 * @param physicalSwitchNodeId
554 * the physical switch node id
557 * @param vlanBindings
560 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
561 final String phyPortName, final List<VlanBindings> vlanBindings) {
562 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
563 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
565 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
566 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
567 transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
571 * Delete vlan binding from transaction.
575 * @param physicalSwitchNodeId
576 * the physical switch node id
582 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
584 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
585 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
586 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
590 * Gets the hw vtep node.
594 * @param datastoreType
598 * @return the hw vtep node
600 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId) {
601 Optional<Node> optNode = MDSALUtil.read(dataBroker, datastoreType,
602 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
603 if (optNode.isPresent()) {
604 return optNode.get();