fix for mac movement issue
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanSmacFlowEventListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.internal;
9
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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.NwConstants;
22 import org.opendaylight.netvirt.elan.utils.ElanUtils;
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.flow.service.rev130819.FlowAdded;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @SuppressWarnings("deprecation")
39 public class ElanSmacFlowEventListener implements SalFlowListener {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
42
43     private final DataBroker broker;
44     private final IInterfaceManager interfaceManager;
45     private final ElanUtils elanUtils;
46
47     public ElanSmacFlowEventListener(DataBroker broker, IInterfaceManager interfaceManager, ElanUtils elanUtils) {
48         this.broker = broker;
49         this.interfaceManager = interfaceManager;
50         this.elanUtils = elanUtils;
51     }
52
53     @Override
54     public void onFlowAdded(FlowAdded arg0) {
55         // TODO Auto-generated method stub
56
57     }
58
59     @Override
60     public void onFlowRemoved(FlowRemoved flowRemoved) {
61         short tableId = flowRemoved.getTableId();
62         if (tableId == NwConstants.ELAN_SMAC_TABLE) {
63             BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
64             long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
65             ElanTagName elanTagInfo = elanUtils.getElanInfoByElanTag(elanTag);
66             if (elanTagInfo == null) {
67                 return;
68             }
69             final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
70                 .getEthernetSource().getAddress().getValue().toUpperCase();
71             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
72             if (portTag == 0) {
73                 LOG.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
74                 return;
75             }
76             Optional<IfIndexInterface> existingInterfaceInfo = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
77             if (!existingInterfaceInfo.isPresent()) {
78                 LOG.debug("Interface is not available for port Tag {}", portTag);
79                 return;
80             }
81             String interfaceName = existingInterfaceInfo.get().getInterfaceName();
82             PhysAddress physAddress = new PhysAddress(srcMacAddress);
83             if (interfaceName == null) {
84                 LOG.error(String.format("LPort record not found for tag %d", portTag));
85                 return;
86             }
87             MacEntry macEntry = elanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
88             InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
89             String elanInstanceName = elanTagInfo.getName();
90             if (macEntry != null && interfaceInfo != null) {
91                 WriteTransaction deleteFlowTx = broker.newWriteOnlyTransaction();
92                 elanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(broker, elanInstanceName), interfaceInfo,
93                         macEntry, deleteFlowTx);
94                 ListenableFuture<Void> result = deleteFlowTx.submit();
95                 addCallBack(result, srcMacAddress);
96             }
97             InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
98                     .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
99             WriteTransaction tx = broker.newWriteOnlyTransaction();
100             Optional<MacEntry> existingInterfaceMacEntry = elanUtils.read(broker,
101                 LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
102             if (existingInterfaceMacEntry.isPresent()) {
103                 tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
104                 MacEntry macEntryInElanInstance = elanUtils.getMacEntryForElanInstance(elanInstanceName,
105                         physAddress).orNull();
106                 if (macEntryInElanInstance != null
107                         && macEntryInElanInstance.getInterface().equals(interfaceName)) {
108                     InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
109                             .getMacEntryOperationalDataPath(elanInstanceName, physAddress);
110                     tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
111                 }
112                 ListenableFuture<Void> writeResult = tx.submit();
113                 addCallBack(writeResult, srcMacAddress);
114             }
115         }
116     }
117
118     private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
119         //WRITE Callback
120         Futures.addCallback(writeResult, new FutureCallback<Void>() {
121             @Override
122             public void onSuccess(Void noarg) {
123                 LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
124             }
125
126             @Override
127             public void onFailure(Throwable error) {
128                 LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
129             }
130         });
131     }
132
133     @Override
134     public void onFlowUpdated(FlowUpdated arg0) {
135         // TODO Auto-generated method stub
136
137     }
138
139     @Override
140     public void onNodeErrorNotification(NodeErrorNotification arg0) {
141         // TODO Auto-generated method stub
142
143     }
144
145     @Override
146     public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
147         // TODO Auto-generated method stub
148
149     }
150
151     @Override
152     public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
153
154     }
155
156
157 }