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.OutboundQueue;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceRegistry;
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.TxFacade;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 abstract class AbstractService<I, O> {
36 private static final Logger LOG = LoggerFactory.getLogger(AbstractService.class);
37 private static final long WAIT_TIME = 2000;
38 private static final BigInteger PRIMARY_CONNECTION = BigInteger.ZERO;
40 private final short version;
41 private final BigInteger datapathId;
42 private final RequestContextStack requestContextStack;
43 private final DeviceContext deviceContext;
44 private final MessageSpy messageSpy;
45 private EventIdentifier eventIdentifier;
47 AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
48 final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
50 this.requestContextStack = requestContextStack;
51 this.deviceContext = deviceContext;
52 this.datapathId = deviceInfo.getDatapathId();
53 this.version = deviceInfo.getVersion();
54 this.messageSpy = deviceContext.getMessageSpy();
57 public EventIdentifier getEventIdentifier() {
58 return eventIdentifier;
61 public void setEventIdentifier(final EventIdentifier eventIdentifier) {
62 this.eventIdentifier = eventIdentifier;
65 public short getVersion() {
69 public BigInteger getDatapathId() {
73 public RequestContextStack getRequestContextStack() {
74 return requestContextStack;
78 public DeviceContext getDeviceContext() {
82 protected DeviceRegistry getDeviceRegistry() {return deviceContext;}
84 public DeviceInfo getDeviceInfo() {return deviceContext.getDeviceInfo();}
86 public TxFacade getTxFacade() {return deviceContext;}
88 public MessageSpy getMessageSpy() {
92 protected abstract OfHeader buildRequest(Xid xid, I input) throws Exception;
94 protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
96 public final ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input) {
97 Preconditions.checkNotNull(input);
99 final Class<?> requestType;
100 if (input instanceof DataContainer) {
101 requestType = ((DataContainer) input).getImplementedInterface();
103 requestType = input.getClass();
105 getMessageSpy().spyMessage(requestType, MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
107 LOG.trace("Handling general service call");
108 final RequestContext<O> requestContext = requestContextStack.createRequestContext();
109 if (requestContext == null) {
110 LOG.trace("Request context refused.");
111 getMessageSpy().spyMessage(AbstractService.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
112 return failedFuture();
115 if (requestContext.getXid() == null) {
116 getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
117 return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
120 getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
122 final Xid xid = requestContext.getXid();
123 OfHeader request = null;
125 request = buildRequest(xid, input);
126 Verify.verify(xid.getValue().equals(request.getXid()), "Expected XID %s got %s", xid.getValue(), request.getXid());
127 } catch (Exception e) {
128 LOG.error("Failed to build request for {}, forfeiting request {}", input, xid.getValue(), e);
129 RequestContextUtil.closeRequestContextWithRpcError(requestContext, "failed to build request input: " + e.getMessage());
131 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
132 outboundQueue.commitEntry(xid.getValue(), request, createCallback(requestContext, requestType));
135 return requestContext.getFuture();
138 protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
139 final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
140 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
141 return Futures.immediateFuture(rpcResult);