*/
package org.opendaylight.openflowplugin.impl.rpc;
-import java.util.concurrent.atomic.AtomicLong;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import com.google.common.collect.Iterators;
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
-import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
-import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.concurrent.ConcurrentHashMap;
-
public class RpcManagerImpl implements RpcManager {
private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
private final RpcProviderRegistry rpcProviderRegistry;
private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
+ private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
private final int maxRequestsQuota;
- private final ConcurrentHashMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
- private boolean isStatisticsRpcEnabled;
- private NotificationPublishService notificationPublishService;
+ private final ConcurrentMap<NodeId, RpcContext> contexts = new ConcurrentHashMap<>();
+
+ private final LifecycleConductor conductor;
public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
- final int quotaValue) {
+ final int quotaValue,
+ final LifecycleConductor lifecycleConductor) {
this.rpcProviderRegistry = rpcProviderRegistry;
maxRequestsQuota = quotaValue;
+ this.conductor = lifecycleConductor;
}
@Override
}
@Override
- public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
- NodeId nodeId = deviceContext.getDeviceState().getNodeId();
- OfpRole ofpRole = deviceContext.getDeviceState().getRole();
-
- LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
-
- RpcContext rpcContext = contexts.get(deviceContext);
- if (rpcContext == null) {
- rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry, deviceContext, maxRequestsQuota);
- contexts.put(deviceContext, rpcContext);
- }
-
+ public void onDeviceContextLevelUp(final NodeId nodeId) throws Exception {
- if (ofpRole == OfpRole.BECOMESLAVE) {
- // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
- LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
- try {
- MdSalRegistratorUtils.unregisterServices(rpcContext);
- } catch (Exception e) {
- LOG.error("Exception while unregistering rpcs for slave role for node:{}. But continuing.", nodeId, e);
- }
+ final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(nodeId));
- } else {
- LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
- MdSalRegistratorUtils.registerServices(rpcContext, deviceContext);
+ final RpcContext rpcContext = new RpcContextImpl(
+ rpcProviderRegistry,
+ deviceContext,
+ deviceContext.getMessageSpy(),
+ maxRequestsQuota,
+ deviceContext.getDeviceState().getNodeInstanceIdentifier());
- if (isStatisticsRpcEnabled) {
- MdSalRegistratorUtils.registerStatCompatibilityServices(rpcContext, deviceContext,
- notificationPublishService, new AtomicLong());
- }
- }
+ deviceContext.setRpcContext(rpcContext);
- deviceContext.addDeviceContextClosedHandler(this);
+ Verify.verify(contexts.putIfAbsent(nodeId, rpcContext) == null, "RpcCtx still not closed for node {}", nodeId);
// finish device initialization cycle back to DeviceManager
- deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
+ deviceInitPhaseHandler.onDeviceContextLevelUp(nodeId);
}
@Override
- public void close() throws Exception {
-
+ public void close() {
+ for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
+ iterator.hasNext();) {
+ iterator.next().close();
+ }
}
-
@Override
- public void onDeviceContextClosed(DeviceContext deviceContext) {
- RpcContext removedContext = contexts.remove(deviceContext);
+ public void onDeviceContextLevelDown(final DeviceContext deviceContext) {
+ final RpcContext removedContext = contexts.remove(deviceContext.getDeviceState().getNodeId());
if (removedContext != null) {
- try {
- LOG.info("Unregistering rpcs for device context closure");
- removedContext.close();
- } catch (Exception e) {
- LOG.error("Exception while unregistering rpcs onDeviceContextClosed handler for node:{}. But continuing.",
- deviceContext.getDeviceState().getNodeId(), e);
- }
+ LOG.info("Unregister RPCs services for device context closure");
+ removedContext.close();
}
- }
- public void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled) {
- this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
+ deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceContext);
}
@Override
- public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
- this.notificationPublishService = notificationPublishService;
+ public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
+ this.deviceTerminPhaseHandler = handler;
+ }
+
+ /**
+ * This method is only for testing
+ */
+ @VisibleForTesting
+ void addRecordToContexts(NodeId nodeId, RpcContext rpcContexts) {
+ if(!contexts.containsKey(nodeId)) {
+ this.contexts.put(nodeId,rpcContexts);
+ }
}
}