Wrap service handlers to method handleServiceCall.
[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.SettableFuture;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14
15 import com.google.common.util.concurrent.Futures;
16 import java.math.BigInteger;
17 import java.util.concurrent.Future;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.slf4j.Logger;
27
28 abstract class CommonService {
29     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
30     private static final long WAIT_TIME = 2000;
31     protected final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
32             .<Void> failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
33     protected static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
34
35     // protected OFRpcTaskContext rpcTaskContext;
36     protected short version;
37     protected BigInteger datapathId;
38     protected RpcContext rpcContext;
39     protected DeviceContext deviceContext;
40     private ConnectionAdapter primaryConnectionAdapter;
41
42     CommonService() {
43     }
44
45     public CommonService(final RpcContext rpcContext) {
46         this.rpcContext = rpcContext;
47
48         this.deviceContext = rpcContext.getDeviceContext();
49         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
50         this.datapathId = features.getDatapathId();
51         this.version = features.getVersion();
52         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
53     }
54
55     protected long provideWaitTime() {
56         return WAIT_TIME;
57     }
58
59     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
60         if (connectionID == null) {
61             return primaryConnectionAdapter;
62         }
63         if (connectionID.equals(PRIMARY_CONNECTION)) {
64             return primaryConnectionAdapter;
65         }
66
67         // TODO uncomment when getAuxiali.... will be merged to APIs
68         // final ConnectionContext auxiliaryConnectionContext =
69         // deviceContext.getAuxiliaryConnectionContext(connectionID);
70         final ConnectionContext auxiliaryConnectionContext = null;
71         if (auxiliaryConnectionContext != null) {
72             return auxiliaryConnectionContext.getConnectionAdapter();
73         }
74
75         return primaryConnectionAdapter;
76     }
77
78     <T extends DataObject, F>  Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
79             final Function<BigInteger,Future<RpcResult<F>>> function) {
80         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
81
82         final RequestContext<T> requestContext = rpcContext.createRequestContext();
83         final SettableFuture<RpcResult<T>> result = rpcContext.storeOrFail(requestContext);
84
85         if (!result.isDone()) {
86             final Future<RpcResult<F>> resultFromOFLib = function.apply(connectionID);
87
88             final RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext, deviceContext);
89             rpcResultConvertor.processResultFromOfJava(resultFromOFLib);
90
91         } else {
92             RequestContextUtil.closeRequstContext(requestContext);
93         }
94         return result;
95     }
96
97 }