From c254fca40b61f91a5340cda0e64a1b50d9155305 Mon Sep 17 00:00:00 2001 From: Jozef Bacigal Date: Mon, 20 Jun 2016 09:17:38 +0200 Subject: [PATCH] Added context state to all contexts Change-Id: Ib2330821f71571c1dc6bcd81bec81fe288c2d83d Signed-off-by: Jozef Bacigal --- .../api/openflow/OFPContext.java | 21 +++++++++++++ .../api/openflow/device/DeviceContext.java | 25 ---------------- .../impl/device/DeviceContextImpl.java | 22 +++++++------- .../impl/device/DeviceManagerImpl.java | 2 +- .../impl/role/RoleContextImpl.java | 10 +++++++ .../impl/rpc/RpcContextImpl.java | 30 ++++++++++++++----- .../statistics/StatisticsContextImpl.java | 28 ++++++++++++----- 7 files changed, 87 insertions(+), 51 deletions(-) diff --git a/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/OFPContext.java b/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/OFPContext.java index 6e2dfedddb..788f0c5e2a 100644 --- a/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/OFPContext.java +++ b/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/OFPContext.java @@ -11,4 +11,25 @@ package org.opendaylight.openflowplugin.api.openflow; * General API for all OFP Context */ public interface OFPContext { + + /** + * distinguished device context states + */ + enum CONTEXT_STATE { + /** + * initial phase + */ + INITIALIZATION, + /** + * standard phase + */ + WORKING, + /** + * termination phase + */ + TERMINATION + } + + CONTEXT_STATE getState(); + } diff --git a/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java b/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java index 00bfdeb4b9..c5baf45cf6 100644 --- a/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java +++ b/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java @@ -44,31 +44,6 @@ public interface DeviceContext extends AutoCloseable, OFPContext, DeviceRegistry{ - /** - * distinguished device context states - */ - enum DEVICE_CONTEXT_STATE { - /** - * initial phase of talking to switch - */ - INITIALIZATION, - /** - * standard phase - interacting with switch - */ - WORKING, - /** - * termination phase of talking to switch - */ - TERMINATION - } - - /** - * Method returns current device context state. - * - * @return {@link DeviceContext.DEVICE_CONTEXT_STATE} - */ - DEVICE_CONTEXT_STATE getDeviceContextState(); - /** * Method close all auxiliary connections and primary connection. */ diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java index f25e3aa010..bcf4259fc9 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java @@ -131,7 +131,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi private final DeviceInfo deviceInfo; - private volatile DEVICE_CONTEXT_STATE deviceCtxState; + private volatile CONTEXT_STATE contextState; @VisibleForTesting DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext, @@ -168,7 +168,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi itemLifeCycleSourceRegistry = new ItemLifeCycleRegistryImpl(); flowLifeCycleKeeper = new ItemLifeCycleSourceImpl(); itemLifeCycleSourceRegistry.registerLifeCycleSource(flowLifeCycleKeeper); - deviceCtxState = DEVICE_CONTEXT_STATE.INITIALIZATION; + contextState = CONTEXT_STATE.INITIALIZATION; } /** @@ -448,8 +448,8 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi @Override public void onPublished() { - Verify.verify(DEVICE_CONTEXT_STATE.INITIALIZATION.equals(deviceCtxState)); - deviceCtxState = DEVICE_CONTEXT_STATE.WORKING; + Verify.verify(CONTEXT_STATE.INITIALIZATION.equals(contextState)); + contextState = CONTEXT_STATE.WORKING; primaryConnectionContext.getConnectionAdapter().setPacketInFiltering(false); for (final ConnectionContext switchAuxConnectionContext : auxiliaryConnectionContexts.values()) { switchAuxConnectionContext.getConnectionAdapter().setPacketInFiltering(false); @@ -484,11 +484,11 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi @Override public synchronized void shutdownConnection() { LOG.debug("Shutdown method for node {}", deviceInfo.getNodeId()); - if (DEVICE_CONTEXT_STATE.TERMINATION.equals(deviceCtxState)) { + if (CONTEXT_STATE.TERMINATION.equals(contextState)) { LOG.debug("DeviceCtx for Node {} is in termination process.", deviceInfo.getNodeId()); return; } - deviceCtxState = DEVICE_CONTEXT_STATE.TERMINATION; + contextState = CONTEXT_STATE.TERMINATION; if (ConnectionContext.CONNECTION_STATE.RIP.equals(getPrimaryConnectionContext().getConnectionState())) { LOG.debug("ConnectionCtx for Node {} is in RIP state.", deviceInfo.getNodeId()); @@ -507,11 +507,6 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi deviceMeterRegistry.close(); } - @Override - public DEVICE_CONTEXT_STATE getDeviceContextState() { - return deviceCtxState; - } - @Override public ListenableFuture shuttingDownDataStoreTransactions() { return transactionChainManager.shuttingDown(); @@ -521,4 +516,9 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi TransactionChainManager getTransactionChainManager() { return this.transactionChainManager; } + + @Override + public CONTEXT_STATE getState() { + return this.contextState; + } } diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java index b29a1e6c42..a19efba66b 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java @@ -374,7 +374,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi @Override public ListenableFuture apply(@Nonnull final Void input) throws Exception { statisticsContext.statListForCollectingInitialization(); - return statisticsContext.gatherDynamicData(); + return statisticsContext.initialGatherDynamicData(); } }); diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleContextImpl.java index e0392a7cc9..5ddf504fbb 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleContextImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleContextImpl.java @@ -16,6 +16,7 @@ import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlready import org.opendaylight.controller.md.sal.common.api.clustering.Entity; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration; import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService; +import org.opendaylight.openflowplugin.api.openflow.OFPContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor; @@ -47,6 +48,7 @@ class RoleContextImpl implements RoleContext { private final Semaphore roleChangeGuard = new Semaphore(1, true); private final LifecycleConductor conductor; + private volatile CONTEXT_STATE contextState; RoleContextImpl(final DeviceInfo deviceInfo, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) { this.entityOwnershipService = entityOwnershipService; @@ -54,11 +56,13 @@ class RoleContextImpl implements RoleContext { this.txEntity = txEntity; this.deviceInfo = deviceInfo; this.conductor = lifecycleConductor; + contextState = CONTEXT_STATE.INITIALIZATION; } @Override public boolean initialization() { LOG.info("Initialization main candidate for node {}", deviceInfo.getNodeId()); + contextState = CONTEXT_STATE.WORKING; return registerCandidate(this.entity); } @@ -183,10 +187,16 @@ class RoleContextImpl implements RoleContext { @Override public void close() { + contextState = CONTEXT_STATE.TERMINATION; unregisterAllCandidates(); } public boolean isMaster(){ return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null); } + + @Override + public CONTEXT_STATE getState() { + return contextState; + } } diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/rpc/RpcContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/rpc/RpcContextImpl.java index dfbe5fbb74..c91a134675 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/rpc/RpcContextImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/rpc/RpcContextImpl.java @@ -17,6 +17,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Semaphore; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.openflowplugin.api.openflow.OFPContext; import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer; import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext; @@ -37,6 +38,8 @@ class RpcContextImpl implements RpcContext { private final XidSequencer xidSequencer; private boolean isStatisticsRpcEnabled; + private volatile CONTEXT_STATE contextState; + // TODO: add private Sal salBroker private final ConcurrentMap, RoutedRpcRegistration> rpcRegistrations = new ConcurrentHashMap<>(); private final KeyedInstanceIdentifier nodeInstanceIdentifier; @@ -52,6 +55,7 @@ class RpcContextImpl implements RpcContext { this.nodeInstanceIdentifier = nodeInstanceIdentifier; tracker = new Semaphore(maxRequests, true); + contextState = CONTEXT_STATE.WORKING; } /** @@ -84,13 +88,20 @@ class RpcContextImpl implements RpcContext { */ @Override public void close() { - for (final Iterator, RoutedRpcRegistration>> iterator = Iterators - .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext();) { - final RoutedRpcRegistration rpcRegistration = iterator.next().getValue(); - rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier); - rpcRegistration.close(); - LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(), - nodeInstanceIdentifier); + if (CONTEXT_STATE.TERMINATION.equals(contextState)){ + if (LOG.isDebugEnabled()) { + LOG.debug("RpcContext is already in TERMINATION state."); + } + } else { + contextState = CONTEXT_STATE.TERMINATION; + for (final Iterator, RoutedRpcRegistration>> iterator = Iterators + .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext(); ) { + final RoutedRpcRegistration rpcRegistration = iterator.next().getValue(); + rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier); + rpcRegistration.close(); + LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(), + nodeInstanceIdentifier); + } } } @@ -146,4 +157,9 @@ class RpcContextImpl implements RpcContext { public boolean isStatisticsRpcEnabled() { return isStatisticsRpcEnabled; } + + @Override + public CONTEXT_STATE getState() { + return contextState; + } } diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpl.java index 5d56002585..362fbb6b13 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpl.java @@ -27,6 +27,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.annotation.concurrent.GuardedBy; +import org.opendaylight.openflowplugin.api.openflow.OFPContext; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; @@ -64,6 +65,7 @@ class StatisticsContextImpl implements StatisticsContext { private Timeout pollTimeout; private volatile boolean schedulingEnabled; + private volatile CONTEXT_STATE contextState; StatisticsContextImpl(@CheckForNull final DeviceInfo deviceInfo, final boolean shuttingDownStatisticsPolling, final LifecycleConductor lifecycleConductor) { this.deviceContext = Preconditions.checkNotNull(lifecycleConductor.getDeviceContext(deviceInfo)); @@ -74,6 +76,7 @@ class StatisticsContextImpl implements StatisticsContext { statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext); itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext); statListForCollectingInitialization(); + contextState = CONTEXT_STATE.WORKING; } @Override @@ -187,13 +190,20 @@ class StatisticsContextImpl implements StatisticsContext { @Override public void close() { - schedulingEnabled = false; - for (final Iterator> iterator = Iterators.consumingIterator(requestContexts.iterator()); - iterator.hasNext();) { - RequestContextUtil.closeRequestContextWithRpcError(iterator.next(), CONNECTION_CLOSED); - } - if (null != pollTimeout && !pollTimeout.isExpired()) { - pollTimeout.cancel(); + if (CONTEXT_STATE.TERMINATION.equals(contextState)) { + if (LOG.isDebugEnabled()) { + LOG.debug("Statistics context is already in state TERMINATION."); + } + } else { + contextState = CONTEXT_STATE.TERMINATION; + schedulingEnabled = false; + for (final Iterator> iterator = Iterators.consumingIterator(requestContexts.iterator()); + iterator.hasNext(); ) { + RequestContextUtil.closeRequestContextWithRpcError(iterator.next(), CONNECTION_CLOSED); + } + if (null != pollTimeout && !pollTimeout.isExpired()) { + pollTimeout.cancel(); + } } } @@ -369,4 +379,8 @@ class StatisticsContextImpl implements StatisticsContext { return itemLifeCycleListener; } + @Override + public CONTEXT_STATE getState() { + return contextState; + } } -- 2.36.6