Fix bugs related to wrong exception handling
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanSmacFlowEventListener.java
index 203c48f3558a3b0f4389ed2efb1cd4812f815d08..ce15848b4202029823820b6aaf490b8c64beb6a3 100644 (file)
@@ -7,17 +7,19 @@
  */
 package org.opendaylight.netvirt.elan.internal;
 
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.math.BigInteger;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.netvirt.elan.utils.ElanUtils;
-import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
-import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.genius.mdsalutil.NwConstants;
+import org.opendaylight.netvirt.elan.utils.ElanUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
@@ -25,45 +27,29 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.Flow
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-
 @SuppressWarnings("deprecation")
 public class ElanSmacFlowEventListener implements SalFlowListener {
-    private final DataBroker broker;
-    private IInterfaceManager interfaceManager;
-    private static final Logger logger = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
 
-    public ElanSmacFlowEventListener(DataBroker dataBroker) {
-        broker = dataBroker;
-    }
-    private SalFlowService salFlowService;
+    private static final Logger LOG = LoggerFactory.getLogger(ElanSmacFlowEventListener.class);
 
-    public SalFlowService getSalFlowService() {
-        return this.salFlowService;
-    }
+    private final DataBroker broker;
+    private final IInterfaceManager interfaceManager;
+    private final ElanUtils elanUtils;
 
-    public void setSalFlowService(final SalFlowService salFlowService) {
-        this.salFlowService = salFlowService;
-    }
-    public void setInterfaceManager(IInterfaceManager interfaceManager) {
+    public ElanSmacFlowEventListener(DataBroker broker, IInterfaceManager interfaceManager, ElanUtils elanUtils) {
+        this.broker = broker;
         this.interfaceManager = interfaceManager;
+        this.elanUtils = elanUtils;
     }
 
-
-    public void setMdSalApiManager(IMdsalApiManager mdsalManager) {
-    }
     @Override
     public void onFlowAdded(FlowAdded arg0) {
         // TODO Auto-generated method stub
@@ -71,81 +57,92 @@ public class ElanSmacFlowEventListener implements SalFlowListener {
     }
 
     @Override
-    public void onFlowRemoved(FlowRemoved arg0) {
-        // TODO Auto-generated method stub
-        
-    }
-
-    @Override
-    public void onFlowUpdated(FlowUpdated arg0) {
-        // TODO Auto-generated method stub
-        
-    }
-
-    @Override
-    public void onNodeErrorNotification(NodeErrorNotification arg0) {
-        // TODO Auto-generated method stub
-        
-    }
-
-    @Override
-    public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
-        // TODO Auto-generated method stub
-        
-    }
-
-    @Override
-    public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
-        short tableId = switchFlowRemoved.getTableId();
-        if (tableId == ElanConstants.ELAN_SMAC_TABLE) {
-            BigInteger metadata = switchFlowRemoved.getMatch().getMetadata().getMetadata();
+    public void onFlowRemoved(FlowRemoved flowRemoved) {
+        short tableId = flowRemoved.getTableId();
+        if (tableId == NwConstants.ELAN_SMAC_TABLE) {
+            BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
             long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
-            ElanTagName elanTagInfo = ElanUtils.getElanInfoByElanTag(elanTag);
+            ElanTagName elanTagInfo = elanUtils.getElanInfoByElanTag(elanTag);
             if (elanTagInfo == null) {
                 return;
             }
-            final String srcMacAddress = switchFlowRemoved.getMatch().getEthernetMatch()
-                    .getEthernetSource().getAddress().getValue().toUpperCase();
+            final String srcMacAddress = flowRemoved.getMatch().getEthernetMatch()
+                .getEthernetSource().getAddress().getValue().toUpperCase();
             int portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
             if (portTag == 0) {
-                logger.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
+                LOG.debug(String.format("Flow removed event on SMAC flow entry. But having port Tag as 0 "));
                 return;
             }
-            Optional<IfIndexInterface> existingInterfaceInfo = ElanUtils.getInterfaceInfoByInterfaceTag(portTag);
+            Optional<IfIndexInterface> existingInterfaceInfo = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
             if (!existingInterfaceInfo.isPresent()) {
-                logger.debug("Interface is not available for port Tag {}", portTag);
+                LOG.debug("Interface is not available for port Tag {}", portTag);
                 return;
             }
             String interfaceName = existingInterfaceInfo.get().getInterfaceName();
             PhysAddress physAddress = new PhysAddress(srcMacAddress);
             if (interfaceName == null) {
-                logger.error(String.format("LPort record not found for tag %d", portTag));
+                LOG.error(String.format("LPort record not found for tag %d", portTag));
                 return;
             }
-            MacEntry macEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
+            MacEntry macEntry = elanUtils.getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
             InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
-            if(macEntry != null && interfaceInfo != null) {
-                ElanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(elanTagInfo.getName()), interfaceInfo, macEntry);
+            if (macEntry != null && interfaceInfo != null) {
+                WriteTransaction deleteFlowTx = broker.newWriteOnlyTransaction();
+                elanUtils.deleteMacFlows(ElanUtils.getElanInstanceByName(broker, elanTagInfo.getName()), interfaceInfo,
+                        macEntry, deleteFlowTx);
+                ListenableFuture<Void> result = deleteFlowTx.submit();
+                addCallBack(result, srcMacAddress);
             }
-            InstanceIdentifier<MacEntry> macEntryIdForElanInterface =  ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
-            InstanceIdentifier<MacEntry> macEntryIdForElanInstance  =  ElanUtils.getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
+            InstanceIdentifier<MacEntry> macEntryIdForElanInterface = ElanUtils
+                    .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
+            InstanceIdentifier<MacEntry> macEntryIdForElanInstance = ElanUtils
+                    .getMacEntryOperationalDataPath(elanTagInfo.getName(), physAddress);
             WriteTransaction tx = broker.newWriteOnlyTransaction();
             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInterface);
             tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryIdForElanInstance);
             ListenableFuture<Void> writeResult = tx.submit();
-
-            //WRITE Callback
-            Futures.addCallback(writeResult, new FutureCallback<Void>() {
-                @Override
-                public void onSuccess(Void noarg) {
-                    logger.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
-                }
-
-                @Override
-                public void onFailure(Throwable error) {
-                    logger.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
-                }
-            });
+            addCallBack(writeResult, srcMacAddress);
         }
+
+    }
+
+    private void addCallBack(ListenableFuture<Void> writeResult, String srcMacAddress) {
+        //WRITE Callback
+        Futures.addCallback(writeResult, new FutureCallback<Void>() {
+            @Override
+            public void onSuccess(Void noarg) {
+                LOG.debug("Successfully removed macEntry {} from Operational Datastore", srcMacAddress);
+            }
+
+            @Override
+            public void onFailure(Throwable error) {
+                LOG.debug("Error {} while removing macEntry {} from Operational Datastore", error, srcMacAddress);
+            }
+        });
+    }
+
+    @Override
+    public void onFlowUpdated(FlowUpdated arg0) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public void onNodeErrorNotification(NodeErrorNotification arg0) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
+        // TODO Auto-generated method stub
+
     }
+
+    @Override
+    public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
+
+    }
+
+
 }