More elan Blueprint clean-up
[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             if (macEntry != null && interfaceInfo != null) {
90                 WriteTransaction deleteFlowTx = broker.newWriteOnlyTransaction();
91                 elanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(broker, elanTagInfo.getName()), interfaceInfo,
92                         macEntry, deleteFlowTx);
93                 ListenableFuture<Void> result = deleteFlowTx.submit();
94                 addCallBack(result, srcMacAddress);
95             }
96             InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
97                     .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
98             InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
99                     .getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
100             WriteTransaction tx = broker.newWriteOnlyTransaction();
101             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
102             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
103             ListenableFuture<Void> writeResult = tx.submit();
104             addCallBack(writeResult, srcMacAddress);
105         }
106
107     }
108
109     private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
110         //WRITE Callback
111         Futures.addCallback(writeResult, new FutureCallback<Void>() {
112             @Override
113             public void onSuccess(Void noarg) {
114                 LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
115             }
116
117             @Override
118             public void onFailure(Throwable error) {
119                 LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
120             }
121         });
122     }
123
124     @Override
125     public void onFlowUpdated(FlowUpdated arg0) {
126         // TODO Auto-generated method stub
127
128     }
129
130     @Override
131     public void onNodeErrorNotification(NodeErrorNotification arg0) {
132         // TODO Auto-generated method stub
133
134     }
135
136     @Override
137     public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
138         // TODO Auto-generated method stub
139
140     }
141
142     @Override
143     public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
144
145     }
146
147
148 }