RpcContext and RequestContext API fine tuning
[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 java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.Future;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21
22 public class RpcContextImpl implements RpcContext {
23
24     final ProviderContext providerContext;
25
26     // TODO: add private Sal salBroker
27     private final List<RequestContext> requestContexts = new ArrayList<>();
28     private DeviceContext deviceContext;
29     private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
30
31     private int maxRequestsPerDevice;
32
33     public RpcContextImpl(final ProviderContext providerContext) {
34         this.providerContext = providerContext;
35     }
36
37     /**
38      * 
39      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
40      *      org.opendaylight.yangtools.yang.binding.RpcService)
41      */
42     @Override
43     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
44             final S serviceInstance) {
45         rpcRegistrations.add(providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance));
46     }
47
48     @Override
49     public Future<RpcResult<? extends DataObject>> addNewRequest(final DataObject data) {
50         return null;
51     }
52
53     /**
54      * Unregisters all services.
55      * 
56      * @see java.lang.AutoCloseable#close()
57      */
58     @Override
59     public void close() throws Exception {
60         for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) {
61             rpcRegistration.close();
62         }
63     }
64
65     /**
66      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
67      */
68     @Override
69     public void setRequestContextQuota(final int maxRequestsPerDevice) {
70         this.maxRequestsPerDevice = maxRequestsPerDevice;
71     }
72
73     @Override
74     public void forgetRequestContext(final RequestContext requestContext) {
75
76     }
77
78     public boolean isRequestContextCapacityEmpty() {
79         return requestContexts.size() <= maxRequestsPerDevice;
80     }
81
82
83 }