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
8 package org.opendaylight.netvirt.elan.utils;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
21 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
22 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class ElanForwardingEntriesHandler {
36 private static final Logger LOG = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
38 private final DataBroker broker;
39 private final ManagedNewTransactionRunner txRunner;
40 private final ElanUtils elanUtils;
43 public ElanForwardingEntriesHandler(DataBroker dataBroker, ElanUtils elanUtils) {
44 this.broker = dataBroker;
45 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
46 this.elanUtils = elanUtils;
49 public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName,
50 String existingInterfaceName, MacEntry mac, WriteTransaction tx) {
51 if (existingInterfaceName.equals(interfaceName)) {
52 LOG.error("Static MAC address {} has already been added for the same ElanInstance "
53 + "{} on the same Logical Interface Port {}."
54 + " No operation will be done.",
55 mac.getMacAddress().toString(), elanInstanceName, interfaceName);
57 LOG.warn("Static MAC address {} had already been added for ElanInstance {} on Logical Interface Port {}. "
58 + "This would be considered as MAC movement scenario and old static mac will be removed "
59 + "and new static MAC will be added"
60 + "for ElanInstance {} on Logical Interface Port {}",
61 mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName);
62 //Update the ElanInterface Forwarding Container & ElanForwarding Container
63 deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac, tx);
64 createElanInterfaceForwardingTablesList(interfaceName, mac, tx);
65 updateElanForwardingTablesList(elanInstanceName, interfaceName, mac, tx);
70 public void addElanInterfaceForwardingTableList(String elanInstanceName, String interfaceName,
71 StaticMacEntries staticMacEntries, WriteTransaction tx) {
72 MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true)
73 .setMacAddress(staticMacEntries.getMacAddress())
74 .setIpPrefix(staticMacEntries.getIpPrefix())
75 .setInterface(interfaceName).setKey(new MacEntryKey(staticMacEntries.getMacAddress())).build();
77 createElanForwardingTablesList(elanInstanceName, macEntry, tx);
78 createElanInterfaceForwardingTablesList(interfaceName, macEntry, tx);
81 public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
82 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
83 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
84 MacEntry existingInterfaceMacEntry = elanUtils
85 .getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
86 if (existingInterfaceMacEntry != null) {
87 tx.delete(LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
91 public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac, WriteTransaction tx) {
92 InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils
93 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
94 MacEntry existingInterfaceMacEntry = elanUtils
95 .getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
96 if (existingInterfaceMacEntry == null) {
97 MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
98 .setInterface(interfaceName)
99 .setIsStaticAddress(true).setKey(new MacEntryKey(mac.getMacAddress())).build();
100 tx.put(LogicalDatastoreType.OPERATIONAL, existingMacEntryId, macEntry,
101 WriteTransaction.CREATE_MISSING_PARENTS);
105 public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac,
106 WriteTransaction tx) {
107 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
108 mac.getMacAddress());
109 MacEntry existingMacEntry = elanUtils.getMacEntryFromElanMacId(macEntryId);
110 if (existingMacEntry != null && elanUtils.getElanMacTable(elanName) != null) {
111 MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true)
112 .setMacAddress(mac.getMacAddress()).setIpPrefix(mac.getIpPrefix())
113 .setKey(new MacEntryKey(mac.getMacAddress())).build();
114 tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry);
118 private void createElanForwardingTablesList(String elanName, MacEntry macEntry, WriteTransaction tx) {
119 InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName,
120 macEntry.getMacAddress());
121 Optional<MacEntry> existingMacEntry = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
122 if (!existingMacEntry.isPresent() && elanUtils.getElanMacTable(elanName) != null) {
123 tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry, WriteTransaction.CREATE_MISSING_PARENTS);
127 public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
129 List<ListenableFuture<Void>> futures = new ArrayList<>();
130 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(interfaceTx -> {
131 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
132 .getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
133 interfaceTx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
134 deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry, interfaceTx);
135 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(flowTx -> {
136 elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, flowTx);
139 for (ListenableFuture<Void> future : futures) {
140 ListenableFutures.addErrorLogging(future, LOG, "Error deleting ELAN interface forwarding entries");
144 public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress,
145 WriteTransaction tx) {
146 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
147 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
148 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);