830ec8b8809e6857c33214e94b7a63a4b8f464d1
[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.base.Preconditions;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.concurrent.Semaphore;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
19 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class RpcContextImpl implements RpcContext {
27     private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
28     private final RpcProviderRegistry rpcProviderRegistry;
29     private final DeviceContext deviceContext;
30     private final MessageSpy messageSpy;
31     private final Semaphore tracker;
32
33     private final Collection<RoutedRpcRegistration<?>> rpcRegistrations = new HashSet<>();
34
35     public RpcContextImpl(final MessageSpy messageSpy, final RpcProviderRegistry rpcProviderRegistry, final DeviceContext deviceContext, final int maxRequests) {
36         this.messageSpy = messageSpy;
37         this.rpcProviderRegistry = rpcProviderRegistry;
38         this.deviceContext = Preconditions.checkNotNull(deviceContext);
39         tracker = new Semaphore(maxRequests, true);
40     }
41
42     /**
43      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
44      * org.opendaylight.yangtools.yang.binding.RpcService)
45      */
46     @Override
47     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
48                                                                         final S serviceInstance) {
49         final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
50         routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
51         rpcRegistrations.add(routedRpcReg);
52         LOG.debug("Registration of service {} for device {}.", serviceClass, deviceContext.getDeviceState().getNodeInstanceIdentifier());
53
54         if (serviceInstance instanceof ItemLifeCycleSource) {
55             // TODO: collect registration for selective unregistering in case of tearing down only one rpc
56             deviceContext.getItemLifeCycleSourceRegistry().registerLifeCycleSource((ItemLifeCycleSource) serviceInstance);
57         }
58     }
59
60     @Override
61     public <S extends RpcService> S lookupRpcService(Class<S> serviceClass) {
62         S service = null;
63         for (RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
64             final RpcService rpcService = rpcRegistration.getInstance();
65             if (serviceClass.isInstance(rpcService)) {
66                 service = (S) rpcService;
67                 break;
68             }
69         }
70         return service;
71     }
72     /**
73      * Unregisters all services.
74      *
75      * @see java.lang.AutoCloseable#close()
76      */
77     @Override
78     public void close() {
79         for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
80             rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
81             rpcRegistration.close();
82             LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
83                     deviceContext.getDeviceState().getNodeInstanceIdentifier());
84         }
85     }
86
87     @Override
88     public <T> RequestContext<T> createRequestContext() {
89         if (!tracker.tryAcquire()) {
90             LOG.trace("Device queue {} at capacity", this);
91             return null;
92         }
93
94         return new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
95             @Override
96             public void close() {
97                 tracker.release();
98                 LOG.trace("Removed request context with xid {}", getXid().getValue());
99                 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
100             }
101         };
102     }
103 }