Merge "Neutron port listener updated to support allowed address pair with security...
[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.netvirt.elan.utils.ElanConstants;
16 import org.opendaylight.netvirt.elan.utils.ElanUtils;
17 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
18 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
19 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
20 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.base.Optional;
38 import com.google.common.util.concurrent.FutureCallback;
39 import com.google.common.util.concurrent.Futures;
40 import com.google.common.util.concurrent.ListenableFuture;
41
42 @SuppressWarnings("deprecation")
43 public class ElanSmacFlowEventListener implements SalFlowListener {
44
45     private  ElanServiceProvider elanServiceProvider = null;
46     private static volatile ElanSmacFlowEventListener elanSmacFlowEventListener = null;
47     private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
48
49     public ElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
50         this.elanServiceProvider = elanServiceProvider;
51     }
52
53     public static ElanSmacFlowEventListener getElanSmacFlowEventListener(ElanServiceProvider elanServiceProvider) {
54         if (elanSmacFlowEventListener == null)
55             synchronized (ElanPacketInHandler.class) {
56                 if (elanSmacFlowEventListener == null) {
57                     elanSmacFlowEventListener = new ElanSmacFlowEventListener(elanServiceProvider);
58                 }
59             }
60         return elanSmacFlowEventListener;
61     }
62
63
64     public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
65     }
66     @Override
67     public void onFlowAdded(FlowAdded arg0) {
68         // TODO Auto-generated method stub
69
70     }
71
72     @Override
73     public void onFlowRemoved(FlowRemoved flowRemoved) {
74         short tableId = flowRemoved.getTableId();
75         if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
76             BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
77             long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
78             ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
79             if (elanTagInfo == null) {
80                 return;
81             }
82             final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
83                 .getEthernetSource().getAddress().getValue().toUpperCase();
84             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
85             if (portTag == 0) {
86                 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
87                 return;
88             }
89             Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
90             if (!existingInterfaceInfo.isPresent()) {
91                 logger.debug("Interface is not available for port Tag {}", portTag);
92                 return;
93             }
94             String interfaceName = existingInterfaceInfo.get().getInterfaceName();
95             PhysAddress physAddress = new PhysAddress(srcMacAddress);
96             if (interfaceName == null) {
97                 logger.error(String.format("LPort record not found for tag %d", portTag));
98                 return;
99             }
100             MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
101             InterfaceInfo interfaceInfo = elanServiceProvider.getInterfaceManager().getInterfaceInfo(interfaceName);
102             if (macEntry != null && interfaceInfo != null) {
103                 WriteTransaction deleteFlowTx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
104                 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry, deleteFlowTx);
105                 ListenableFuture<Void> result = deleteFlowTx.submit();
106                 addCallBack(result, srcMacAddress);
107             }
108             InstanceIdentifier<MacEntry> macEntryIdForElanInterface =  ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
109             InstanceIdentifier<MacEntry> macEntryIdForElanInstance  =  ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
110             WriteTransaction tx = elanServiceProvider.getBroker().newWriteOnlyTransaction();
111             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
112             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
113             ListenableFuture<Void> writeResult = tx.submit();
114             addCallBack(writeResult, srcMacAddress);
115         }
116
117     }
118
119     private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
120         //WRITE Callback
121         Futures.addCallback(writeResult, new FutureCallback<Void>() {
122             @Override
123             public void onSuccess(Void noarg) {
124                 logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
125             }
126
127             @Override
128             public void onFailure(Throwable error) {
129                 logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
130             }
131         });
132     }
133
134     @Override
135     public void onFlowUpdated(FlowUpdated arg0) {
136         // TODO Auto-generated method stub
137
138     }
139
140     @Override
141     public void onNodeErrorNotification(NodeErrorNotification arg0) {
142         // TODO Auto-generated method stub
143
144     }
145
146     @Override
147     public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
148         // TODO Auto-generated method stub
149
150     }
151
152     @Override
153     public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
154
155     }
156
157
158 }