X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Frpc%2FRpcContextImpl.java;h=f450b96160b137e3b426601d5af3ea5ae4917e53;hb=39633bcebf3b3eb1bd1c92d37ba3c8b510916027;hp=6299cacf6607d130053a8c6b4db488d36b14632d;hpb=6afee8e2f21f7d248ce3e80118a86fe20c6b843c;p=openflowplugin.git 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 6299cacf66..f450b96160 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 @@ -7,45 +7,74 @@ */ package org.opendaylight.openflowplugin.impl.rpc; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; +import com.google.common.collect.Iterators; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import java.util.Iterator; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Semaphore; -import java.util.concurrent.atomic.AtomicLong; -import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; +import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; +import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState; +import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener; import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext; import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy; -import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils; +import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider; +import org.opendaylight.openflowplugin.impl.util.MdSalRegistrationUtils; +import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; import org.opendaylight.yangtools.yang.binding.RpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class RpcContextImpl implements RpcContext { +class RpcContextImpl implements RpcContext { private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class); private final RpcProviderRegistry rpcProviderRegistry; - private final DeviceContext deviceContext; private final MessageSpy messageSpy; private final Semaphore tracker; + private boolean isStatisticsRpcEnabled; // TODO: add private Sal salBroker private final ConcurrentMap, RoutedRpcRegistration> rpcRegistrations = new ConcurrentHashMap<>(); - private final boolean isStatisticsRpcEnabled; + private final KeyedInstanceIdentifier nodeInstanceIdentifier; + private volatile ContextState state = ContextState.INITIALIZATION; + private final DeviceInfo deviceInfo; + private final DeviceContext deviceContext; + private final ExtensionConverterProvider extensionConverterProvider; + private final ConvertorExecutor convertorExecutor; private final NotificationPublishService notificationPublishService; - public RpcContextImpl(final MessageSpy messageSpy, final RpcProviderRegistry rpcProviderRegistry, final DeviceContext deviceContext, - final int maxRequests, final boolean isStatisticsRpcEnabled, - final NotificationPublishService notificationPublishService) { - this.deviceContext = Preconditions.checkNotNull(deviceContext); - this.messageSpy = Preconditions.checkNotNull(messageSpy); - this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry); - this.isStatisticsRpcEnabled = isStatisticsRpcEnabled; + RpcContextImpl(@Nonnull final RpcProviderRegistry rpcProviderRegistry, + final int maxRequests, + @Nonnull final DeviceContext deviceContext, + @Nonnull final ExtensionConverterProvider extensionConverterProvider, + @Nonnull final ConvertorExecutor convertorExecutor, + @Nonnull final NotificationPublishService notificationPublishService, + boolean statisticsRpcEnabled) { + this.deviceContext = deviceContext; + this.deviceInfo = deviceContext.getDeviceInfo(); + this.nodeInstanceIdentifier = deviceContext.getDeviceInfo().getNodeInstanceIdentifier(); + this.messageSpy = deviceContext.getMessageSpy(); + this.rpcProviderRegistry = rpcProviderRegistry; + this.extensionConverterProvider = extensionConverterProvider; this.notificationPublishService = notificationPublishService; - tracker = new Semaphore(maxRequests, true); + this.convertorExecutor = convertorExecutor; + this.isStatisticsRpcEnabled = statisticsRpcEnabled; + this.tracker = new Semaphore(maxRequests, true); } /** @@ -55,27 +84,25 @@ public class RpcContextImpl implements RpcContext { @Override public void registerRpcServiceImplementation(final Class serviceClass, final S serviceInstance) { - LOG.trace("Try to register service {} for device {}.", serviceClass, deviceContext.getDeviceState().getNodeInstanceIdentifier()); if (! rpcRegistrations.containsKey(serviceClass)) { final RoutedRpcRegistration routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance); - routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier()); + routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier); rpcRegistrations.put(serviceClass, routedRpcReg); - LOG.debug("Registration of service {} for device {}.", serviceClass, deviceContext.getDeviceState().getNodeInstanceIdentifier()); - } - } - - public void registerStatCompatibilityServices() { - if (isStatisticsRpcEnabled) { - MdSalRegistratorUtils.registerStatCompatibilityServices(RpcContextImpl.this, deviceContext, - notificationPublishService, new AtomicLong()); + if (LOG.isDebugEnabled()) { + LOG.debug("Registration of service {} for device {}.", + serviceClass.getSimpleName(), + nodeInstanceIdentifier.getKey().getId().getValue()); + } } } @Override public S lookupRpcService(final Class serviceClass) { - final RpcService rpcService = rpcRegistrations.get(serviceClass).getInstance(); + RoutedRpcRegistration registration = rpcRegistrations.get(serviceClass); + final RpcService rpcService = registration.getInstance(); return (S) rpcService; } + /** * Unregisters all services. * @@ -83,13 +110,28 @@ public class RpcContextImpl implements RpcContext { */ @Override public void close() { - for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations.values()) { - rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier()); + if (ContextState.TERMINATION.equals(state)) { + if (LOG.isDebugEnabled()) { + LOG.debug("RpcContext for node {} is already in TERMINATION state.", getDeviceInfo().getLOGValue()); + } + } else { + this.state = ContextState.TERMINATION; + unregisterRPCs(); + } + } + + private void unregisterRPCs() { + 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(), - deviceContext.getDeviceState().getNodeInstanceIdentifier()); + + if (LOG.isDebugEnabled()) { + LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType().getSimpleName(), + nodeInstanceIdentifier.getKey().getId().getValue()); + } } - rpcRegistrations.clear(); } @Override @@ -98,28 +140,83 @@ public class RpcContextImpl implements RpcContext { LOG.trace("Device queue {} at capacity", this); return null; } else { - LOG.info("Acquired semaphore for {}, available permits:{} ", deviceContext.getDeviceState().getNodeId(), tracker.availablePermits()); + LOG.trace("Acquired semaphore for {}, available permits:{} ", nodeInstanceIdentifier.getKey().getId().getValue(), tracker.availablePermits()); } - return new AbstractRequestContext(deviceContext.reservedXidForDeviceMessage()) { + final Long xid = deviceInfo.reserveXidForDeviceMessage(); + if (xid == null) { + LOG.warn("Xid cannot be reserved for new RequestContext, node:{}", nodeInstanceIdentifier.getKey().getId().getValue()); + tracker.release(); + return null; + } + + return new AbstractRequestContext(xid) { @Override public void close() { tracker.release(); final long xid = getXid().getValue(); - LOG.info("Removed request context with xid {}", xid); - messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED); + LOG.trace("Removed request context with xid {}", xid); + messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED); } }; } @Override public void unregisterRpcServiceImplementation(final Class serviceClass) { - LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, deviceContext.getDeviceState().getNodeId()); + LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId()); final RoutedRpcRegistration rpcRegistration = rpcRegistrations.remove(serviceClass); if (rpcRegistration != null) { - rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier()); + rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier); rpcRegistration.close(); - LOG.debug("Unregistration serviceClass {} for Node {}", serviceClass, deviceContext.getDeviceState().getNodeId()); + LOG.debug("Un-registration serviceClass {} for Node {}", serviceClass.getSimpleName(), nodeInstanceIdentifier.getKey().getId().getValue()); + } + } + + @VisibleForTesting + boolean isEmptyRpcRegistrations() { + return this.rpcRegistrations.isEmpty(); + } + + @Override + public ServiceGroupIdentifier getServiceIdentifier() { + return this.deviceInfo.getServiceIdentifier(); + } + + @Override + public DeviceInfo getDeviceInfo() { + return this.deviceInfo; + } + + @Override + public ListenableFuture stopClusterServices() { + if (ContextState.TERMINATION.equals(this.state)) { + return Futures.immediateCancelledFuture(); } + + return Futures.transform(Futures.immediateFuture(null), new Function() { + @Nullable + @Override + public Void apply(@Nullable Object input) { + unregisterRPCs(); + return null; + } + }); + } + + @Override + public boolean onContextInstantiateService(final MastershipChangeListener mastershipChangeListener) { + LOG.info("Starting rpc context cluster services for node {}", deviceInfo.getLOGValue()); + MdSalRegistrationUtils.registerServices(this, deviceContext, extensionConverterProvider, convertorExecutor); + + if (isStatisticsRpcEnabled && !deviceContext.canUseSingleLayerSerialization()) { + MdSalRegistrationUtils.registerStatCompatibilityServices( + this, + deviceContext, + notificationPublishService, + convertorExecutor); + } + + mastershipChangeListener.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + return true; } }