Merge "RPCEnabled flag moved from DeviceCtx"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.rpc;
9
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;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.RpcService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 class RpcContextImpl implements RpcContext {
33     private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
34     private final RpcProviderRegistry rpcProviderRegistry;
35     private final MessageSpy messageSpy;
36     private final Semaphore tracker;
37     private final XidSequencer xidSequencer;
38     private boolean isStatisticsRpcEnabled;
39
40     // TODO: add private Sal salBroker
41     private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
42     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
43
44     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;
53
54         tracker = new Semaphore(maxRequests, true);
55     }
56
57     /**
58      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
59      * org.opendaylight.yangtools.yang.binding.RpcService)
60      */
61     @Override
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);
70         }
71     }
72
73     @Override
74     public <S extends RpcService> S lookupRpcService(final Class<S> serviceClass) {
75         RoutedRpcRegistration<?> registration = rpcRegistrations.get(serviceClass);
76         final RpcService rpcService = registration.getInstance();
77         return (S) rpcService;
78     }
79
80     /**
81      * Unregisters all services.
82      *
83      * @see java.lang.AutoCloseable#close()
84      */
85     @Override
86     public void close() {
87         for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
88                 .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext();) {
89             final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
90             rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
91             rpcRegistration.close();
92             LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
93                     nodeInstanceIdentifier);
94         }
95     }
96
97     @Override
98     public <T> RequestContext<T> createRequestContext() {
99         if (!tracker.tryAcquire()) {
100             LOG.trace("Device queue {} at capacity", this);
101             return null;
102         } else {
103             LOG.trace("Acquired semaphore for {}, available permits:{} ", nodeInstanceIdentifier.getKey().getId(), tracker.availablePermits());
104         }
105
106         final Long xid = xidSequencer.reserveXidForDeviceMessage();
107         if (xid == null) {
108             LOG.warn("Xid cannot be reserved for new RequestContext, node:{}", nodeInstanceIdentifier.getKey().getId());
109             tracker.release();
110             return null;
111         }
112
113         return new AbstractRequestContext<T>(xid) {
114             @Override
115             public void close() {
116                 tracker.release();
117                 final long xid = getXid().getValue();
118                 LOG.trace("Removed request context with xid {}", xid);
119                 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
120             }
121         };
122     }
123
124     @Override
125     public <S extends RpcService> void unregisterRpcServiceImplementation(final Class<S> serviceClass) {
126         LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
127         final RoutedRpcRegistration<?> rpcRegistration = rpcRegistrations.remove(serviceClass);
128         if (rpcRegistration != null) {
129             rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
130             rpcRegistration.close();
131             LOG.debug("Unregistration serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
132         }
133     }
134
135     @VisibleForTesting
136     boolean isEmptyRpcRegistrations() {
137         return this.rpcRegistrations.isEmpty();
138     }
139
140     @Override
141     public void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled) {
142         this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
143     }
144
145     @Override
146     public boolean isStatisticsRpcEnabled() {
147         return isStatisticsRpcEnabled;
148     }
149 }