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.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
18 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
28 import org.opendaylight.yangtools.yang.common.RpcError;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 abstract class AbstractService<I, O> {
35 private static final Logger LOG = LoggerFactory.getLogger(AbstractService.class);
36 private static final long WAIT_TIME = 2000;
37 private static final BigInteger PRIMARY_CONNECTION = BigInteger.ZERO;
39 private final short version;
40 private final BigInteger datapathId;
41 private final RequestContextStack requestContextStack;
42 private final DeviceContext deviceContext;
43 private final ConnectionAdapter primaryConnectionAdapter;
44 private final MessageSpy messageSpy;
46 public AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
47 this.requestContextStack = requestContextStack;
48 this.deviceContext = deviceContext;
49 final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
50 this.datapathId = features.getDatapathId();
51 this.version = features.getVersion();
52 this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
53 this.messageSpy = deviceContext.getMessageSpy();
56 public short getVersion() {
60 public BigInteger getDatapathId() {
64 public RequestContextStack getRequestContextStack() {
65 return requestContextStack;
68 public DeviceContext getDeviceContext() {
72 public MessageSpy getMessageSpy() {
76 protected abstract OfHeader buildRequest(Xid xid, I input);
77 protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
79 public final ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input) {
80 Preconditions.checkNotNull(input);
82 final Class<?> requestType;
83 if (input instanceof DataContainer) {
84 requestType = ((DataContainer) input).getImplementedInterface();
86 requestType = input.getClass();
88 getMessageSpy().spyMessage(requestType, MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
90 LOG.trace("Handling general service call");
91 final RequestContext<O> requestContext = requestContextStack.createRequestContext();
92 if (requestContext == null) {
93 LOG.trace("Request context refused.");
94 deviceContext.getMessageSpy().spyMessage(AbstractService.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
95 return failedFuture();
98 if (requestContext.getXid() == null) {
99 deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
100 return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
103 messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
105 final Xid xid = requestContext.getXid();
106 OfHeader request = null;
108 request = buildRequest(xid, input);
109 Verify.verify(xid.getValue().equals(request.getXid()), "Expected XID %s got %s", xid.getValue(), request.getXid());
110 } catch (Exception e) {
111 LOG.error("Failed to build request for {}, forfeiting request {}", input, xid.getValue(), e);
112 // FIXME: complete the requestContext
114 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
115 outboundQueue.commitEntry(xid.getValue(), request, createCallback(requestContext, requestType));
118 return requestContext.getFuture();
121 protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
122 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
123 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
124 return Futures.immediateFuture(rpcResult);