Merge "BUG-4177: Li - flowMod result ignores errors from device"
[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.RpcContext;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
21 import org.opendaylight.yangtools.yang.binding.RpcService;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class RpcContextImpl implements RpcContext {
26     private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
27     private final RpcProviderRegistry rpcProviderRegistry;
28     private final DeviceContext deviceContext;
29     private final MessageSpy messageSpy;
30     private final Semaphore tracker;
31
32     // TODO: add private Sal salBroker
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
55     /**
56      * Unregisters all services.
57      *
58      * @see java.lang.AutoCloseable#close()
59      */
60     @Override
61     public void close() {
62         for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
63             rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
64             rpcRegistration.close();
65         }
66     }
67
68     @Override
69     public <T> RequestContext<T> createRequestContext() {
70         if (!tracker.tryAcquire()) {
71             LOG.trace("Device queue {} at capacity", this);
72             return null;
73         }
74
75         return new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
76             @Override
77             public void close() {
78                 tracker.release();
79                 LOG.trace("Removed request context with xid {}", getXid().getValue());
80                 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
81             }
82         };
83     }
84
85     @Override
86     public void onDeviceContextClosed(DeviceContext deviceContext) {
87         close();
88     }
89 }