Fix Checkstyle violations related to exception handling in elanmanager.
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanNodeListener.java
index 8758cfca69978febfa9fdf642963c0d0847a8466..b3eae7588bb64f0b250fd75188e0ff6a2e280825 100644 (file)
@@ -7,14 +7,24 @@
  */
 package org.opendaylight.netvirt.elan.internal;
 
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
-import org.opendaylight.genius.mdsalutil.*;
+import org.opendaylight.genius.mdsalutil.ActionInfo;
+import org.opendaylight.genius.mdsalutil.ActionType;
+import org.opendaylight.genius.mdsalutil.FlowEntity;
+import org.opendaylight.genius.mdsalutil.InstructionInfo;
+import org.opendaylight.genius.mdsalutil.InstructionType;
+import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.genius.mdsalutil.MatchInfo;
+import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
@@ -23,58 +33,40 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.List;
+public class ElanNodeListener extends AbstractDataChangeListener<Node> implements AutoCloseable {
 
-public class ElanNodeListener extends AbstractDataChangeListener<Node> {
+    private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
+
+    private final DataBroker broker;
+    private final IMdsalApiManager mdsalManager;
 
-    private static final Logger logger = LoggerFactory.getLogger(ElanNodeListener.class);
-    private static volatile ElanNodeListener elanNodeListener = null;
-    private ElanServiceProvider elanServiceProvider = null;
     private ListenerRegistration<DataChangeListener> listenerRegistration;
-    public static ElanNodeListener getElanNodeListener(ElanServiceProvider elanServiceProvider) {
-        if (elanNodeListener == null)
-            synchronized (ElanNodeListener.class) {
-                if (elanNodeListener == null)
-                {
-                    ElanNodeListener elanNodeListener = new ElanNodeListener(elanServiceProvider);
-                    return elanNodeListener;
-
-                }
-            }
-        return elanNodeListener;
-    }
 
-    public ElanNodeListener(ElanServiceProvider elanServiceProvider) {
+    public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
         super(Node.class);
-        this.elanServiceProvider= elanServiceProvider;
-        registerListener(this.elanServiceProvider.getBroker());
+        this.broker = dataBroker;
+        this.mdsalManager = mdsalManager;
+    }
+
+    public void init() {
+        registerListener(broker);
     }
 
     private void registerListener(final DataBroker db) {
-        try {
-            listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
-                getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
-        } catch (final Exception e) {
-            logger.error("ElanNodeListener: DataChange listener registration fail!", e);
-            throw new IllegalStateException("ElanNodeListener: registration Listener failed.", e);
-        }
+        listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
+            getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
     }
 
     private InstanceIdentifier<Node> getWildCardPath() {
         return InstanceIdentifier.create(Nodes.class).child(Node.class);
     }
 
-
     @Override
     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
-
     }
 
     @Override
     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
-
     }
 
     @Override
@@ -82,7 +74,7 @@ public class ElanNodeListener extends AbstractDataChangeListener<Node> {
         NodeId nodeId = add.getId();
         String[] node =  nodeId.getValue().split(":");
         if (node.length < 2) {
-            logger.warn("Unexpected nodeId {}", nodeId.getValue());
+            LOG.warn("Unexpected nodeId {}", nodeId.getValue());
             return;
         }
         BigInteger dpId = new BigInteger(node[1]);
@@ -95,34 +87,40 @@ public class ElanNodeListener extends AbstractDataChangeListener<Node> {
     }
 
     private void setupTableMissSmacFlow(BigInteger dpId) {
-        List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
-        List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
-        List <ActionInfo> actionsInfos = new ArrayList <ActionInfo> ();
+        List<InstructionInfo> mkInstructions = new ArrayList<>();
+        List<ActionInfo> actionsInfos = new ArrayList<>();
         actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
-        mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { ElanConstants.ELAN_DMAC_TABLE }));
+        mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
 
-        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_SMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_SMAC_TABLE),
-            0, "ELAN sMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC,
-            mkMatches, mkInstructions);
-        this.elanServiceProvider.getMdsalManager().installFlow(flowEntity);
+        List<MatchInfo> mkMatches = new ArrayList<>();
+        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
+                getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
+                ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
+        mdsalManager.installFlow(flowEntity);
     }
 
     private void setupTableMissDmacFlow(BigInteger dpId) {
-        List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
+        List<MatchInfo> mkMatches = new ArrayList<>();
 
-        List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
-        mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { ElanConstants.ELAN_UNKNOWN_DMAC_TABLE }));
+        List<InstructionInfo> mkInstructions = new ArrayList<>();
+        mkInstructions.add(
+                new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_UNKNOWN_DMAC_TABLE }));
 
-        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_DMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_DMAC_TABLE),
-            0, "ELAN dMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC,
-            mkMatches, mkInstructions);
-        this.elanServiceProvider.getMdsalManager().installFlow(flowEntity);
+        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
+                getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
+                ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
+        mdsalManager.installFlow(flowEntity);
     }
 
     private String getTableMissFlowRef(long tableId) {
         return new StringBuffer().append(tableId).toString();
     }
 
-
+    @Override
+    public void close() throws Exception {
+        if (listenerRegistration != null) {
+            listenerRegistration.close();
+        }
+    }
 }