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.base.Preconditions;
11 import com.google.common.collect.Iterators;
12 import java.util.Iterator;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.Semaphore;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
22 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
23 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.RpcService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class RpcContextImpl implements RpcContext {
34 private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
35 private final RpcProviderRegistry rpcProviderRegistry;
36 private final MessageSpy messageSpy;
37 private final Semaphore tracker;
38 private final XidSequencer xidSequencer;
40 // TODO: add private Sal salBroker
41 private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
42 private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
44 public RpcContextImpl(final RpcProviderRegistry rpcProviderRegistry,
45 final XidSequencer xidSequencer,
46 final MessageSpy messageSpy,
47 final int maxRequests,
48 final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier) {
49 this.xidSequencer = Preconditions.checkNotNull(xidSequencer);
50 this.messageSpy = Preconditions.checkNotNull(messageSpy);
51 this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
52 this.nodeInstanceIdentifier = nodeInstanceIdentifier;
54 tracker = new Semaphore(maxRequests, true);
58 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
59 * org.opendaylight.yangtools.yang.binding.RpcService)
62 public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
63 final S serviceInstance) {
64 LOG.trace("Try to register service {} for device {}.", serviceClass, nodeInstanceIdentifier);
65 if (! rpcRegistrations.containsKey(serviceClass)) {
66 final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
67 routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier);
68 rpcRegistrations.put(serviceClass, routedRpcReg);
69 LOG.debug("Registration of service {} for device {}.", serviceClass, nodeInstanceIdentifier);
74 public <S extends RpcService> S lookupRpcService(final Class<S> serviceClass) {
75 final RpcService rpcService = rpcRegistrations.get(serviceClass).getInstance();
76 return (S) rpcService;
80 * Unregisters all services.
82 * @see java.lang.AutoCloseable#close()
86 for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
87 .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext();) {
88 final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
89 rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
90 rpcRegistration.close();
91 LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
92 nodeInstanceIdentifier);
97 public <T> RequestContext<T> createRequestContext() {
98 if (!tracker.tryAcquire()) {
99 LOG.trace("Device queue {} at capacity", this);
102 LOG.trace("Acquired semaphore for {}, available permits:{} ", nodeInstanceIdentifier.getKey().getId(), tracker.availablePermits());
105 final Long xid = xidSequencer.reserveXidForDeviceMessage();
107 LOG.warn("Xid cannot be reserved for new RequestContext, node:{}", nodeInstanceIdentifier.getKey().getId());
112 return new AbstractRequestContext<T>(xid) {
114 public void close() {
116 final long xid = getXid().getValue();
117 LOG.trace("Removed request context with xid {}", xid);
118 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
124 public <S extends RpcService> void unregisterRpcServiceImplementation(final Class<S> serviceClass) {
125 LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
126 final RoutedRpcRegistration<?> rpcRegistration = rpcRegistrations.remove(serviceClass);
127 if (rpcRegistration != null) {
128 rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
129 rpcRegistration.close();
130 LOG.debug("Unregistration serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());