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.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
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.infra.Datastore;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
28 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
29 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
30 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
31 import org.opendaylight.genius.mdsalutil.NwConstants;
32 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
33 import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock;
34 import org.opendaylight.mdsal.binding.api.DataBroker;
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<Void>> 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 ListenableFuture<Void> result = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
122 if (macEntry != null && interfaceInfo != null) {
123 deleteSmacAndDmacFlows(elanInstanceCache.get(elanInstanceName).orElse(null),
124 interfaceInfo, srcMacAddress, tx);
125 } else if (macEntry == null) { //Remove flow of src flow entry only for MAC movement
126 MacEntry macEntryOfElanForwarding = elanUtils.getMacEntryForElanInstance(elanTagInfo.getName(),
127 physAddress).orElse(null);
128 if (macEntryOfElanForwarding != null) {
129 String macAddress = macEntryOfElanForwarding.getMacAddress().getValue();
130 elanUtils.deleteSmacFlowOnly(elanInstanceCache.get(elanInstanceName).orElse(null),
131 interfaceInfo, macAddress, tx);
133 deleteSmacAndDmacFlows(elanInstanceCache.get(elanInstanceName).orElse(null), interfaceInfo,
138 elanFutures.add(result);
139 addCallBack(result, srcMacAddress);
140 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
141 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
142 Optional<MacEntry> existingInterfaceMacEntry = ElanUtils.read(broker,
143 LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
144 if (existingInterfaceMacEntry.isPresent()) {
145 ListenableFuture<Void> future = txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
147 tx.delete(macEntryIdForElanInterface);
148 MacEntry macEntryInElanInstance = elanUtils.getMacEntryForElanInstance(elanInstanceName,
149 physAddress).orElse(null);
150 if (macEntryInElanInstance != null
151 && macEntryInElanInstance.getInterface().equals(interfaceName)) {
152 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
153 .getMacEntryOperationalDataPath(elanInstanceName, physAddress);
154 tx.delete(macEntryIdForElanInstance);
157 elanFutures.add(future);
158 addCallBack(future, srcMacAddress);
161 }, ElanConstants.JOB_MAX_RETRIES);
165 private static void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
167 Futures.addCallback(writeResult, new FutureCallback<Void>() {
169 public void onSuccess(Void noarg) {
170 LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
174 public void onFailure(Throwable error) {
175 LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
177 }, MoreExecutors.directExecutor());
180 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
181 TypedReadWriteTransaction<Datastore.Configuration> deleteFlowTx)
182 throws ExecutionException, InterruptedException {
183 if (elanInfo == null || interfaceInfo == null) {
186 try (NamedSimpleReentrantLock.Acquired lock = elanUtils.lockElanMacDPN(elanInfo.getElanTag().toJava(),
187 macAddress, interfaceInfo.getDpId())) {
188 elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, deleteFlowTx);
193 public void onFlowUpdated(FlowUpdated arg0) {
194 // TODO Auto-generated method stub
198 public void onNodeErrorNotification(NodeErrorNotification arg0) {
199 // TODO Auto-generated method stub
204 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
205 // TODO Auto-generated method stub
210 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {