Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / rpc / ModuleRpcs.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.rpc;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
14 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
15
16 import java.util.Map;
17
18 public final class ModuleRpcs {
19
20     private final Map<String, String> yangToJavaNames = Maps.newHashMap();
21     private final Map<String, Map<String, InstanceRuntimeRpc>> rpcMapping = Maps.newHashMap();
22
23     public void addNameMapping(RuntimeBeanEntry runtimeEntry) {
24         String yangName = runtimeEntry.getYangName();
25         Preconditions.checkState(!yangToJavaNames.containsKey(yangName),
26                 "RuntimeBean %s found twice in same namespace", yangName);
27         yangToJavaNames.put(yangName, runtimeEntry.getJavaNamePrefix());
28     }
29
30     public void addRpc(RuntimeBeanEntry runtimeEntry, Rpc rpc) {
31         String yangName = runtimeEntry.getYangName();
32         Map<String, InstanceRuntimeRpc> map = rpcMapping.get(yangName);
33         if (map == null) {
34             map = Maps.newHashMap();
35             rpcMapping.put(yangName, map);
36         }
37
38         Preconditions.checkState(!map.containsKey(rpc.getYangName()), "Rpc %s for runtime bean %s added twice",
39                 rpc.getYangName(), yangName);
40         map.put(rpc.getYangName(), new InstanceRuntimeRpc(rpc));
41     }
42
43     public String getRbeJavaName(String yangName) {
44         String javaName = yangToJavaNames.get(yangName);
45         Preconditions.checkState(javaName != null,
46                 "No runtime bean entry found under yang name %s, available yang names %s", yangName,
47                 yangToJavaNames.keySet());
48         return javaName;
49     }
50
51     public InstanceRuntimeRpc getRpc(String rbeName, String rpcName) {
52         Map<String, InstanceRuntimeRpc> rpcs = rpcMapping.get(rbeName);
53         Preconditions.checkState(rpcs != null, "No rpcs found for runtime bean %s", rbeName);
54         InstanceRuntimeRpc rpc = rpcs.get(rpcName);
55         Preconditions.checkState(rpc != null, "No rpc found for runtime bean %s with name %s", rbeName, rpcName);
56         return rpc;
57     }
58
59     public Map<String, String> getYangToJavaNames() {
60         return yangToJavaNames;
61     }
62
63     public Map<String, Map<String, InstanceRuntimeRpc>> getRpcMapping() {
64         return rpcMapping;
65     }
66 }