Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / connect / dom / RpcInvocationStrategy.java
1 /*
2  ** Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.controller.sal.binding.impl.connect.dom;
10
11 import java.lang.ref.WeakReference;
12 import java.lang.reflect.Method;
13 import java.util.Collections;
14 import java.util.concurrent.Future;
15
16 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
20 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.Node;
26 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
27 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
28
29 import com.google.common.base.Function;
30 import com.google.common.base.Optional;
31 import com.google.common.collect.ImmutableList;
32 import com.google.common.util.concurrent.Futures;
33 import com.google.common.util.concurrent.ListenableFuture;
34
35 /*
36  * RPC's can have both input, output, one or the other, or neither.
37  *
38  * This class handles the permutations and provides two means of invocation:
39  * 1. forwardToDomBroker
40  * 2.
41  *
42  * Weak References to the input and output classes are used to allow these classes to
43  * be from another OSGi bundle/class loader which may come and go.
44  *
45  */
46 public class RpcInvocationStrategy {
47
48     private final BindingIndependentMappingService mappingService;
49     private final RpcProvisionRegistry biRpcRegistry;
50     protected final Method targetMethod;
51     protected final QName rpc;
52
53     @SuppressWarnings("rawtypes")
54     private final WeakReference<Class> inputClass;
55
56     @SuppressWarnings("rawtypes")
57     private final WeakReference<Class> outputClass;
58
59     @SuppressWarnings({ "rawtypes" })
60     public RpcInvocationStrategy(final QName rpc,
61                                  final Method targetMethod,
62                                  final BindingIndependentMappingService mappingService,
63                                  final RpcProvisionRegistry biRpcRegistry ) {
64
65         this.targetMethod = targetMethod;
66         this.rpc = rpc;
67
68         Optional<Class<?>> outputClassOption = BindingReflections.resolveRpcOutputClass(targetMethod);
69         Optional<Class<? extends DataContainer>> inputClassOption = BindingReflections.resolveRpcInputClass(targetMethod);
70
71         if ( outputClassOption != null && outputClassOption.isPresent() ) {
72             this.outputClass = new WeakReference(outputClassOption.get() ) ;
73         } else {
74             this.outputClass = null ;
75         }
76         if ( inputClassOption != null && inputClassOption.isPresent() ) {
77             this.inputClass = new WeakReference(inputClassOption.get() ) ;
78         } else {
79             this.inputClass = null ;
80         }
81
82         this.mappingService = mappingService;
83         this.biRpcRegistry = biRpcRegistry;
84     }
85
86     @SuppressWarnings({ "unchecked" })
87     public ListenableFuture<RpcResult<?>> forwardToDomBroker(final DataObject input) {
88
89         if(biRpcRegistry == null) {
90             return Futures.<RpcResult<?>> immediateFuture(RpcResultBuilder.failed().build());
91         }
92
93         CompositeNode inputXml = null;
94         if( input != null ) {
95             CompositeNode xml = mappingService.toDataDom(input);
96             inputXml = ImmutableCompositeNode.create(rpc, ImmutableList.<Node<?>> of(xml));
97         } else {
98             inputXml = ImmutableCompositeNode.create( rpc, Collections.<Node<?>>emptyList() );
99         }
100
101         Function<RpcResult<CompositeNode>, RpcResult<?>> transformationFunction =
102                                        new Function<RpcResult<CompositeNode>, RpcResult<?>>() {
103             @SuppressWarnings("rawtypes")
104             @Override
105             public RpcResult<?> apply(RpcResult<CompositeNode> result) {
106
107                 Object output = null;
108
109                 if( getOutputClass() != null ) {
110                     if (result.getResult() != null) {
111                         output = mappingService.dataObjectFromDataDom(getOutputClass().get(),
112                                                                     result.getResult());
113                     }
114                 }
115
116                 return RpcResultBuilder.from( (RpcResult)result ).withResult( output ).build();
117             }
118         };
119
120         return Futures.transform(biRpcRegistry.invokeRpc(rpc, inputXml), transformationFunction);
121     }
122
123     @SuppressWarnings("unchecked")
124     private RpcResult<CompositeNode> uncheckedInvoke(final RpcService rpcService, final CompositeNode domInput) throws Exception {
125
126         Future<RpcResult<?>> futureResult = null;
127
128         if( inputClass != null ){
129             DataContainer bindingInput = mappingService.dataObjectFromDataDom(inputClass.get(), domInput);
130             futureResult = (Future<RpcResult<?>>) targetMethod.invoke(rpcService, bindingInput);
131
132         } else {
133             futureResult = (Future<RpcResult<?>>) targetMethod.invoke(rpcService);
134         }
135
136         if (futureResult == null) {
137             return RpcResultBuilder.<CompositeNode>failed().build();
138         }
139
140         @SuppressWarnings("rawtypes")
141         RpcResult bindingResult = futureResult.get();
142
143         final Object resultObj = bindingResult.getResult();
144         Object output = null;
145         if (resultObj instanceof DataObject) {
146             output = mappingService.toDataDom((DataObject)resultObj);
147         }
148         return RpcResultBuilder.from( bindingResult ).withResult( output ).build();
149     }
150
151     public RpcResult<CompositeNode> invokeOn(final RpcService rpcService, final CompositeNode domInput) throws Exception {
152         return uncheckedInvoke(rpcService, domInput);
153     }
154
155     @SuppressWarnings("rawtypes")
156     public WeakReference<Class> getOutputClass() {
157         return outputClass;
158     }
159 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.