Cleanup RequestContextStack
[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 java.util.Collection;
11 import java.util.HashSet;
12 import java.util.concurrent.Semaphore;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
14 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
22 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.binding.RpcService;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class RpcContextImpl implements RpcContext {
28     private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
29     private final RpcProviderRegistry rpcProviderRegistry;
30     private final MessageSpy messageSpy;
31     private final Semaphore tracker;
32
33     // TODO: add private Sal salBroker
34     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
35     private final Collection<RoutedRpcRegistration<?>> rpcRegistrations = new HashSet<>();
36
37     public RpcContextImpl(final MessageSpy messageSpy, final RpcProviderRegistry rpcProviderRegistry, final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier, final int maxRequests) {
38         this.messageSpy = messageSpy;
39         this.rpcProviderRegistry = rpcProviderRegistry;
40         this.nodeInstanceIdentifier = nodeInstanceIdentifier;
41         tracker = new Semaphore(maxRequests, true);
42     }
43
44     /**
45      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
46      * org.opendaylight.yangtools.yang.binding.RpcService)
47      */
48     @Override
49     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
50                                                                         final S serviceInstance) {
51         final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
52         routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier);
53         rpcRegistrations.add(routedRpcReg);
54         LOG.debug("Registration of service {} for device {}.", serviceClass, nodeInstanceIdentifier);
55     }
56
57     /**
58      * Unregisters all services.
59      *
60      * @see java.lang.AutoCloseable#close()
61      */
62     @Override
63     public void close() {
64         for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
65             rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
66             rpcRegistration.close();
67         }
68     }
69
70     @Override
71     public <T> RequestContext<T> createRequestContext() {
72         if (!tracker.tryAcquire()) {
73             LOG.trace("Device queue {} at capacity", this);
74             return null;
75         }
76
77         return new AbstractRequestContext<T>() {
78             @Override
79             public void close() {
80                 tracker.release();
81                 LOG.trace("Removed request context with xid {}", getXid().getValue());
82                 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
83             }
84         };
85     }
86
87     @Override
88     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
89         for (RoutedRpcRegistration<?> registration : rpcRegistrations) {
90             registration.close();
91         }
92     }
93 }