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 com.google.common.base.Optional;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Locale;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
24 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
25 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
26 import org.opendaylight.genius.mdsalutil.NwConstants;
27 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
28 import org.opendaylight.netvirt.elan.utils.ElanConstants;
29 import org.opendaylight.netvirt.elan.utils.ElanUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 public class ElanSmacFlowEventListener implements SalFlowListener {
48 private static final Logger LOG = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
50 private final DataBroker broker;
51 private final IInterfaceManager interfaceManager;
52 private final ElanUtils elanUtils;
53 private final JobCoordinator jobCoordinator;
56 public ElanSmacFlowEventListener(DataBroker broker, IInterfaceManager interfaceManager, ElanUtils elanUtils,
57 JobCoordinator jobCoordinator) {
59 this.interfaceManager = interfaceManager;
60 this.elanUtils = elanUtils;
61 this.jobCoordinator = jobCoordinator;
65 public void onFlowAdded(FlowAdded arg0) {
66 // TODO Auto-generated method stub
71 public void onFlowRemoved(FlowRemoved flowRemoved) {
72 short tableId = flowRemoved.getTableId();
73 if (tableId == NwConstants.ELAN_SMAC_TABLE) {
74 BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
75 long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
76 ElanTagName elanTagInfo = elanUtils.getElanInfoByElanTag(elanTag);
77 if (elanTagInfo == null) {
80 final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
81 .getEthernetSource().getAddress().getValue().toUpperCase(Locale.getDefault());
82 int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
84 LOG.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
87 Optional<IfIndexInterface> existingInterfaceInfo = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
88 if (!existingInterfaceInfo.isPresent()) {
89 LOG.debug("Interface is not available for port Tag {}", portTag);
92 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
93 PhysAddress physAddress = new PhysAddress(srcMacAddress);
94 if (interfaceName == null) {
95 LOG.error(String.format("LPort record not found for tag %d", portTag));
98 jobCoordinator.enqueueJob(ElanUtils.getElanInterfaceJobKey(interfaceName), () -> {
99 List<ListenableFuture<Void>> elanFutures = new ArrayList<>();
100 MacEntry macEntry = elanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
101 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
102 String elanInstanceName = elanTagInfo.getName();
103 LOG.info("Deleting the Mac-Entry:{} present on ElanInstance:{}", macEntry, elanInstanceName);
104 if (macEntry != null && interfaceInfo != null) {
105 WriteTransaction deleteFlowTx = broker.newWriteOnlyTransaction();
106 elanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(broker, elanInstanceName), interfaceInfo,
107 macEntry, deleteFlowTx);
108 ListenableFuture<Void> result = deleteFlowTx.submit();
109 elanFutures.add(result);
110 addCallBack(result, srcMacAddress);
112 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
113 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
114 WriteTransaction tx = broker.newWriteOnlyTransaction();
115 Optional<MacEntry> existingInterfaceMacEntry = ElanUtils.read(broker,
116 LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
117 if (existingInterfaceMacEntry.isPresent()) {
118 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
119 MacEntry macEntryInElanInstance = elanUtils.getMacEntryForElanInstance(elanInstanceName,
120 physAddress).orNull();
121 if (macEntryInElanInstance != null
122 && macEntryInElanInstance.getInterface().equals(interfaceName)) {
123 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
124 .getMacEntryOperationalDataPath(elanInstanceName, physAddress);
125 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
127 ListenableFuture<Void> writeResult = tx.submit();
128 elanFutures.add(writeResult);
129 addCallBack(writeResult, srcMacAddress);
132 }, ElanConstants.JOB_MAX_RETRIES);
136 private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
138 Futures.addCallback(writeResult, new FutureCallback<Void>() {
140 public void onSuccess(Void noarg) {
141 LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
145 public void onFailure(Throwable error) {
146 LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
152 public void onFlowUpdated(FlowUpdated arg0) {
153 // TODO Auto-generated method stub
157 public void onNodeErrorNotification(NodeErrorNotification arg0) {
158 // TODO Auto-generated method stub
163 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
164 // TODO Auto-generated method stub
169 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {