72d32944768d59e6e2e7896ac10a1d184dae3ba8
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / ServiceCallProcessingUtil.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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
11
12 import com.google.common.base.Function;
13 import com.google.common.util.concurrent.SettableFuture;
14 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
15 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.slf4j.Logger;
19 import java.math.BigInteger;
20 import java.util.concurrent.Future;
21
22 /**
23  * @author joe
24  *
25  */
26 public class ServiceCallProcessingUtil {
27
28     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(ServiceCallProcessingUtil.class);
29
30     static <T extends DataObject, F>  Future<RpcResult<T>> handleServiceCall(final RpcContext rpcContext,
31 final BigInteger connectionID, final DeviceContext deviceContext, final Function<BigInteger,Future<RpcResult<F>>> function) {
32         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
33         // use primary connection
34
35         final RequestContext<T> requestContext = rpcContext.createRequestContext();
36         final SettableFuture<RpcResult<T>> result = rpcContext.storeOrFail(requestContext);
37
38         if (!result.isDone()) {
39             final Future<RpcResult<F>> resultFromOFLib = function.apply(connectionID);
40
41             final RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext, deviceContext);
42             rpcResultConvertor.processResultFromOfJava(resultFromOFLib);
43
44         } else {
45             RequestContextUtil.closeRequstContext(requestContext);
46         }
47         return result;
48     }
49 }