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.services;
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.math.BigInteger;
15 import java.util.concurrent.Future;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
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.device.RequestContextStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
22 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.slf4j.Logger;
27 public abstract class CommonService {
28 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
29 private static final long WAIT_TIME = 2000;
30 protected final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
31 .<Void>failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
32 protected static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
34 // protected OFRpcTaskContext rpcTaskContext;
36 public BigInteger datapathId;
37 public RequestContextStack requestContextStack;
38 public DeviceContext deviceContext;
39 public ConnectionAdapter primaryConnectionAdapter;
42 * @deprecated use {@link #CommonService(RequestContextStack, DeviceContext)}
45 public CommonService() {
48 public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
49 this.requestContextStack = requestContextStack;
51 this.deviceContext = deviceContext;
52 final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
53 this.datapathId = features.getDatapathId();
54 this.version = features.getVersion();
55 this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
58 protected long provideWaitTime() {
62 protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
63 if (connectionID == null) {
64 return primaryConnectionAdapter;
66 if (connectionID.equals(PRIMARY_CONNECTION)) {
67 return primaryConnectionAdapter;
70 final ConnectionContext auxiliaryConnectionContext =
71 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
72 if (auxiliaryConnectionContext != null) {
73 return auxiliaryConnectionContext.getConnectionAdapter();
76 return primaryConnectionAdapter;
79 public <T, F> Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
80 final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
81 LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
83 final RequestContext<T> requestContext = requestContextStack.createRequestContext();
84 final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
85 if (!result.isDone()) {
86 final DataCrate<T> dataCrate = DataCrateBuilder.<T>builder().setiDConnection(connectionID)
87 .setRequestContext(requestContext).build();
88 requestContext.setXid(deviceContext.getNextXid());
90 LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
91 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
93 final ListenableFuture<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
95 final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
96 OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
99 RequestContextUtil.closeRequstContext(requestContext);