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 private final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
32 .<Void>failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
35 private static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
37 private final short version;
38 private final BigInteger datapathId;
39 private final RequestContextStack requestContextStack;
40 private final DeviceContext deviceContext;
41 private final ConnectionAdapter primaryConnectionAdapter;
42 private final MessageSpy messageSpy;
45 public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
46 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();
52 this.messageSpy = deviceContext.getMessageSpy();
54 public static BigInteger getPrimaryConnection() {
55 return PRIMARY_CONNECTION;
58 public short getVersion(){
62 public BigInteger getDatapathId() {
66 public RequestContextStack getRequestContextStack() {
67 return requestContextStack;
70 public DeviceContext getDeviceContext() {
74 public ConnectionAdapter getPrimaryConnectionAdapter() {
75 return primaryConnectionAdapter;
78 public MessageSpy getMessageSpy() {
82 protected long provideWaitTime() {
87 protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
88 if (connectionID == null) {
89 return primaryConnectionAdapter;
91 if (connectionID.equals(PRIMARY_CONNECTION)) {
92 return primaryConnectionAdapter;
95 final ConnectionContext auxiliaryConnectionContext =
96 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
97 if (auxiliaryConnectionContext != null) {
98 return auxiliaryConnectionContext.getConnectionAdapter();
101 return primaryConnectionAdapter;
105 * @param connectionID connection identifier
106 * @param function data sender
107 * @param <T> rpc result backend type
108 * @param <F> final rpc backend type
111 public <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
112 final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
113 DataCrateBuilder<T> dataCrateBuilder = DataCrateBuilder.<T>builder();
114 return handleServiceCall(connectionID, function, dataCrateBuilder);
116 public <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
117 DataCrateBuilder<T> dataCrateBuilder = DataCrateBuilder.<T>builder();
118 return handleServiceCall(PRIMARY_CONNECTION, function, dataCrateBuilder);
124 * @param connectionID
126 * @param dataCrateBuilder predefined data
129 public final <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
130 final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function,
131 final DataCrateBuilder<T> dataCrateBuilder) {
133 LOG.trace("Handling general service call");
134 final RequestContext<T> requestContext = requestContextStack.createRequestContext();
135 final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
136 if (result.isDone()) {
137 messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
140 DataCrate<T> dataCrate = dataCrateBuilder.setiDConnection(connectionID).setRequestContext(requestContext)
142 final ListenableFuture<RpcResult<F>> resultFromOFLib;
144 requestContext.setXid(deviceContext.getNextXid());
145 LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
146 deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
147 synchronized (deviceContext) {
148 resultFromOFLib = function.apply(dataCrate);
152 final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
153 OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);