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.yang.types.rev130715.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import com.google.common.base.Optional;
39 import com.google.common.util.concurrent.ListenableFuture;
42 * Utility class to related to Hardware VTEP devices.
44 public final class HwvtepUtils {
46 // TODO: (eperefr) Move this to HwvtepSouthboundUtils when in place.
47 public static InstanceIdentifier<LocalUcastMacs> getWildCardPathForLocalUcastMacs() {
48 return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class)
49 .augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class);
53 * Adds the logical switch into config DS.
59 * @param logicalSwitch
61 * @return the listenable future
63 public static ListenableFuture<Void> addLogicalSwitch(DataBroker broker, NodeId nodeId,
64 LogicalSwitches logicalSwitch) {
65 WriteTransaction transaction = broker.newWriteOnlyTransaction();
66 putLogicalSwitch(transaction, nodeId, logicalSwitch);
67 return transaction.submit();
71 * Put the logical switches in the transaction.
80 public static void putLogicalSwitches(final WriteTransaction transaction, final NodeId nodeId,
81 final List<LogicalSwitches> lstSwitches) {
82 if (lstSwitches != null) {
83 for (LogicalSwitches logicalSwitch : lstSwitches) {
84 putLogicalSwitch(transaction, nodeId, logicalSwitch);
90 * Put logical switch in the transaction.
96 * @param logicalSwitch
99 public static void putLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
100 final LogicalSwitches logicalSwitch) {
101 InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
102 logicalSwitch.getHwvtepNodeName());
103 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, logicalSwitch, true);
107 * Delete logical switch from config DS.
113 * @param logicalSwitchName
114 * the logical switch name
115 * @return the listenable future
117 public static ListenableFuture<Void> deleteLogicalSwitch(DataBroker broker, NodeId nodeId,
118 String logicalSwitchName) {
119 WriteTransaction transaction = broker.newWriteOnlyTransaction();
120 deleteLogicalSwitch(transaction, nodeId, logicalSwitchName);
121 return transaction.submit();
125 * Delete logical switch from the transaction.
131 * @param logicalSwitchName
132 * the logical switch name
134 public static void deleteLogicalSwitch(final WriteTransaction transaction, final NodeId nodeId,
135 final String logicalSwitchName) {
136 transaction.delete(LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils
137 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
141 * Gets the logical switch.
145 * @param logicalSwitchName
146 * the logical switch name
147 * @return the logical switch
149 public static LogicalSwitches getLogicalSwitch(DataBroker broker, LogicalDatastoreType datastoreType, NodeId nodeId,
150 String logicalSwitchName) {
151 final InstanceIdentifier<LogicalSwitches> iid = HwvtepSouthboundUtils
152 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
153 Optional<LogicalSwitches> optLogicalSwitch = MDSALUtil.read(broker, datastoreType, iid);
154 if (optLogicalSwitch.isPresent()) {
155 return optLogicalSwitch.get();
161 * Get LogicalSwitches for a given hwVtepNodeId.
165 * @param hwVtepNodeId
166 * Hardware VTEP Node Id
169 * @return the logical switches
171 public static LogicalSwitches getLogicalSwitches(DataBroker broker, String hwVtepNodeId, String vni) {
172 NodeId nodeId = new NodeId(hwVtepNodeId);
173 InstanceIdentifier<LogicalSwitches> logicalSwitchesIdentifier = HwvtepSouthboundUtils
174 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(vni));
176 Optional<LogicalSwitches> logicalSwitches = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
177 logicalSwitchesIdentifier);
178 if (!logicalSwitches.isPresent()) {
182 return logicalSwitches.get();
186 * Put physical locators in the transaction.
192 * @param lstPhysicalLocator
193 * the lst physical locator
195 public static void putPhysicalLocators(WriteTransaction transaction, NodeId nodeId,
196 List<HwvtepPhysicalLocatorAugmentation> lstPhysicalLocator) {
197 if (lstPhysicalLocator != null) {
198 for (HwvtepPhysicalLocatorAugmentation phyLocator : lstPhysicalLocator) {
199 putPhysicalLocator(transaction, nodeId, phyLocator);
205 * Put physical locator in the transaction.
214 public static void putPhysicalLocator(final WriteTransaction transaction, final NodeId nodeId,
215 final HwvtepPhysicalLocatorAugmentation phyLocator) {
216 InstanceIdentifier<TerminationPoint> iid = HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId,
218 TerminationPoint terminationPoint = new TerminationPointBuilder()
219 .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocator))
220 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocator).build();
222 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, terminationPoint, true);
226 * Adds the remote ucast macs into config DS.
232 * @param lstRemoteUcastMacs
233 * the lst remote ucast macs
234 * @return the listenable future
236 public static ListenableFuture<Void> addRemoteUcastMacs(DataBroker broker, NodeId nodeId,
237 List<RemoteUcastMacs> lstRemoteUcastMacs) {
238 WriteTransaction transaction = broker.newWriteOnlyTransaction();
239 putRemoteUcastMacs(transaction, nodeId, lstRemoteUcastMacs);
240 return transaction.submit();
244 * Put remote ucast macs in the transaction.
250 * @param lstRemoteUcastMacs
251 * the lst remote ucast macs
253 public static void putRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
254 final List<RemoteUcastMacs> lstRemoteUcastMacs) {
255 if (lstRemoteUcastMacs != null && !lstRemoteUcastMacs.isEmpty()) {
256 for (RemoteUcastMacs remoteUcastMac : lstRemoteUcastMacs) {
257 putRemoteUcastMac(transaction, nodeId, remoteUcastMac);
263 * Put remote ucast mac in the transaction.
269 * @param remoteUcastMac
270 * the remote ucast mac
272 public static void putRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
273 RemoteUcastMacs remoteUcastMac) {
274 InstanceIdentifier<RemoteUcastMacs> iid = HwvtepSouthboundUtils.createInstanceIdentifier(nodeId).augmentation(HwvtepGlobalAugmentation.class)
275 .child(RemoteUcastMacs.class, new RemoteUcastMacsKey(remoteUcastMac.getLogicalSwitchRef(), remoteUcastMac.getMacEntryKey()));
276 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteUcastMac, true);
280 * Delete remote ucast mac from the config DS.
288 * @return the listenable future
290 public static ListenableFuture<Void> deleteRemoteUcastMac(DataBroker broker, NodeId nodeId,
291 String logicalSwitchName, MacAddress mac) {
292 WriteTransaction transaction = broker.newWriteOnlyTransaction();
293 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
294 return transaction.submit();
298 * Delete remote ucast macs from the config DS.
306 * @return the listenable future
308 public static ListenableFuture<Void> deleteRemoteUcastMacs(DataBroker broker, NodeId nodeId,
309 String logicalSwitchName, List<MacAddress> lstMac) {
310 WriteTransaction transaction = broker.newWriteOnlyTransaction();
311 deleteRemoteUcastMacs(transaction, nodeId, logicalSwitchName, lstMac);
312 return transaction.submit();
316 * Delete remote ucast macs from the transaction.
325 public static void deleteRemoteUcastMacs(final WriteTransaction transaction, final NodeId nodeId,
326 String logicalSwitchName, final List<MacAddress> lstMac) {
327 if (lstMac != null && !lstMac.isEmpty()) {
328 for (MacAddress mac : lstMac) {
329 deleteRemoteUcastMac(transaction, nodeId, logicalSwitchName, mac);
335 * Delete remote ucast mac from the transaction.
344 public static void deleteRemoteUcastMac(final WriteTransaction transaction, final NodeId nodeId,
345 String logialSwitchName,
346 final MacAddress mac) {
347 transaction.delete(LogicalDatastoreType.CONFIGURATION,
348 HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(nodeId, logialSwitchName, mac));
352 * Adds the remote mcast macs into config DS.
358 * @param lstRemoteMcastMacs
359 * the lst remote mcast macs
360 * @return the listenable future
362 public static ListenableFuture<Void> addRemoteMcastMacs(DataBroker broker, NodeId nodeId,
363 List<RemoteMcastMacs> lstRemoteMcastMacs) {
364 WriteTransaction transaction = broker.newWriteOnlyTransaction();
365 putRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacs);
366 return transaction.submit();
370 * Put remote mcast macs in the transaction.
376 * @param lstRemoteMcastMacs
377 * the lst remote mcast macs
379 public static void putRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
380 final List<RemoteMcastMacs> lstRemoteMcastMacs) {
381 if (lstRemoteMcastMacs != null && !lstRemoteMcastMacs.isEmpty()) {
382 for (RemoteMcastMacs remoteMcastMac : lstRemoteMcastMacs) {
383 putRemoteMcastMac(transaction, nodeId, remoteMcastMac);
389 * Put remote mcast mac in the transaction.
395 * @param remoteMcastMac
396 * the remote mcast mac
398 public static void putRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
399 RemoteMcastMacs remoteMcastMac) {
400 InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
401 remoteMcastMac.getKey());
402 transaction.put(LogicalDatastoreType.CONFIGURATION, iid, remoteMcastMac, true);
406 * Delete remote mcast mac from config DS.
412 * @param remoteMcastMacsKey
413 * the remote mcast macs key
414 * @return the listenable future
416 public static ListenableFuture<Void> deleteRemoteMcastMac(DataBroker broker, NodeId nodeId,
417 RemoteMcastMacsKey remoteMcastMacsKey) {
418 WriteTransaction transaction = broker.newWriteOnlyTransaction();
419 deleteRemoteMcastMac(transaction, nodeId, remoteMcastMacsKey);
420 return transaction.submit();
424 * Delete remote mcast macs from config DS.
430 * @param lstRemoteMcastMacsKey
431 * the lst remote mcast macs key
432 * @return the listenable future
434 public static ListenableFuture<Void> deleteRemoteMcastMacs(DataBroker broker, NodeId nodeId,
435 List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
436 WriteTransaction transaction = broker.newWriteOnlyTransaction();
437 deleteRemoteMcastMacs(transaction, nodeId, lstRemoteMcastMacsKey);
438 return transaction.submit();
442 * Delete remote mcast macs from the transaction.
448 * @param lstRemoteMcastMacsKey
449 * the lst remote mcast macs key
451 public static void deleteRemoteMcastMacs(final WriteTransaction transaction, final NodeId nodeId,
452 final List<RemoteMcastMacsKey> lstRemoteMcastMacsKey) {
453 if (lstRemoteMcastMacsKey != null && !lstRemoteMcastMacsKey.isEmpty()) {
454 for (RemoteMcastMacsKey mac : lstRemoteMcastMacsKey) {
455 deleteRemoteMcastMac(transaction, nodeId, mac);
461 * Delete remote mcast mac from the transaction.
467 * @param remoteMcastMacsKey
468 * the remote mcast macs key
470 public static void deleteRemoteMcastMac(final WriteTransaction transaction, final NodeId nodeId,
471 final RemoteMcastMacsKey remoteMcastMacsKey) {
472 transaction.delete(LogicalDatastoreType.CONFIGURATION,
473 HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId, remoteMcastMacsKey));
477 * Merge vlan bindings in the transaction.
483 * @param phySwitchName
484 * the phy switch name
487 * @param vlanBindings
490 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId nodeId,
491 final String phySwitchName, final String phyPortName, final List<VlanBindings> vlanBindings) {
492 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, phySwitchName);
493 mergeVlanBindings(transaction, physicalSwitchNodeId, phyPortName, vlanBindings);
497 * Merge vlan bindings in the transaction.
501 * @param physicalSwitchNodeId
502 * the physical switch node id
505 * @param vlanBindings
508 public static void mergeVlanBindings(final WriteTransaction transaction, final NodeId physicalSwitchNodeId,
509 final String phyPortName, final List<VlanBindings> vlanBindings) {
510 HwvtepPhysicalPortAugmentation phyPortAug = new HwvtepPhysicalPortAugmentationBuilder()
511 .setHwvtepNodeName(new HwvtepNodeName(phyPortName)).setVlanBindings(vlanBindings).build();
513 final InstanceIdentifier<HwvtepPhysicalPortAugmentation> iid = HwvtepSouthboundUtils
514 .createPhysicalPortInstanceIdentifier(physicalSwitchNodeId, phyPortName);
515 transaction.merge(LogicalDatastoreType.CONFIGURATION, iid, phyPortAug, true);
519 * Delete vlan binding from transaction.
523 * @param physicalSwitchNodeId
524 * the physical switch node id
530 public static void deleteVlanBinding(WriteTransaction transaction, NodeId physicalSwitchNodeId, String phyPortName,
532 InstanceIdentifier<VlanBindings> iid = HwvtepSouthboundUtils
533 .createVlanBindingInstanceIdentifier(physicalSwitchNodeId, phyPortName, vlanId);
534 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
538 * Gets the hw vtep node.
542 * @param datastoreType
546 * @return the hw vtep node
548 public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType, NodeId nodeId) {
549 Optional<Node> optNode = MDSALUtil.read(dataBroker, datastoreType,
550 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
551 if (optNode.isPresent()) {
552 return optNode.get();