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 java.math.BigInteger;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.slf4j.Logger;
26 public abstract class CommonService {
27 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
28 private static final long WAIT_TIME = 2000;
29 private static final BigInteger PRIMARY_CONNECTION = BigInteger.ZERO;
31 private final short version;
32 private final BigInteger datapathId;
33 private final RequestContextStack requestContextStack;
34 private final DeviceContext deviceContext;
35 private final ConnectionAdapter primaryConnectionAdapter;
36 private final MessageSpy messageSpy;
39 public CommonService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
40 this.requestContextStack = requestContextStack;
41 this.deviceContext = deviceContext;
42 final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
43 this.datapathId = features.getDatapathId();
44 this.version = features.getVersion();
45 this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
46 this.messageSpy = deviceContext.getMessageSpy();
49 public static BigInteger getPrimaryConnection() {
50 return PRIMARY_CONNECTION;
53 public short getVersion() {
57 public BigInteger getDatapathId() {
61 public RequestContextStack getRequestContextStack() {
62 return requestContextStack;
65 public DeviceContext getDeviceContext() {
69 public ConnectionAdapter getPrimaryConnectionAdapter() {
70 return primaryConnectionAdapter;
73 public MessageSpy getMessageSpy() {
77 protected long provideWaitTime() {
82 protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
83 if (connectionID == null) {
84 return primaryConnectionAdapter;
86 if (connectionID.equals(PRIMARY_CONNECTION)) {
87 return primaryConnectionAdapter;
90 final ConnectionContext auxiliaryConnectionContext =
91 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
92 if (auxiliaryConnectionContext != null) {
93 return auxiliaryConnectionContext.getConnectionAdapter();
96 return primaryConnectionAdapter;
106 public final <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<RequestContext<T>, ListenableFuture<RpcResult<F>>> function) {
108 LOG.trace("Handling general service call");
109 final RequestContext<T> requestContext = createRequestContext();
110 if (requestContext == null) {
111 LOG.trace("Request context refused.");
112 deviceContext.getMessageSpy().spyMessage(CommonService.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
113 return failedFuture();
116 if (requestContext.getXid() == null) {
117 deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
118 return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
120 final ListenableFuture<RpcResult<F>> resultFromOFLib;
122 messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
123 function.apply(requestContext);
125 return requestContext.getFuture();
129 protected final <T> RequestContext<T> createRequestContext() {
130 return requestContextStack.createRequestContext();
133 protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
134 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
135 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
136 return Futures.immediateFuture(rpcResult);