/** * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.openflowplugin.impl.rpc; import com.google.common.util.concurrent.SettableFuture; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext; import org.opendaylight.yangtools.yang.binding.RpcService; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; public class RpcContextImpl implements RpcContext { private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RpcContextImpl.class); final ProviderContext providerContext; // TODO: add private Sal salBroker private final DeviceContext deviceContext; private final List rpcRegistrations = new ArrayList<>(); private final List> synchronizedRequestsList = Collections .>synchronizedList(new ArrayList>()); private int maxRequestsPerDevice; public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) { this.providerContext = providerContext; this.deviceContext = deviceContext; } /** * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class, * org.opendaylight.yangtools.yang.binding.RpcService) */ @Override public void registerRpcServiceImplementation(final Class serviceClass, final S serviceInstance) { final RoutedRpcRegistration routedRpcReg = providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance); routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier()); rpcRegistrations.add(routedRpcReg); } @Override public SettableFuture> storeOrFail(final RequestContext requestContext) { final SettableFuture> rpcResultFuture = requestContext.getFuture(); if (synchronizedRequestsList.size() < maxRequestsPerDevice) { synchronizedRequestsList.add(requestContext); } else { final RpcResult rpcResult = RpcResultBuilder.failed() .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build(); rpcResultFuture.set(rpcResult); } return rpcResultFuture; } /** * Unregisters all services. * * @see java.lang.AutoCloseable#close() */ @Override public void close() throws Exception { for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) { rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier()); rpcRegistration.close(); } } /** * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int) */ @Override public void setRequestContextQuota(final int maxRequestsPerDevice) { this.maxRequestsPerDevice = maxRequestsPerDevice; } @Override public void forgetRequestContext(final RequestContext requestContext) { synchronizedRequestsList.remove(requestContext); LOG.trace("Removed request context with xid {}. Context request in list {}.", requestContext.getXid().getValue(), synchronizedRequestsList.size()); } @Override public DeviceContext getDeviceContext() { return deviceContext; } @Override public RequestContext createRequestContext() { return new RequestContextImpl(this); } public boolean isRequestContextCapacityEmpty() { return synchronizedRequestsList.size() <= maxRequestsPerDevice; } }