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