-/**
+/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
package org.opendaylight.openflowplugin.impl.rpc;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
-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.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
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.ContextChainMastershipWatcher;
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
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.concepts.ObjectRegistration;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.RpcService;
+import org.opendaylight.yangtools.yang.common.Uint32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class RpcContextImpl implements RpcContext {
private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
- private final RpcProviderRegistry rpcProviderRegistry;
+ private final RpcProviderService rpcProviderRegistry;
private final MessageSpy messageSpy;
private final Semaphore tracker;
- private boolean isStatisticsRpcEnabled;
+ private final boolean isStatisticsRpcEnabled;
// TODO: add private Sal salBroker
- private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
+ private final ConcurrentMap<Class<?>, ObjectRegistration<? extends RpcService>> rpcRegistrations =
+ new ConcurrentHashMap<>();
private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
- private CONTEXT_STATE state;
private final DeviceInfo deviceInfo;
private final DeviceContext deviceContext;
private final ExtensionConverterProvider extensionConverterProvider;
private final ConvertorExecutor convertorExecutor;
private final NotificationPublishService notificationPublishService;
- RpcContextImpl(final DeviceInfo deviceInfo,
- final RpcProviderRegistry rpcProviderRegistry,
- final MessageSpy messageSpy,
- final int maxRequests,
- final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier,
- final DeviceContext deviceContext,
- final ExtensionConverterProvider extensionConverterProvider,
- final ConvertorExecutor convertorExecutor,
- final NotificationPublishService notificationPublishService) {
- this.messageSpy = Preconditions.checkNotNull(messageSpy);
- this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
- this.nodeInstanceIdentifier = nodeInstanceIdentifier;
+ private ContextChainMastershipWatcher contextChainMastershipWatcher = null;
- tracker = new Semaphore(maxRequests, true);
+ RpcContextImpl(@NonNull final RpcProviderService rpcProviderRegistry,
+ final int maxRequests,
+ @NonNull final DeviceContext deviceContext,
+ @NonNull final ExtensionConverterProvider extensionConverterProvider,
+ @NonNull final ConvertorExecutor convertorExecutor,
+ @NonNull final NotificationPublishService notificationPublishService,
+ final boolean statisticsRpcEnabled) {
+ this.deviceContext = deviceContext;
+ deviceInfo = deviceContext.getDeviceInfo();
+ nodeInstanceIdentifier = deviceContext.getDeviceInfo().getNodeInstanceIdentifier();
+ messageSpy = deviceContext.getMessageSpy();
+ this.rpcProviderRegistry = rpcProviderRegistry;
this.extensionConverterProvider = extensionConverterProvider;
this.notificationPublishService = notificationPublishService;
- setState(CONTEXT_STATE.WORKING);
- this.deviceInfo = deviceInfo;
- this.deviceContext = deviceContext;
this.convertorExecutor = convertorExecutor;
+ isStatisticsRpcEnabled = statisticsRpcEnabled;
+ tracker = new Semaphore(maxRequests, true);
}
- /**
- * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
- * org.opendaylight.yangtools.yang.binding.RpcService)
- */
@Override
public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
final S serviceInstance) {
- LOG.trace("Try to register service {} for device {}.", serviceClass, nodeInstanceIdentifier);
- if (! rpcRegistrations.containsKey(serviceClass)) {
- final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
- routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier);
+ if (!rpcRegistrations.containsKey(serviceClass)) {
+ final ObjectRegistration<S> routedRpcReg = rpcProviderRegistry.registerRpcImplementation(serviceClass,
+ serviceInstance, ImmutableSet.of(nodeInstanceIdentifier));
rpcRegistrations.put(serviceClass, routedRpcReg);
- LOG.debug("Registration of service {} for device {}.", serviceClass.getSimpleName(), nodeInstanceIdentifier.getKey().getId().getValue());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Registration of service {} for device {}.",
+ serviceClass.getSimpleName(),
+ nodeInstanceIdentifier.getKey().getId().getValue());
+ }
}
}
@Override
public <S extends RpcService> S lookupRpcService(final Class<S> serviceClass) {
- RoutedRpcRegistration<?> registration = rpcRegistrations.get(serviceClass);
+ ObjectRegistration<? extends RpcService> registration = rpcRegistrations.get(serviceClass);
final RpcService rpcService = registration.getInstance();
- return (S) rpcService;
+ return serviceClass.cast(rpcService);
}
- /**
- * Unregisters all services.
- *
- * @see java.lang.AutoCloseable#close()
- */
@Override
public void close() {
- if (CONTEXT_STATE.TERMINATION.equals(getState())){
+ unregisterRPCs();
+ }
+
+ private void unregisterRPCs() {
+ for (final Iterator<Entry<Class<?>, ObjectRegistration<? extends RpcService>>> iterator = Iterators
+ .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext(); ) {
+ final ObjectRegistration<? extends RpcService> rpcRegistration = iterator.next().getValue();
+ rpcRegistration.close();
+
if (LOG.isDebugEnabled()) {
- LOG.debug("RpcContext is already in TERMINATION state.");
- }
- } else {
- setState(CONTEXT_STATE.TERMINATION);
- for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
- .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext(); ) {
- final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
- rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
- rpcRegistration.close();
- if (LOG.isDebugEnabled()) {
- LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType().getSimpleName(),
- nodeInstanceIdentifier.getKey().getId().getValue());
- }
+ LOG.debug("Closing RPC Registration of service {} for device {}.",
+ rpcRegistration.getInstance().getClass().getSimpleName(),
+ nodeInstanceIdentifier.getKey().getId().getValue());
}
}
}
LOG.trace("Device queue {} at capacity", this);
return null;
} else {
- LOG.trace("Acquired semaphore for {}, available permits:{} ", nodeInstanceIdentifier.getKey().getId().getValue(), tracker.availablePermits());
+ LOG.trace("Acquired semaphore for {}, available permits:{} ",
+ nodeInstanceIdentifier.getKey().getId().getValue(), tracker.availablePermits());
}
- final Long xid = deviceInfo.reserveXidForDeviceMessage();
+ final Uint32 xid = deviceInfo.reserveXidForDeviceMessage();
if (xid == null) {
- LOG.warn("Xid cannot be reserved for new RequestContext, node:{}", nodeInstanceIdentifier.getKey().getId().getValue());
+ LOG.warn("Xid cannot be reserved for new RequestContext, node:{}",
+ nodeInstanceIdentifier.getKey().getId().getValue());
tracker.release();
return null;
}
- return new AbstractRequestContext<T>(xid) {
+ return new AbstractRequestContext<>(xid) {
@Override
public void close() {
tracker.release();
- final long xid = getXid().getValue();
- LOG.trace("Removed request context with xid {}", xid);
- messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
+ LOG.trace("Removed request context with xid {}", getXid().getValue());
+ messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
}
};
}
@Override
public <S extends RpcService> void unregisterRpcServiceImplementation(final Class<S> serviceClass) {
- LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
- final RoutedRpcRegistration<?> rpcRegistration = rpcRegistrations.remove(serviceClass);
+ LOG.trace("Try to unregister serviceClass {} for Node {}",
+ serviceClass, nodeInstanceIdentifier.getKey().getId());
+ final ObjectRegistration<? extends RpcService> rpcRegistration = rpcRegistrations.remove(serviceClass);
if (rpcRegistration != null) {
- rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
rpcRegistration.close();
- LOG.debug("Un-registration serviceClass {} for Node {}", serviceClass.getSimpleName(), nodeInstanceIdentifier.getKey().getId().getValue());
+ LOG.debug("Un-registration serviceClass {} for Node {}", serviceClass.getSimpleName(),
+ nodeInstanceIdentifier.getKey().getId().getValue());
}
}
@VisibleForTesting
boolean isEmptyRpcRegistrations() {
- return this.rpcRegistrations.isEmpty();
+ return rpcRegistrations.isEmpty();
}
@Override
- public void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled) {
- this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
- }
-
- @Override
- public boolean isStatisticsRpcEnabled() {
- return isStatisticsRpcEnabled;
- }
-
- @Override
- public CONTEXT_STATE getState() {
- return this.state;
- }
-
- @Override
- public void setState(CONTEXT_STATE state) {
- this.state = state;
+ public DeviceInfo getDeviceInfo() {
+ return deviceInfo;
}
@Override
- public ServiceGroupIdentifier getServiceIdentifier() {
- return this.deviceInfo.getServiceIdentifier();
+ public void registerMastershipWatcher(@NonNull final ContextChainMastershipWatcher newWatcher) {
+ contextChainMastershipWatcher = newWatcher;
}
@Override
- public DeviceInfo getDeviceInfo() {
- return this.deviceInfo;
+ public ListenableFuture<Void> closeServiceInstance() {
+ return Futures.transform(Futures.immediateFuture(null), input -> {
+ unregisterRPCs();
+ return null;
+ }, MoreExecutors.directExecutor());
}
@Override
- public void startupClusterServices() throws ExecutionException, InterruptedException {
+ public void instantiateServiceInstance() {
MdSalRegistrationUtils.registerServices(this, deviceContext, extensionConverterProvider, convertorExecutor);
- if (isStatisticsRpcEnabled) {
+
+ if (isStatisticsRpcEnabled && !deviceContext.canUseSingleLayerSerialization()) {
MdSalRegistrationUtils.registerStatCompatibilityServices(
this,
deviceContext,
convertorExecutor);
}
+ contextChainMastershipWatcher.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
}
+ @NonNull
@Override
- public ListenableFuture<Void> stopClusterServices(boolean deviceDisconnected) {
- MdSalRegistrationUtils.unregisterServices(this);
- return Futures.immediateFuture(null);
+ public ServiceGroupIdentifier getIdentifier() {
+ return deviceInfo.getServiceIdentifier();
}
}