2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.rpc;
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;
25 public class RpcContextImpl implements RpcContext {
27 final ProviderContext providerContext;
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>>());
36 private int maxRequestsPerDevice;
38 public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
39 this.providerContext = providerContext;
40 this.deviceContext = deviceContext;
44 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
45 * org.opendaylight.yangtools.yang.binding.RpcService)
48 public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
49 final S serviceInstance) {
50 rpcRegistrations.add(providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance));
54 public <T extends DataObject> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
55 final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
57 if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
58 synchronizedRequestsList.add(requestContext);
60 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
61 .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
62 rpcResultFuture.set(rpcResult);
64 return rpcResultFuture;
68 * Unregisters all services.
70 * @see java.lang.AutoCloseable#close()
73 public void close() throws Exception {
74 for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) {
75 rpcRegistration.close();
80 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
83 public void setRequestContextQuota(final int maxRequestsPerDevice) {
84 this.maxRequestsPerDevice = maxRequestsPerDevice;
88 public <T extends DataObject> void forgetRequestContext(final RequestContext<T> requestContext) {
89 requestContexts.remove(requestContext);
93 public DeviceContext getDeviceContext() {
98 public <T extends DataObject> RequestContext<T> createRequestContext() {
99 return new RequestContextImpl<T>(this);
102 public boolean isRequestContextCapacityEmpty() {
103 return requestContexts.size() <= maxRequestsPerDevice;