tidy up addFlow futures
[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.util.concurrent.SettableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
20 import org.opendaylight.yangtools.yang.binding.RpcService;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.slf4j.Logger;
25
26 public class RpcContextImpl implements RpcContext {
27
28     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RpcContextImpl.class);
29     final ProviderContext providerContext;
30
31     // TODO: add private Sal salBroker
32     private final DeviceContext deviceContext;
33     private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
34     private final List<RequestContext<?>> synchronizedRequestsList = Collections
35             .<RequestContext<?>>synchronizedList(new ArrayList<RequestContext<?>>());
36
37     private int maxRequestsPerDevice;
38
39     public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
40         this.providerContext = providerContext;
41         this.deviceContext = deviceContext;
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 = providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance);
52         routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
53         rpcRegistrations.add(routedRpcReg);
54     }
55
56     @Override
57     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
58         final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
59
60         if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
61             synchronizedRequestsList.add(requestContext);
62         } else {
63             final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
64                     .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
65             rpcResultFuture.set(rpcResult);
66         }
67         return rpcResultFuture;
68     }
69
70     /**
71      * Unregisters all services.
72      *
73      * @see java.lang.AutoCloseable#close()
74      */
75     @Override
76     public void close() throws Exception {
77         for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
78             rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
79             rpcRegistration.close();
80         }
81     }
82
83     /**
84      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
85      */
86     @Override
87     public void setRequestContextQuota(final int maxRequestsPerDevice) {
88         this.maxRequestsPerDevice = maxRequestsPerDevice;
89     }
90
91     @Override
92     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
93         synchronizedRequestsList.remove(requestContext);
94         LOG.trace("Removed request context with xid {}. Context request in list {}.",
95                 requestContext.getXid().getValue(), synchronizedRequestsList.size());
96     }
97
98     @Override
99     public DeviceContext getDeviceContext() {
100         return deviceContext;
101     }
102
103     @Override
104     public <T> RequestContext<T> createRequestContext() {
105         return new RequestContextImpl<T>(this);
106     }
107
108     public boolean isRequestContextCapacityEmpty() {
109         return synchronizedRequestsList.size() <= maxRequestsPerDevice;
110     }
111
112 }