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 org.slf4j.Logger;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
20 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 public class RpcContextImpl implements RpcContext {
29 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RpcContextImpl.class);
30 final ProviderContext providerContext;
32 // TODO: add private Sal salBroker
33 private final DeviceContext deviceContext;
34 private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
35 private final List<RequestContext<?>> synchronizedRequestsList = Collections
36 .<RequestContext<?>>synchronizedList(new ArrayList<RequestContext<?>>());
38 private int maxRequestsPerDevice;
40 public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
41 this.providerContext = providerContext;
42 this.deviceContext = deviceContext;
46 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
47 * org.opendaylight.yangtools.yang.binding.RpcService)
50 public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
51 final S serviceInstance) {
52 final RoutedRpcRegistration<S> routedRpcReg = providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance);
53 routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
54 rpcRegistrations.add(routedRpcReg);
58 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
59 final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
61 if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
62 synchronizedRequestsList.add(requestContext);
64 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
65 .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
66 rpcResultFuture.set(rpcResult);
68 return rpcResultFuture;
72 * Unregisters all services.
74 * @see java.lang.AutoCloseable#close()
77 public void close() throws Exception {
78 for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
79 rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
80 rpcRegistration.close();
85 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
88 public void setRequestContextQuota(final int maxRequestsPerDevice) {
89 this.maxRequestsPerDevice = maxRequestsPerDevice;
93 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
94 synchronizedRequestsList.remove(requestContext);
95 LOG.trace("Removed request context with xid {}. Context request in list {}.",
96 requestContext.getXid(), synchronizedRequestsList.size());
100 public DeviceContext getDeviceContext() {
101 return deviceContext;
105 public <T> RequestContext<T> createRequestContext() {
106 return new RequestContextImpl<T>(this);
109 public boolean isRequestContextCapacityEmpty() {
110 return synchronizedRequestsList.size() <= maxRequestsPerDevice;