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.SettableFuture;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import java.math.BigInteger;
25 import java.util.concurrent.Future;
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;
41 public CommonService() {
44 public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
45 this.requestContextStack = requestContextStack;
47 this.deviceContext = deviceContext;
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 public <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 = requestContextStack.createRequestContext();
82 final SettableFuture<RpcResult<T>> result = requestContextStack.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);