6edb9812193c891eea3e766e7bec15b31307b6ef
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-broker-impl / src / main / java / org / opendaylight / controller / sal / core / impl / RpcUtils.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.core.impl;\r
9 \r
10 import java.util.ArrayList;\r
11 import java.util.Collection;\r
12 import java.util.Collections;\r
13 import java.util.List;\r
14 import java.util.concurrent.Callable;\r
15 \r
16 import org.opendaylight.controller.sal.core.api.RpcImplementation;\r
17 import org.opendaylight.controller.yang.common.QName;\r
18 import org.opendaylight.controller.yang.common.RpcError;\r
19 import org.opendaylight.controller.yang.common.RpcResult;\r
20 import org.opendaylight.controller.yang.data.api.CompositeNode;\r
21 \r
22 \r
23 public class RpcUtils {\r
24 \r
25     Callable<RpcResult<CompositeNode>> callableFor(\r
26             final RpcImplementation implemenation, final QName rpc,\r
27             final CompositeNode input) {\r
28 \r
29         return new Callable<RpcResult<CompositeNode>>() {\r
30 \r
31             @Override\r
32             public RpcResult<CompositeNode> call() throws Exception {\r
33                 return implemenation.invokeRpc(rpc, input);\r
34             }\r
35         };\r
36     }\r
37 \r
38     public static <T> RpcResult<T> getRpcResult(boolean successful, T result,\r
39             List<RpcError> errors) {\r
40         RpcResult<T> ret = new RpcResultTO<T>(successful, result, errors);\r
41         return ret;\r
42     }\r
43 \r
44     private static class RpcResultTO<T> implements RpcResult<T> {\r
45 \r
46         private final Collection<RpcError> errors;\r
47         private final T result;\r
48         private final boolean successful;\r
49 \r
50         public RpcResultTO(boolean successful, T result, List<RpcError> errors) {\r
51             this.successful = successful;\r
52             this.result = result;\r
53             this.errors = Collections.unmodifiableList(new ArrayList<RpcError>(\r
54                     errors));\r
55         }\r
56 \r
57         @Override\r
58         public boolean isSuccessful() {\r
59             return successful;\r
60         }\r
61 \r
62         @Override\r
63         public T getResult() {\r
64             return result;\r
65         }\r
66 \r
67         @Override\r
68         public Collection<RpcError> getErrors() {\r
69             return errors;\r
70         }\r
71 \r
72     }\r
73 }\r
74