Make sure RpcResultBuilder is local
[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     public CommonService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
39         this.requestContextStack = requestContextStack;
40         this.deviceContext = deviceContext;
41         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
42         this.datapathId = features.getDatapathId();
43         this.version = features.getVersion();
44         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
45         this.messageSpy = deviceContext.getMessageSpy();
46     }
47
48     public short getVersion() {
49         return version;
50     }
51
52     public BigInteger getDatapathId() {
53         return datapathId;
54     }
55
56     public RequestContextStack getRequestContextStack() {
57         return requestContextStack;
58     }
59
60     public DeviceContext getDeviceContext() {
61         return deviceContext;
62     }
63
64     public MessageSpy getMessageSpy() {
65         return messageSpy;
66     }
67
68
69    public final <T> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<RequestContext<T>, ListenableFuture<RpcResult<T>>> function) {
70
71         LOG.trace("Handling general service call");
72         final RequestContext<T> requestContext = createRequestContext();
73         if (requestContext == null) {
74             LOG.trace("Request context refused.");
75             deviceContext.getMessageSpy().spyMessage(CommonService.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
76             return failedFuture();
77         }
78
79         if (requestContext.getXid() == null) {
80             deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
81             return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
82         }
83
84         messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
85         function.apply(requestContext);
86
87         return requestContext.getFuture();
88
89     }
90
91     protected final <T> RequestContext<T> createRequestContext() {
92         return requestContextStack.createRequestContext();
93     }
94
95     protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
96         final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
97                 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
98         return Futures.immediateFuture(rpcResult);
99     }
100 }