2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.rpc;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterators;
13 import java.util.Iterator;
14 import java.util.Map.Entry;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.Semaphore;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
23 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
26 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.binding.RpcService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class RpcContextImpl implements RpcContext {
35 private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
36 private final RpcProviderRegistry rpcProviderRegistry;
37 private final MessageSpy messageSpy;
38 private final Semaphore tracker;
39 private final XidSequencer xidSequencer;
41 // TODO: add private Sal salBroker
42 private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
43 private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
45 public RpcContextImpl(final RpcProviderRegistry rpcProviderRegistry,
46 final XidSequencer xidSequencer,
47 final MessageSpy messageSpy,
48 final int maxRequests,
49 final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier) {
50 this.xidSequencer = Preconditions.checkNotNull(xidSequencer);
51 this.messageSpy = Preconditions.checkNotNull(messageSpy);
52 this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
53 this.nodeInstanceIdentifier = nodeInstanceIdentifier;
55 tracker = new Semaphore(maxRequests, true);
59 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
60 * org.opendaylight.yangtools.yang.binding.RpcService)
63 public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
64 final S serviceInstance) {
65 LOG.trace("Try to register service {} for device {}.", serviceClass, nodeInstanceIdentifier);
66 if (! rpcRegistrations.containsKey(serviceClass)) {
67 final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
68 routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier);
69 rpcRegistrations.put(serviceClass, routedRpcReg);
70 LOG.debug("Registration of service {} for device {}.", serviceClass, nodeInstanceIdentifier);
75 public <S extends RpcService> S lookupRpcService(final Class<S> serviceClass) {
76 RoutedRpcRegistration<?> registration = rpcRegistrations.get(serviceClass);
77 final RpcService rpcService = registration.getInstance();
78 return (S) rpcService;
82 * Unregisters all services.
84 * @see java.lang.AutoCloseable#close()
88 for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
89 .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext();) {
90 final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
91 rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
92 rpcRegistration.close();
93 LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
94 nodeInstanceIdentifier);
99 public <T> RequestContext<T> createRequestContext() {
100 if (!tracker.tryAcquire()) {
101 LOG.trace("Device queue {} at capacity", this);
104 LOG.trace("Acquired semaphore for {}, available permits:{} ", nodeInstanceIdentifier.getKey().getId(), tracker.availablePermits());
107 final Long xid = xidSequencer.reserveXidForDeviceMessage();
109 LOG.warn("Xid cannot be reserved for new RequestContext, node:{}", nodeInstanceIdentifier.getKey().getId());
114 return new AbstractRequestContext<T>(xid) {
116 public void close() {
118 final long xid = getXid().getValue();
119 LOG.trace("Removed request context with xid {}", xid);
120 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
126 public <S extends RpcService> void unregisterRpcServiceImplementation(final Class<S> serviceClass) {
127 LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
128 final RoutedRpcRegistration<?> rpcRegistration = rpcRegistrations.remove(serviceClass);
129 if (rpcRegistration != null) {
130 rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
131 rpcRegistration.close();
132 LOG.debug("Unregistration serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
137 public boolean isEmptyRpcRegistrations() {
138 return this.rpcRegistrations.isEmpty();