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.SettableFuture;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import com.google.common.util.concurrent.Futures;
15 import java.math.BigInteger;
16 import java.util.concurrent.Future;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
18 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
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 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;
35 protected short version;
36 protected BigInteger datapathId;
37 protected RpcContext rpcContext;
38 protected DeviceContext deviceContext;
39 private ConnectionAdapter primaryConnectionAdapter;
44 public CommonService(final RpcContext rpcContext) {
45 this.rpcContext = rpcContext;
47 this.deviceContext = rpcContext.getDeviceContext();
48 final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
49 this.datapathId = features.getDatapathId();
50 this.version = features.getVersion();
51 this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
54 protected long provideWaitTime() {
58 protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
59 if (connectionID == null) {
60 return primaryConnectionAdapter;
62 if (connectionID.equals(PRIMARY_CONNECTION)) {
63 return primaryConnectionAdapter;
66 // TODO uncomment when getAuxiali.... will be merged to APIs
67 // final ConnectionContext auxiliaryConnectionContext =
68 // deviceContext.getAuxiliaryConnectionContext(connectionID);
69 final ConnectionContext auxiliaryConnectionContext = null;
70 if (auxiliaryConnectionContext != null) {
71 return auxiliaryConnectionContext.getConnectionAdapter();
74 return primaryConnectionAdapter;
77 <T extends DataObject, F> Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
78 final Function<DataCrate<T>, Future<RpcResult<F>>> function) {
79 LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
81 final RequestContext<T> requestContext = rpcContext.createRequestContext();
82 final SettableFuture<RpcResult<T>> result = rpcContext.storeOrFail(requestContext);
83 final DataCrate<T> dataCrate = DataCrateBuilder.<T> builder().setiDConnection(connectionID)
84 .setRequestContext(requestContext).build();
85 if (!result.isDone()) {
86 final Future<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
88 final RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext, deviceContext);
89 rpcResultConvertor.processResultFromOfJava(resultFromOFLib);
92 RequestContextUtil.closeRequstContext(requestContext);