Merge "Add blueprint wiring for ipv6service"
[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 java.math.BigInteger;
11
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;
37
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;
42
43 @SuppressWarnings("deprecation")
44 public class ElanSmacFlowEventListener implements SalFlowListener {
45
46     private  ElanServiceProvider elanServiceProvider = null;
47     private static volatile ElanSmacFlowEventListener elanSmacFlowEventListener = null;
48     private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
49
50     public ElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
51         this.elanServiceProvider = elanServiceProvider;
52     }
53
54     public static ElanSmacFlowEventListener getElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
55         if (elanSmacFlowEventListener == null)
56             synchronized (ElanPacketInHandler.class) {
57                 if (elanSmacFlowEventListener == null) {
58                     elanSmacFlowEventListener = new ElanSmacFlowEventListener(elanServiceProvider);
59                 }
60             }
61         return elanSmacFlowEventListener;
62     }
63
64
65     public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
66     }
67     @Override
68     public void onFlowAdded(FlowAdded arg0) {
69         // TODO Auto-generated method stub
70
71     }
72
73     @Override
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) {
81                 return;
82             }
83             final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
84                 .getEthernetSource().getAddress().getValue().toUpperCase();
85             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
86             if (portTag == 0) {
87                 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
88                 return;
89             }
90             Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
91             if (!existingInterfaceInfo.isPresent()) {
92                 logger.debug("Interface is not available for port Tag {}", portTag);
93                 return;
94             }
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));
99                 return;
100             }
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);
108             }
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);
116         }
117
118     }
119
120     private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
121         //WRITE Callback
122         Futures.addCallback(writeResult, new FutureCallback<Void>() {
123             @Override
124             public void onSuccess(Void noarg) {
125                 logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
126             }
127
128             @Override
129             public void onFailure(Throwable error) {
130                 logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
131             }
132         });
133     }
134
135     @Override
136     public void onFlowUpdated(FlowUpdated arg0) {
137         // TODO Auto-generated method stub
138
139     }
140
141     @Override
142     public void onNodeErrorNotification(NodeErrorNotification arg0) {
143         // TODO Auto-generated method stub
144
145     }
146
147     @Override
148     public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
149         // TODO Auto-generated method stub
150
151     }
152
153     @Override
154     public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
155
156     }
157
158
159 }