ELAN FT Support for BE
[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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.vpnservice.elan.utils.ElanConstants;
13 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
14 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
15 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
16 import org.opendaylight.vpnservice.itm.api.IITMProvider;
17 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
18 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.*;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.tag.name.map.ElanTagName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterface;
24 //import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331._if.indexes._interface.map.IfIndexInterface;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.math.BigInteger;
30
31 public class ElanSmacFlowEventListener implements SalFlowListener {
32     private final DataBroker broker;
33     private IMdsalApiManager mdsalManager;
34     private IInterfaceManager interfaceManager;
35     private IITMProvider itmManager;
36     private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
37
38     public ElanSmacFlowEventListener(DataBroker dataBroker) {
39         broker = dataBroker;
40     }
41     private SalFlowService salFlowService;
42
43     public SalFlowService getSalFlowService() {
44         return this.salFlowService;
45     }
46
47     public void setSalFlowService(final SalFlowService salFlowService) {
48         this.salFlowService = salFlowService;
49     }
50     public void setInterfaceManager(IInterfaceManager interfaceManager) {
51         this.interfaceManager = interfaceManager;
52     }
53
54
55     public void setIITMManager(IITMProvider itmManager) {
56         this.itmManager = itmManager;
57     }
58     public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
59         this.mdsalManager = mdsalManager;
60     }
61     @Override
62     public void onFlowAdded(FlowAdded arg0) {
63         // TODO Auto-generated method stub
64         
65     }
66
67     @Override
68     public void onFlowRemoved(FlowRemoved arg0) {
69         // TODO Auto-generated method stub
70         
71     }
72
73     @Override
74     public void onFlowUpdated(FlowUpdated arg0) {
75         // TODO Auto-generated method stub
76         
77     }
78
79     @Override
80     public void onNodeErrorNotification(NodeErrorNotification arg0) {
81         // TODO Auto-generated method stub
82         
83     }
84
85     @Override
86     public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
87         // TODO Auto-generated method stub
88         
89     }
90
91     @Override
92     public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
93         short tableId = switchFlowRemoved.getTableId();
94         if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
95             BigInteger metadata = switchFlowRemoved.getMatch().getMetadata().getMetadata();
96             long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
97             ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
98             if (elanTagInfo == null) {
99                 return;
100             }
101             String srcMacAddress = switchFlowRemoved.getMatch().getEthernetMatch()
102                     .getEthernetSource().getAddress().getValue().toUpperCase();
103             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
104             if (portTag == 0) {
105                 logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
106                 return;
107             }
108             IfIndexInterface existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
109             String interfaceName = existingInterfaceInfo.getInterfaceName();
110             PhysAddress physAddress = new PhysAddress(srcMacAddress);
111             if (interfaceName == null) {
112                 logger.error(String.format("LPort record not found for tag %d", portTag));
113                 return;
114             }
115             MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
116             InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
117             if(macEntry != null && interfaceInfo != null) {
118                 ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry);
119             }
120             InstanceIdentifier<MacEntry> macEntryId =  ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
121             ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
122         }
123     }
124
125 }