Bug 5334
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / 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.vpnservice.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.vpnservice.elan.utils.ElanConstants;
16 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
17 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
18 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
19 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
20 import org.opendaylight.vpnservice.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.vpnservice.elan.rev150602.elan.tag.name.map.ElanTagName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._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     private final DataBroker broker;
45     private IInterfaceManager interfaceManager;
46     private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
47
48     public ElanSmacFlowEventListener(DataBroker dataBroker) {
49         broker = dataBroker;
50     }
51     private SalFlowService salFlowService;
52
53     public SalFlowService getSalFlowService() {
54         return this.salFlowService;
55     }
56
57     public void setSalFlowService(final SalFlowService salFlowService) {
58         this.salFlowService = salFlowService;
59     }
60     public void setInterfaceManager(IInterfaceManager interfaceManager) {
61         this.interfaceManager = interfaceManager;
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 arg0) {
75         // TODO Auto-generated method stub
76         
77     }
78
79     @Override
80     public void onFlowUpdated(FlowUpdated arg0) {
81         // TODO Auto-generated method stub
82         
83     }
84
85     @Override
86     public void onNodeErrorNotification(NodeErrorNotification arg0) {
87         // TODO Auto-generated method stub
88         
89     }
90
91     @Override
92     public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
93         // TODO Auto-generated method stub
94         
95     }
96
97     @Override
98     public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
99         short tableId = switchFlowRemoved.getTableId();
100         if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
101             BigInteger metadata = switchFlowRemoved.getMatch().getMetadata().getMetadata();
102             long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
103             ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
104             if (elanTagInfo == null) {
105                 return;
106             }
107             final String srcMacAddress = switchFlowRemoved.getMatch().getEthernetMatch()
108                     .getEthernetSource().getAddress().getValue().toUpperCase();
109             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
110             if (portTag == 0) {
111                 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
112                 return;
113             }
114             Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
115             if (!existingInterfaceInfo.isPresent()) {
116                 logger.debug("Interface is not available for port Tag {}", portTag);
117                 return;
118             }
119             String interfaceName = existingInterfaceInfo.get().getInterfaceName();
120             PhysAddress physAddress = new PhysAddress(srcMacAddress);
121             if (interfaceName == null) {
122                 logger.error(String.format("LPort record not found for tag %d", portTag));
123                 return;
124             }
125             MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
126             InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
127             if(macEntry != null && interfaceInfo != null) {
128                 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry);
129             }
130             InstanceIdentifier<MacEntry> macEntryIdForElanInterface =  ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
131             InstanceIdentifier<MacEntry> macEntryIdForElanInstance  =  ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
132             WriteTransaction tx = broker.newWriteOnlyTransaction();
133             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
134             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
135             ListenableFuture<Void> writeResult = tx.submit();
136
137             //WRITE Callback
138             Futures.addCallback(writeResult, new FutureCallback<Void>() {
139                 @Override
140                 public void onSuccess(Void noarg) {
141                     logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
142                 }
143
144                 @Override
145                 public void onFailure(Throwable error) {
146                     logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
147                 }
148             });
149         }
150     }
151 }