Fix findbugs violations in hwvtepsouthbound-impl
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundProvider.java
index 9956f4ef6eab46826fe9f87bc8d74c04d1ef2a45..1ff225a2e591e8c9469d6dfcefd31e271102931c 100644 (file)
@@ -9,10 +9,9 @@ package org.opendaylight.ovsdb.hwvtepsouthbound;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
-
 import java.util.Collection;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicBoolean;
-
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
@@ -25,7 +24,6 @@ import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipC
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
@@ -48,11 +46,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundProvider.class);
     private static final String ENTITY_TYPE = "ovsdb-hwvtepsouthbound-provider";
 
-    public static DataBroker getDb() {
-        return db;
-    }
-
-    private static DataBroker db;
+    private final DataBroker dataBroker;
     private final EntityOwnershipService entityOwnershipService;
     private final OvsdbConnection ovsdbConnection;
 
@@ -62,7 +56,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
     private HwvtepsbPluginInstanceEntityOwnershipListener providerOwnershipChangeListener;
     private HwvtepDataChangeListener hwvtepDTListener;
     private HwvtepReconciliationManager hwvtepReconciliationManager;
-    private AtomicBoolean registered = new AtomicBoolean(false);
+    private final AtomicBoolean registered = new AtomicBoolean(false);
     private ListenerRegistration<HwvtepSouthboundProvider> operTopologyRegistration;
 
     public HwvtepSouthboundProvider(final DataBroker dataBroker,
@@ -70,7 +64,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
             final OvsdbConnection ovsdbConnection,
             final DOMSchemaService schemaService,
             final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
-        this.db = dataBroker;
+        this.dataBroker = dataBroker;
         this.entityOwnershipService = entityOwnershipServiceDependency;
         registration = null;
         this.ovsdbConnection = ovsdbConnection;
@@ -84,10 +78,10 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
      */
     public void init() {
         LOG.info("HwvtepSouthboundProvider Session Initiated");
-        txInvoker = new TransactionInvokerImpl(db);
-        cm = new HwvtepConnectionManager(db, txInvoker, entityOwnershipService);
-        hwvtepDTListener = new HwvtepDataChangeListener(db, cm);
-        hwvtepReconciliationManager = new HwvtepReconciliationManager(db, cm);
+        txInvoker = new TransactionInvokerImpl(dataBroker);
+        cm = new HwvtepConnectionManager(dataBroker, txInvoker, entityOwnershipService);
+        hwvtepDTListener = new HwvtepDataChangeListener(dataBroker, cm);
+        hwvtepReconciliationManager = new HwvtepReconciliationManager(dataBroker, cm);
         //Register listener for entityOnwership changes
         providerOwnershipChangeListener =
                 new HwvtepsbPluginInstanceEntityOwnershipListener(this,this.entityOwnershipService);
@@ -95,7 +89,6 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         //register instance entity to get the ownership of the provider
         Entity instanceEntity = new Entity(ENTITY_TYPE, ENTITY_TYPE);
         try {
-            Optional<EntityOwnershipState> ownershipStateOpt = entityOwnershipService.getOwnershipState(instanceEntity);
             registration = entityOwnershipService.registerCandidate(instanceEntity);
         } catch (CandidateAlreadyRegisteredException e) {
             LOG.warn("HWVTEP Southbound Provider instance entity {} was already "
@@ -105,13 +98,14 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
         DataTreeIdentifier<Topology> treeId =
-                new DataTreeIdentifier<Topology>(LogicalDatastoreType.OPERATIONAL, path);
+                new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, path);
 
         LOG.trace("Registering listener for path {}", treeId);
-        operTopologyRegistration = db.registerDataTreeChangeListener(treeId, this);
+        operTopologyRegistration = dataBroker.registerDataTreeChangeListener(treeId, this);
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void close() throws Exception {
         LOG.info("HwvtepSouthboundProvider Closed");
         if (txInvoker != null) {
@@ -122,19 +116,19 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
                 LOG.error("HWVTEP Southbound Provider failed to close TransactionInvoker", e);
             }
         }
-        if(cm != null){
+        if (cm != null) {
             cm.close();
             cm = null;
         }
-        if(registration != null) {
+        if (registration != null) {
             registration.close();
             registration = null;
         }
-        if(providerOwnershipChangeListener != null) {
+        if (providerOwnershipChangeListener != null) {
             providerOwnershipChangeListener.close();
             providerOwnershipChangeListener = null;
         }
-        if(hwvtepDTListener != null) {
+        if (hwvtepDTListener != null) {
             hwvtepDTListener.close();
             hwvtepDTListener = null;
         }
@@ -148,7 +142,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         InstanceIdentifier<Topology> path = InstanceIdentifier
                 .create(NetworkTopology.class)
                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID));
-        ReadWriteTransaction transaction = db.newReadWriteTransaction();
+        ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
         CheckedFuture<Optional<Topology>, ReadFailedException> hwvtepTp = transaction.read(type, path);
         try {
             if (!hwvtepTp.get().isPresent()) {
@@ -159,7 +153,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
             } else {
                 transaction.cancel();
             }
-        } catch (Exception e) {
+        } catch (InterruptedException | ExecutionException e) {
             LOG.error("Error initializing hwvtep topology", e);
         }
     }
@@ -167,8 +161,8 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
     public void handleOwnershipChange(EntityOwnershipChange ownershipChange) {
         if (ownershipChange.isOwner()) {
             LOG.info("*This* instance of HWVTEP southbound provider is set as a MASTER instance");
-            LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present"
-                    ,HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
+            LOG.info("Initialize HWVTEP topology {} in operational and config data store if not already present",
+                    HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
             initializeHwvtepTopology(LogicalDatastoreType.OPERATIONAL);
             initializeHwvtepTopology(LogicalDatastoreType.CONFIGURATION);
         } else {
@@ -193,9 +187,9 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         }).start();
     }
 
-    private class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
-        private HwvtepSouthboundProvider hsp;
-        private EntityOwnershipListenerRegistration listenerRegistration;
+    private static class HwvtepsbPluginInstanceEntityOwnershipListener implements EntityOwnershipListener {
+        private final HwvtepSouthboundProvider hsp;
+        private final EntityOwnershipListenerRegistration listenerRegistration;
 
         HwvtepsbPluginInstanceEntityOwnershipListener(HwvtepSouthboundProvider hsp,
                 EntityOwnershipService entityOwnershipService) {
@@ -206,6 +200,7 @@ public class HwvtepSouthboundProvider implements ClusteredDataTreeChangeListener
         public void close() {
             this.listenerRegistration.close();
         }
+
         @Override
         public void ownershipChanged(EntityOwnershipChange ownershipChange) {
             hsp.handleOwnershipChange(ownershipChange);