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.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.slf4j.Logger;
28 public abstract class CommonService {
29 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
30 private static final long WAIT_TIME = 2000;
31 protected final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
32 .<Void>failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
33 protected static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
35 // protected OFRpcTaskContext rpcTaskContext;
37 public BigInteger datapathId;
38 public RequestContextStack requestContextStack;
39 public DeviceContext deviceContext;
40 public ConnectionAdapter primaryConnectionAdapter;
41 public MessageSpy messageSpy;
45 * @deprecated use {@link #CommonService(RequestContextStack, DeviceContext)}
48 public CommonService() {
51 public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
52 this.requestContextStack = requestContextStack;
54 this.deviceContext = deviceContext;
55 final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
56 this.datapathId = features.getDatapathId();
57 this.version = features.getVersion();
58 this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
59 this.messageSpy = deviceContext.getMessageSpy();
62 protected long provideWaitTime() {
66 protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
67 if (connectionID == null) {
68 return primaryConnectionAdapter;
70 if (connectionID.equals(PRIMARY_CONNECTION)) {
71 return primaryConnectionAdapter;
74 final ConnectionContext auxiliaryConnectionContext =
75 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
76 if (auxiliaryConnectionContext != null) {
77 return auxiliaryConnectionContext.getConnectionAdapter();
80 return primaryConnectionAdapter;
84 * @param connectionID connection identifier
85 * @param function data sender
86 * @param <T> rpc result backend type
87 * @param <F> final rpc backend type
90 public <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
91 final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
92 DataCrateBuilder<T> dataCrateBuilder = DataCrateBuilder.<T>builder();
93 return handleServiceCall(connectionID, function, dataCrateBuilder);
101 * @param dataCrateBuilder predefined data
104 public <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
105 final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function,
106 final DataCrateBuilder<T> dataCrateBuilder) {
107 LOG.debug("Handling general service call");
109 final RequestContext<T> requestContext = requestContextStack.createRequestContext();
110 final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
111 if (!result.isDone()) {
112 DataCrate<T> dataCrate = dataCrateBuilder.setiDConnection(connectionID).setRequestContext(requestContext)
114 requestContext.setXid(deviceContext.getNextXid());
116 LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
117 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
119 final ListenableFuture<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
121 final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
122 OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
125 messageSpy.spyMessage(requestContext, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);