fix for BUG-956 - deadlock by rpc invocation
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcFutureResultTransformFactory.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.core.sal;
9
10 import java.util.Collection;
11
12 import org.opendaylight.controller.sal.common.util.Rpcs;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.base.Function;
22
23 /**
24  * collection of transformation functions dedicated to rpc future results  
25  */
26 public abstract class OFRpcFutureResultTransformFactory {
27     
28     protected static Logger LOG = LoggerFactory
29             .getLogger(OFRpcFutureResultTransformFactory.class);
30
31     /**
32      * @return translator from {@link UpdateFlowOutput} to {@link AddFlowOutput}
33      */
34     public static Function<RpcResult<UpdateFlowOutput>,RpcResult<AddFlowOutput>> createForAddFlowOutput() {
35         return new Function<RpcResult<UpdateFlowOutput>,RpcResult<AddFlowOutput>>() {
36
37             @Override
38             public RpcResult<AddFlowOutput> apply(final RpcResult<UpdateFlowOutput> input) {
39
40                 UpdateFlowOutput updateFlowOutput = input.getResult();
41
42                 AddFlowOutputBuilder addFlowOutput = new AddFlowOutputBuilder();
43                 addFlowOutput.setTransactionId(updateFlowOutput.getTransactionId());
44                 AddFlowOutput result = addFlowOutput.build();
45
46                 RpcResult<AddFlowOutput> rpcResult = assembleRpcResult(input, result);
47                 LOG.debug("Returning the Add Flow RPC result to MD-SAL");
48                 return rpcResult;
49             }
50
51         };
52     }
53     
54     
55     /**
56      * @param input
57      * @param result
58      * @return
59      */
60     protected static <E> RpcResult<E> assembleRpcResult(RpcResult<?> input, E result) {
61         Collection<RpcError> errors = input.getErrors();
62         RpcResult<E> rpcResult = Rpcs.getRpcResult(input.isSuccessful(), result, errors);
63         return rpcResult;
64     }
65 }