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 java.math.BigInteger;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.mdsalutil.NwConstants;
16 import org.opendaylight.netvirt.elan.utils.ElanConstants;
17 import org.opendaylight.netvirt.elan.utils.ElanUtils;
18 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
19 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
20 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
21 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Optional;
39 import com.google.common.util.concurrent.FutureCallback;
40 import com.google.common.util.concurrent.Futures;
41 import com.google.common.util.concurrent.ListenableFuture;
43 @SuppressWarnings("deprecation")
44 public class ElanSmacFlowEventListener implements SalFlowListener {
46 private ElanServiceProvider elanServiceProvider = null;
47 private static volatile ElanSmacFlowEventListener elanSmacFlowEventListener = null;
48 private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
50 public ElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
51 this.elanServiceProvider = elanServiceProvider;
54 public static ElanSmacFlowEventListener getElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
55 if (elanSmacFlowEventListener == null)
56 synchronized (ElanPacketInHandler.class) {
57 if (elanSmacFlowEventListener == null) {
58 elanSmacFlowEventListener = new ElanSmacFlowEventListener(elanServiceProvider);
61 return elanSmacFlowEventListener;
65 public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
68 public void onFlowAdded(FlowAdded arg0) {
69 // TODO Auto-generated method stub
74 public void onFlowRemoved(FlowRemoved flowRemoved) {
75 short tableId = flowRemoved.getTableId();
76 if (tableId == NwConstants.ELAN_SMAC_TABLE) {
77 BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
78 long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
79 ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
80 if (elanTagInfo == null) {
83 final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
84 .getEthernetSource().getAddress().getValue().toUpperCase();
85 int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
87 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
90 Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
91 if (!existingInterfaceInfo.isPresent()) {
92 logger.debug("Interface is not available for port Tag {}", portTag);
95 String interfaceName = existingInterfaceInfo.get().getInterfaceName();
96 PhysAddress physAddress = new PhysAddress(srcMacAddress);
97 if (interfaceName == null) {
98 logger.error(String.format("LPort record not found for tag %d", portTag));
101 MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
102 InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager().getInterfaceInfo(interfaceName);
103 if (macEntry != null && interfaceInfo != null) {
104 WriteTransaction deleteFlowTx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
105 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry, deleteFlowTx);
106 ListenableFuture<Void> result = deleteFlowTx.submit();
107 addCallBack(result, srcMacAddress);
109 InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
110 InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
111 WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
112 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
113 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
114 ListenableFuture<Void> writeResult = tx.submit();
115 addCallBack(writeResult, srcMacAddress);
120 private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
122 Futures.addCallback(writeResult, new FutureCallback<Void>() {
124 public void onSuccess(Void noarg) {
125 logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
129 public void onFailure(Throwable error) {
130 logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
136 public void onFlowUpdated(FlowUpdated arg0) {
137 // TODO Auto-generated method stub
142 public void onNodeErrorNotification(NodeErrorNotification arg0) {
143 // TODO Auto-generated method stub
148 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
149 // TODO Auto-generated method stub
154 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {