Make sure RequestContext has a constant XID
[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.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
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     // TODO: add private Sal salBroker
34     private final Collection<RoutedRpcRegistration<?>> rpcRegistrations = new HashSet<>();
35
36     public RpcContextImpl(final MessageSpy messageSpy, final RpcProviderRegistry rpcProviderRegistry, final DeviceContext deviceContext, final int maxRequests) {
37         this.messageSpy = messageSpy;
38         this.rpcProviderRegistry = rpcProviderRegistry;
39         this.deviceContext = Preconditions.checkNotNull(deviceContext);
40         tracker = new Semaphore(maxRequests, true);
41     }
42
43     /**
44      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
45      * org.opendaylight.yangtools.yang.binding.RpcService)
46      */
47     @Override
48     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
49                                                                         final S serviceInstance) {
50         final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
51         routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
52         rpcRegistrations.add(routedRpcReg);
53         LOG.debug("Registration of service {} for device {}.", serviceClass, deviceContext.getDeviceState().getNodeInstanceIdentifier());
54     }
55
56     /**
57      * Unregisters all services.
58      *
59      * @see java.lang.AutoCloseable#close()
60      */
61     @Override
62     public void close() {
63         for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
64             rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
65             rpcRegistration.close();
66         }
67     }
68
69     @Override
70     public <T> RequestContext<T> createRequestContext() {
71         if (!tracker.tryAcquire()) {
72             LOG.trace("Device queue {} at capacity", this);
73             return null;
74         }
75
76         return new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
77             @Override
78             public void close() {
79                 tracker.release();
80                 LOG.trace("Removed request context with xid {}", getXid().getValue());
81                 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
82             }
83         };
84     }
85
86     @Override
87     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
88         for (RoutedRpcRegistration<?> registration : rpcRegistrations) {
89             registration.close();
90         }
91     }
92 }