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 java.util.function.Function;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceRegistry;
23 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
25 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
26 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
28 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
29 import org.opendaylight.openflowplugin.impl.services.util.RequestContextUtil;
30 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yangtools.yang.binding.DataContainer;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public abstract class AbstractService<I, O> {
40 private static final Logger LOG = LoggerFactory.getLogger(AbstractService.class);
42 private final short version;
43 private final BigInteger datapathId;
44 private final RequestContextStack requestContextStack;
45 private final DeviceContext deviceContext;
46 private final MessageSpy messageSpy;
47 private EventIdentifier eventIdentifier;
49 AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
50 final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
52 this.requestContextStack = requestContextStack;
53 this.deviceContext = deviceContext;
54 this.datapathId = deviceInfo.getDatapathId();
55 this.version = deviceInfo.getVersion();
56 this.messageSpy = deviceContext.getMessageSpy();
59 public boolean canUseSingleLayerSerialization() {
60 return deviceContext.canUseSingleLayerSerialization();
63 public EventIdentifier getEventIdentifier() {
64 return eventIdentifier;
67 public void setEventIdentifier(final EventIdentifier eventIdentifier) {
68 this.eventIdentifier = eventIdentifier;
71 public short getVersion() {
75 public BigInteger getDatapathId() {
79 public RequestContextStack getRequestContextStack() {
80 return requestContextStack;
84 public DeviceContext getDeviceContext() {
88 public DeviceRegistry getDeviceRegistry() {
92 public DeviceInfo getDeviceInfo() {
93 return deviceContext.getDeviceInfo();
96 public TxFacade getTxFacade() {
100 public MessageSpy getMessageSpy() {
104 protected abstract OfHeader buildRequest(Xid xid, I input) throws ServiceException;
106 protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
108 public ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input) {
109 return handleServiceCall(input, null);
112 public ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input,
113 @Nullable final Function<OfHeader, Boolean> isComplete) {
114 Preconditions.checkNotNull(input);
116 final Class<?> requestType = input instanceof DataContainer
117 ? ((DataContainer) input).implementedInterface()
120 getMessageSpy().spyMessage(requestType, MessageSpy.StatisticsGroup.TO_SWITCH_ENTERED);
122 LOG.trace("Handling general service call");
123 final RequestContext<O> requestContext = requestContextStack.createRequestContext();
125 if (requestContext == null) {
126 LOG.trace("Request context refused.");
127 getMessageSpy().spyMessage(AbstractService.class, MessageSpy.StatisticsGroup.TO_SWITCH_DISREGARDED);
128 return Futures.immediateFuture(RpcResultBuilder
130 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded")
134 if (requestContext.getXid() == null) {
135 getMessageSpy().spyMessage(requestContext.getClass(),
136 MessageSpy.StatisticsGroup.TO_SWITCH_RESERVATION_REJECTED);
137 return RequestContextUtil
138 .closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
141 getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.StatisticsGroup.TO_SWITCH_READY_FOR_SUBMIT);
143 final Xid xid = requestContext.getXid();
144 OfHeader request = null;
146 request = buildRequest(xid, input);
147 Verify.verify(xid.getValue().equals(request.getXid()),
148 "Expected XID %s got %s",
151 } catch (ServiceException ex) {
152 LOG.error("Failed to build request for {}, forfeiting request {}", input, xid.getValue(), ex);
153 RequestContextUtil.closeRequestContextWithRpcError(requestContext,
154 "failed to build request input: " + ex.getMessage());
156 final OutboundQueue outboundQueue =
157 getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
159 if (isComplete != null) {
160 outboundQueue.commitEntry(xid.getValue(),
162 createCallback(requestContext, requestType), isComplete);
164 outboundQueue.commitEntry(xid.getValue(), request, createCallback(requestContext, requestType));
168 return requestContext.getFuture();