Added type parameteris.
[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.yangtools.yang.binding.DataObject;
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
25 public class RpcContextImpl implements RpcContext {
26
27     final ProviderContext providerContext;
28
29     // TODO: add private Sal salBroker
30     private final List<RequestContext<? extends DataObject>> requestContexts = new ArrayList<>();
31     private final DeviceContext deviceContext;
32     private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
33     private final List<RequestContext<? extends DataObject>> synchronizedRequestsList = Collections
34             .<RequestContext<? extends DataObject>>synchronizedList(new ArrayList<RequestContext<? extends DataObject>>());
35
36     private int maxRequestsPerDevice;
37
38     public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
39         this.providerContext = providerContext;
40         this.deviceContext = deviceContext;
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         rpcRegistrations.add(providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance));
51     }
52
53     @Override
54     public <T extends DataObject> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
55         final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
56
57         if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
58             synchronizedRequestsList.add(requestContext);
59         } else {
60             final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
61                     .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
62             rpcResultFuture.set(rpcResult);
63         }
64         return rpcResultFuture;
65     }
66
67     /**
68      * Unregisters all services.
69      *
70      * @see java.lang.AutoCloseable#close()
71      */
72     @Override
73     public void close() throws Exception {
74         for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) {
75             rpcRegistration.close();
76         }
77     }
78
79     /**
80      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
81      */
82     @Override
83     public void setRequestContextQuota(final int maxRequestsPerDevice) {
84         this.maxRequestsPerDevice = maxRequestsPerDevice;
85     }
86
87     @Override
88     public <T extends DataObject> void forgetRequestContext(final RequestContext<T> requestContext) {
89         requestContexts.remove(requestContext);
90     }
91
92     @Override
93     public DeviceContext getDeviceContext() {
94         return deviceContext;
95     }
96
97     @Override
98     public <T extends DataObject> RequestContext<T> createRequestContext() {
99         return new RequestContextImpl<T>(this);
100     }
101
102     public boolean isRequestContextCapacityEmpty() {
103         return requestContexts.size() <= maxRequestsPerDevice;
104     }
105
106 }