Merge "Bug 5925 - Reuse Threads using ThreadPool in ConnectionManagerImpl"
[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 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;
34
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;
39
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;
46
47     AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
48         final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
49
50         this.requestContextStack = requestContextStack;
51         this.deviceContext = deviceContext;
52         this.datapathId = deviceInfo.getDatapathId();
53         this.version = deviceInfo.getVersion();
54         this.messageSpy = deviceContext.getMessageSpy();
55     }
56
57     public EventIdentifier getEventIdentifier() {
58         return eventIdentifier;
59     }
60
61     public void setEventIdentifier(final EventIdentifier eventIdentifier) {
62         this.eventIdentifier = eventIdentifier;
63     }
64
65     public short getVersion() {
66         return version;
67     }
68
69     public BigInteger getDatapathId() {
70         return datapathId;
71     }
72
73     public RequestContextStack getRequestContextStack() {
74         return requestContextStack;
75     }
76
77     @Deprecated
78     public DeviceContext getDeviceContext() {
79         return deviceContext;
80     }
81
82     protected DeviceRegistry getDeviceRegistry() {return deviceContext;}
83
84     public DeviceInfo getDeviceInfo() {return deviceContext.getDeviceInfo();}
85
86     public TxFacade getTxFacade() {return deviceContext;}
87
88     public MessageSpy getMessageSpy() {
89         return messageSpy;
90     }
91
92     protected abstract OfHeader buildRequest(Xid xid, I input) throws Exception;
93
94     protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
95
96     public final ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input) {
97         Preconditions.checkNotNull(input);
98
99         final Class<?> requestType;
100         if (input instanceof DataContainer) {
101             requestType = ((DataContainer) input).getImplementedInterface();
102         } else {
103             requestType = input.getClass();
104         }
105         getMessageSpy().spyMessage(requestType, MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
106
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();
113         }
114
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.");
118         }
119
120         getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
121
122         final Xid xid = requestContext.getXid();
123         OfHeader request = null;
124         try {
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());
130         } finally {
131             final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
132             outboundQueue.commitEntry(xid.getValue(), request, createCallback(requestContext, requestType));
133         }
134
135         return requestContext.getFuture();
136     }
137
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);
142     }
143 }