Fix connection closing before initialization
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / LifecycleServiceImpl.java
index 39696efd7b8aa5d730d0899f7c11b308026b77cd..7b6cbe58eed8d29efae56bc0d415260872e158d1 100644 (file)
@@ -79,6 +79,7 @@ public class LifecycleServiceImpl implements LifecycleService {
     @Override
     public void close() throws Exception {
         if (registration != null) {
+            LOG.info("Unregistering clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
             registration.close();
             registration = null;
         }
@@ -86,6 +87,8 @@ public class LifecycleServiceImpl implements LifecycleService {
 
     @Override
     public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
+        LOG.info("Registering clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
+
         //lifecycle service -> device context -> statistics context -> rpc context -> role context -> lifecycle service
         this.clusterInitializationPhaseHandler = deviceContext;
         this.deviceContext.setLifecycleInitializationPhaseHandler(this.statContext);
@@ -129,45 +132,9 @@ public class LifecycleServiceImpl implements LifecycleService {
     }
 
     private void fillDeviceFlowRegistry() {
-        // Fill device flow registry with flows from datastore
-        final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
-
-        // Start statistics scheduling only after we finished initializing device flow registry
-        Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
-            @Override
-            public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
-                if (LOG.isDebugEnabled()) {
-                    // Count all flows we read from datastore for debugging purposes.
-                    // This number do not always represent how many flows were actually added
-                    // to DeviceFlowRegistry, because of possible duplicates.
-                    long flowCount = Optional.fromNullable(result).asSet().stream()
-                            .flatMap(Collection::stream)
-                            .filter(Objects::nonNull)
-                            .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
-                            .filter(Objects::nonNull)
-                            .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
-                            .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
-                            .filter(Objects::nonNull)
-                            .filter(table -> Objects.nonNull(table.getFlow()))
-                            .flatMap(table -> table.getFlow().stream())
-                            .filter(Objects::nonNull)
-                            .count();
-
-                    LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
-                }
-            }
-
-            @Override
-            public void onFailure(Throwable t) {
-                if (deviceFlowRegistryFill.isCancelled()) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
-                    }
-                } else {
-                    LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);
-                }
-            }
-        });
+        
+       final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
+        Futures.addCallback(deviceFlowRegistryFill, new DeviceFlowRegistryCallback(deviceFlowRegistryFill));
     }
 
     @Override
@@ -188,4 +155,46 @@ public class LifecycleServiceImpl implements LifecycleService {
         fillDeviceFlowRegistry();
         return true;
     }
+
+    private class DeviceFlowRegistryCallback implements FutureCallback<List<Optional<FlowCapableNode>>> {
+        private final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill;
+
+        public DeviceFlowRegistryCallback(ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill) {
+            this.deviceFlowRegistryFill = deviceFlowRegistryFill;
+        }
+
+        @Override
+        public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
+            if (LOG.isDebugEnabled()) {
+                // Count all flows we read from datastore for debugging purposes.
+                // This number do not always represent how many flows were actually added
+                // to DeviceFlowRegistry, because of possible duplicates.
+                long flowCount = Optional.fromNullable(result).asSet().stream()
+                        .flatMap(Collection::stream)
+                        .filter(Objects::nonNull)
+                        .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
+                        .filter(Objects::nonNull)
+                        .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
+                        .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
+                        .filter(Objects::nonNull)
+                        .filter(table -> Objects.nonNull(table.getFlow()))
+                        .flatMap(table -> table.getFlow().stream())
+                        .filter(Objects::nonNull)
+                        .count();
+
+                LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
+            }
+        }
+
+        @Override
+        public void onFailure(Throwable t) {
+            if (deviceFlowRegistryFill.isCancelled()) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
+                }
+            } else {
+                LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);
+            }
+        }
+    }
 }