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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
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;
24 import org.slf4j.Logger;
26 public class RpcContextImpl implements RpcContext {
28 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RpcContextImpl.class);
29 final ProviderContext providerContext;
31 // TODO: add private Sal salBroker
32 private final DeviceContext deviceContext;
33 private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
34 private final List<RequestContext<?>> synchronizedRequestsList = Collections
35 .<RequestContext<?>>synchronizedList(new ArrayList<RequestContext<?>>());
37 private int maxRequestsPerDevice;
39 public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
40 this.providerContext = providerContext;
41 this.deviceContext = deviceContext;
45 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
46 * org.opendaylight.yangtools.yang.binding.RpcService)
49 public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
50 final S serviceInstance) {
51 final RoutedRpcRegistration<S> routedRpcReg = providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance);
52 routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
53 rpcRegistrations.add(routedRpcReg);
57 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
58 final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
60 if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
61 synchronizedRequestsList.add(requestContext);
63 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
64 .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
65 rpcResultFuture.set(rpcResult);
67 return rpcResultFuture;
71 * Unregisters all services.
73 * @see java.lang.AutoCloseable#close()
76 public void close() throws Exception {
77 for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
78 rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
79 rpcRegistration.close();
84 * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
87 public void setRequestContextQuota(final int maxRequestsPerDevice) {
88 this.maxRequestsPerDevice = maxRequestsPerDevice;
92 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
93 synchronizedRequestsList.remove(requestContext);
94 LOG.trace("Removed request context with xid {}. Context request in list {}.",
95 requestContext.getXid().getValue(), synchronizedRequestsList.size());
99 public DeviceContext getDeviceContext() {
100 return deviceContext;
104 public <T> RequestContext<T> createRequestContext() {
105 return new RequestContextImpl<T>(this);
108 public boolean isRequestContextCapacityEmpty() {
109 return synchronizedRequestsList.size() <= maxRequestsPerDevice;