Implementation of Rpc interfaces
[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.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
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.RpcError;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.concurrent.Future;
27
28 public class RpcContextImpl implements RpcContext {
29
30     final ProviderContext providerContext;
31
32     // TODO: add private Sal salBroker
33     private final List<RequestContext> requestContexts = new ArrayList<>();
34     private DeviceContext deviceContext;
35     private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
36     private final List<RequestContext> synchronizedRequestsList = Collections.synchronizedList(new ArrayList<RequestContext>());
37
38     private int maxRequestsPerDevice;
39
40     public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
41         this.providerContext = providerContext;
42         this.deviceContext = deviceContext;
43     }
44
45     /**
46      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
47      * org.opendaylight.yangtools.yang.binding.RpcService)
48      */
49     @Override
50     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
51                                                                         final S serviceInstance) {
52         rpcRegistrations.add(providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance));
53     }
54
55     @Override
56     public <T extends DataObject> Future<RpcResult<T>> addNewRequest(final DataObject data) {
57         final Future<RpcResult<T>> rpcResultFuture;
58         final RequestContext requestContext = new RequestContextImpl(this);
59
60         if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
61             synchronizedRequestsList.add(requestContext);
62             rpcResultFuture = requestContext.createRequestFuture(data);
63
64             ListenableFuture<RpcResult<Void>> resultFutureFromDevice = sendRequestToDevice(data);
65             Futures.addCallback(resultFutureFromDevice, new FutureCallback<Object>() {
66                 @Override
67                 public void onSuccess(final Object o) {
68                     requestContext.requestSucceeded();
69                 }
70
71                 @Override
72                 public void onFailure(final Throwable throwable) {
73                     requestContext.requestFailed(throwable.getCause().getMessage());
74                 }
75             });
76         } else {
77             rpcResultFuture = Futures.immediateFuture(RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, "", "Request queue full.").build());
78         }
79
80
81         return rpcResultFuture;
82     }
83
84     private ListenableFuture<RpcResult<Void>> sendRequestToDevice(final DataObject data) {
85         //TODO : send data to device
86         return null;
87     }
88
89
90     /**
91      * Unregisters all services.
92      *
93      * @see java.lang.AutoCloseable#close()
94      */
95     @Override
96     public void close() throws Exception {
97         for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) {
98             rpcRegistration.close();
99         }
100     }
101
102     /**
103      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
104      */
105     @Override
106     public void setRequestContextQuota(final int maxRequestsPerDevice) {
107         this.maxRequestsPerDevice = maxRequestsPerDevice;
108     }
109
110     @Override
111     public void forgetRequestContext(final RequestContext requestContext) {
112         requestContexts.remove(requestContext);
113     }
114
115     public boolean isRequestContextCapacityEmpty() {
116         return requestContexts.size() <= maxRequestsPerDevice;
117     }
118
119 }