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 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;
25 public class RpcContextImpl implements RpcContext {
27 final ProviderContext providerContext;
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>());
35 private int maxRequestsPerDevice;
37 public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
38 this.providerContext = providerContext;
39 this.deviceContext = deviceContext;
43 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
44 * org.opendaylight.yangtools.yang.binding.RpcService)
47 public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
48 final S serviceInstance) {
49 rpcRegistrations.add(providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance));
53 public <T extends DataObject> SettableFuture<RpcResult<T>> storeOrFail(RequestContext requestContext) {
54 final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.createRequestFuture();
56 if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
57 synchronizedRequestsList.add(requestContext);
59 RpcResult rpcResult = RpcResultBuilder.failed().withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
60 rpcResultFuture.set(rpcResult);
62 return rpcResultFuture;
67 * Unregisters all services.
69 * @see java.lang.AutoCloseable#close()
72 public void close() throws Exception {
73 for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) {
74 rpcRegistration.close();
79 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
82 public void setRequestContextQuota(final int maxRequestsPerDevice) {
83 this.maxRequestsPerDevice = maxRequestsPerDevice;
87 public void forgetRequestContext(final RequestContext requestContext) {
88 requestContexts.remove(requestContext);
92 public DeviceContext getDeviceContext() {
97 public <T extends DataObject> RequestContext createRequestContext() {
98 return new RequestContextImpl<T>(this);
101 public boolean isRequestContextCapacityEmpty() {
102 return requestContexts.size() <= maxRequestsPerDevice;