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.internal;
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
11 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
25 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
26 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
29 import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.util.Datastore.Configuration;
32 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
33 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
34 import org.opendaylight.mdsal.binding.util.TypedReadWriteTransaction;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
37 import org.opendaylight.netvirt.elan.utils.ElanConstants;
38 import org.opendaylight.netvirt.elan.utils.ElanUtils;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53 import org.opendaylight.yangtools.yang.common.Uint64;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 public class ElanSmacFlowEventListener implements SalFlowListener {
60 private static final Logger LOG = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
62 private final DataBroker broker;
63 private final ManagedNewTransactionRunner txRunner;
64 private final IInterfaceManager interfaceManager;
65 private final ElanUtils elanUtils;
66 private final JobCoordinator jobCoordinator;
67 private final ElanInstanceCache elanInstanceCache;
70 public ElanSmacFlowEventListener(DataBroker broker, IInterfaceManager interfaceManager, ElanUtils elanUtils,
71 JobCoordinator jobCoordinator, ElanInstanceCache elanInstanceCache) {
73 this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
74 this.interfaceManager = interfaceManager;
75 this.elanUtils = elanUtils;
76 this.jobCoordinator = jobCoordinator;
77 this.elanInstanceCache = elanInstanceCache;
81 public void onFlowAdded(FlowAdded arg0) {
82 // TODO Auto-generated method stub
87 public void onFlowRemoved(FlowRemoved flowRemoved) {
88 short tableId = flowRemoved.getTableId().toJava();
89 if (tableId == NwConstants.ELAN_SMAC_TABLE) {
90 Uint64 metadata = flowRemoved.getMatch().getMetadata().getMetadata();
91 Uint32 elanTag = Uint32.valueOf(MetaDataUtil.getElanTagFromMetadata(metadata));
92 ElanTagName elanTagInfo = elanUtils.getElanInfoByElanTag(elanTag);
93 if (elanTagInfo == null) {
96 final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
97 .getEthernetSource().getAddress().getValue().toUpperCase(Locale.getDefault());
98 Uint64 portTag = MetaDataUtil.getLportFromMetadata(metadata);
99 if (portTag.intValue() == 0) {
100 LOG.debug("Flow removed event on SMAC flow entry. But having port Tag as 0 ");
103 Optional<IfIndexInterface> existingInterfaceInfo = elanUtils
104 .getInterfaceInfoByInterfaceTag(Uint32.valueOf(portTag.longValue()));
105 if (!existingInterfaceInfo.isPresent()) {
106 LOG.debug("Interface is not available for port Tag {}", portTag);
109 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
110 PhysAddress physAddress = new PhysAddress(srcMacAddress);
111 if (interfaceName == null) {
112 LOG.error("LPort record not found for tag {}", portTag);
115 jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(interfaceName), () -> {
116 List<ListenableFuture<?>> elanFutures = new ArrayList<>();
117 MacEntry macEntry = elanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
118 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
119 String elanInstanceName = elanTagInfo.getName();
120 LOG.info("Deleting the Mac-Entry:{} present on ElanInstance:{}", macEntry, elanInstanceName);
121 FluentFuture<? extends Object> result =
122 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
123 if (macEntry != null && interfaceInfo != null) {
124 deleteSmacAndDmacFlows(elanInstanceCache.get(elanInstanceName).orElse(null),
125 interfaceInfo, srcMacAddress, tx);
126 } else if (macEntry == null) { //Remove flow of src flow entry only for MAC movement
127 MacEntry macEntryOfElanForwarding = elanUtils
128 .getMacEntryForElanInstance(elanTagInfo.getName(), physAddress).orElse(null);
129 if (macEntryOfElanForwarding != null) {
130 String macAddress = macEntryOfElanForwarding.getMacAddress().getValue();
131 elanUtils.deleteSmacFlowOnly(elanInstanceCache.get(elanInstanceName).orElse(null),
132 interfaceInfo, macAddress, tx);
134 deleteSmacAndDmacFlows(elanInstanceCache.get(elanInstanceName).orElse(null),
135 interfaceInfo, srcMacAddress, tx);
139 elanFutures.add(result);
140 addCallBack(result, srcMacAddress);
141 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
142 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
143 Optional<MacEntry> existingInterfaceMacEntry = ElanUtils.read(broker,
144 LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
145 if (existingInterfaceMacEntry.isPresent()) {
146 FluentFuture<? extends Object> future =
147 txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, tx -> {
148 tx.delete(macEntryIdForElanInterface);
149 MacEntry macEntryInElanInstance = elanUtils.getMacEntryForElanInstance(elanInstanceName,
150 physAddress).orElse(null);
151 if (macEntryInElanInstance != null
152 && macEntryInElanInstance.getInterface().equals(interfaceName)) {
153 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
154 .getMacEntryOperationalDataPath(elanInstanceName, physAddress);
155 tx.delete(macEntryIdForElanInstance);
158 elanFutures.add(future);
159 addCallBack(future, srcMacAddress);
162 }, ElanConstants.JOB_MAX_RETRIES);
166 private static void addCallBack(FluentFuture<? extends Object> writeResult, String srcMacAddress) {
168 writeResult.addCallback(new FutureCallback<Object>() {
170 public void onSuccess(Object noarg) {
171 LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
175 public void onFailure(Throwable error) {
176 LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
178 }, MoreExecutors.directExecutor());
181 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
182 TypedReadWriteTransaction<Configuration> deleteFlowTx)
183 throws ExecutionException, InterruptedException {
184 if (elanInfo == null || interfaceInfo == null) {
187 try (NamedSimpleReentrantLock.Acquired lock = elanUtils.lockElanMacDPN(elanInfo.getElanTag().toJava(),
188 macAddress, interfaceInfo.getDpId())) {
189 elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, deleteFlowTx);
194 public void onFlowUpdated(FlowUpdated arg0) {
195 // TODO Auto-generated method stub
199 public void onNodeErrorNotification(NodeErrorNotification arg0) {
200 // TODO Auto-generated method stub
205 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
206 // TODO Auto-generated method stub
211 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {