Make sure RequestContext has a constant XID
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / CommonService.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.Function;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.math.BigInteger;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.slf4j.Logger;
25
26 public abstract class CommonService {
27     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
28     private static final long WAIT_TIME = 2000;
29     private static final BigInteger PRIMARY_CONNECTION = BigInteger.ZERO;
30
31     private final short version;
32     private final BigInteger datapathId;
33     private final RequestContextStack requestContextStack;
34     private final DeviceContext deviceContext;
35     private final ConnectionAdapter primaryConnectionAdapter;
36     private final MessageSpy messageSpy;
37
38
39     public CommonService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
40         this.requestContextStack = requestContextStack;
41         this.deviceContext = deviceContext;
42         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
43         this.datapathId = features.getDatapathId();
44         this.version = features.getVersion();
45         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
46         this.messageSpy = deviceContext.getMessageSpy();
47     }
48
49     public static BigInteger getPrimaryConnection() {
50         return PRIMARY_CONNECTION;
51     }
52
53     public short getVersion() {
54         return version;
55     }
56
57     public BigInteger getDatapathId() {
58         return datapathId;
59     }
60
61     public RequestContextStack getRequestContextStack() {
62         return requestContextStack;
63     }
64
65     public DeviceContext getDeviceContext() {
66         return deviceContext;
67     }
68
69     public ConnectionAdapter getPrimaryConnectionAdapter() {
70         return primaryConnectionAdapter;
71     }
72
73     public MessageSpy getMessageSpy() {
74         return messageSpy;
75     }
76
77     protected long provideWaitTime() {
78         return WAIT_TIME;
79     }
80
81
82     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
83         if (connectionID == null) {
84             return primaryConnectionAdapter;
85         }
86         if (connectionID.equals(PRIMARY_CONNECTION)) {
87             return primaryConnectionAdapter;
88         }
89
90         final ConnectionContext auxiliaryConnectionContext =
91                 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
92         if (auxiliaryConnectionContext != null) {
93             return auxiliaryConnectionContext.getConnectionAdapter();
94         }
95
96         return primaryConnectionAdapter;
97     }
98
99
100     /**
101      * @param <T>
102      * @param <F>
103      * @param function
104      * @return
105      */
106     public final <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<RequestContext<T>, ListenableFuture<RpcResult<F>>> function) {
107
108         LOG.trace("Handling general service call");
109         final RequestContext<T> requestContext = createRequestContext();
110         if (requestContext == null) {
111             LOG.trace("Request context refused.");
112             deviceContext.getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
113             return failedFuture();
114         }
115
116         if (requestContext.getXid() == null) {
117             deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
118             return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
119         }
120         final ListenableFuture<RpcResult<F>> resultFromOFLib;
121
122         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
123         deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
124
125         messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
126         function.apply(requestContext);
127
128         return requestContext.getFuture();
129
130     }
131
132     protected final <T> RequestContext<T> createRequestContext() {
133         return requestContextStack.createRequestContext();
134     }
135
136     protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
137         final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
138                 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
139         return Futures.immediateFuture(rpcResult);
140     }
141 }