Merge "Add NodeConfiguratorImpl enqueue trace"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / AbstractService.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
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;
38
39 public abstract class AbstractService<I, O> {
40     private static final Logger LOG = LoggerFactory.getLogger(AbstractService.class);
41
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;
48
49     AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
50         final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
51
52         this.requestContextStack = requestContextStack;
53         this.deviceContext = deviceContext;
54         this.datapathId = deviceInfo.getDatapathId();
55         this.version = deviceInfo.getVersion();
56         this.messageSpy = deviceContext.getMessageSpy();
57     }
58
59     public boolean canUseSingleLayerSerialization() {
60         return deviceContext.canUseSingleLayerSerialization();
61     }
62
63     public EventIdentifier getEventIdentifier() {
64         return eventIdentifier;
65     }
66
67     public void setEventIdentifier(final EventIdentifier eventIdentifier) {
68         this.eventIdentifier = eventIdentifier;
69     }
70
71     public short getVersion() {
72         return version;
73     }
74
75     public BigInteger getDatapathId() {
76         return datapathId;
77     }
78
79     public RequestContextStack getRequestContextStack() {
80         return requestContextStack;
81     }
82
83     @Deprecated
84     public DeviceContext getDeviceContext() {
85         return deviceContext;
86     }
87
88     public DeviceRegistry getDeviceRegistry() {
89         return deviceContext;
90     }
91
92     public DeviceInfo getDeviceInfo() {
93         return deviceContext.getDeviceInfo();
94     }
95
96     public TxFacade getTxFacade() {
97         return deviceContext;
98     }
99
100     public MessageSpy getMessageSpy() {
101         return messageSpy;
102     }
103
104     protected abstract OfHeader buildRequest(Xid xid, I input) throws ServiceException;
105
106     protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
107
108     public ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input) {
109         return handleServiceCall(input, null);
110     }
111
112     public ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input,
113             @Nullable final Function<OfHeader, Boolean> isComplete) {
114         Preconditions.checkNotNull(input);
115
116         final Class<?> requestType = input instanceof DataContainer
117             ? ((DataContainer) input).implementedInterface()
118             : input.getClass();
119
120         getMessageSpy().spyMessage(requestType, MessageSpy.StatisticsGroup.TO_SWITCH_ENTERED);
121
122         LOG.trace("Handling general service call");
123         final RequestContext<O> requestContext = requestContextStack.createRequestContext();
124
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
129                     .<O>failed()
130                     .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded")
131                     .build());
132         }
133
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.");
139         }
140
141         getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.StatisticsGroup.TO_SWITCH_READY_FOR_SUBMIT);
142
143         final Xid xid = requestContext.getXid();
144         OfHeader request = null;
145         try {
146             request = buildRequest(xid, input);
147             Verify.verify(xid.getValue().equals(request.getXid()),
148                           "Expected XID %s got %s",
149                           xid.getValue(),
150                           request.getXid());
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());
155         } finally {
156             final OutboundQueue outboundQueue =
157                     getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
158
159             if (isComplete != null) {
160                 outboundQueue.commitEntry(xid.getValue(),
161                                           request,
162                                           createCallback(requestContext, requestType), isComplete);
163             } else {
164                 outboundQueue.commitEntry(xid.getValue(), request, createCallback(requestContext, requestType));
165             }
166         }
167
168         return requestContext.getFuture();
169     }
170 }