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;
38 public CommonService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
39 this.requestContextStack = requestContextStack;
40 this.deviceContext = deviceContext;
41 final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
42 this.datapathId = features.getDatapathId();
43 this.version = features.getVersion();
44 this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
45 this.messageSpy = deviceContext.getMessageSpy();
48 public short getVersion() {
52 public BigInteger getDatapathId() {
56 public RequestContextStack getRequestContextStack() {
57 return requestContextStack;
60 public DeviceContext getDeviceContext() {
64 public MessageSpy getMessageSpy() {
69 public final <T> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<RequestContext<T>, ListenableFuture<RpcResult<T>>> function) {
71 LOG.trace("Handling general service call");
72 final RequestContext<T> requestContext = createRequestContext();
73 if (requestContext == null) {
74 LOG.trace("Request context refused.");
75 deviceContext.getMessageSpy().spyMessage(CommonService.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
76 return failedFuture();
79 if (requestContext.getXid() == null) {
80 deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
81 return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
84 messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
85 function.apply(requestContext);
87 return requestContext.getFuture();
91 protected final <T> RequestContext<T> createRequestContext() {
92 return requestContextStack.createRequestContext();
95 protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
96 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
97 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
98 return Futures.immediateFuture(rpcResult);