5e44427da3ce4ba006a1c6085466bbb05d28f22b
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / get / Get.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.operations.get;
10
11 import com.google.common.collect.Maps;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import javax.management.ObjectName;
16 import org.opendaylight.controller.config.util.ConfigRegistryClient;
17 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
18 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleConfig;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.InstanceRuntime;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.ModuleRuntime;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.Runtime;
25 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
26 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
27 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
28 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
29 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
30 import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
31 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
32 import org.opendaylight.controller.netconf.util.xml.XmlElement;
33 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39 public class Get extends AbstractConfigNetconfOperation {
40
41     private final YangStoreSnapshot yangStoreSnapshot;
42     private static final Logger logger = LoggerFactory.getLogger(Get.class);
43
44     public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
45                String netconfSessionIdForReporting) {
46         super(configRegistryClient, netconfSessionIdForReporting);
47         this.yangStoreSnapshot = yangStoreSnapshot;
48     }
49
50     private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(ConfigRegistryClient configRegistryClient,
51             Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
52         Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
53
54         for (String namespace : mBeanEntries.keySet()) {
55
56             Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
57             Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
58             for (String module : entriesFromNamespace.keySet()) {
59
60                 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
61
62                 Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
63                 RuntimeBeanEntry root = null;
64                 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
65                     cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap()));
66                     if (rbe.isRoot()){
67                         root = rbe;
68                     }
69                 }
70
71                 if (root == null){
72                     continue;
73                 }
74
75                 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
76                 ModuleRuntime moduleRuntime = new ModuleRuntime(rootInstanceRuntime);
77                 innerMap.put(module, moduleRuntime);
78             }
79
80             retVal.put(namespace, innerMap);
81         }
82         return retVal;
83     }
84
85     private InstanceRuntime createInstanceRuntime(RuntimeBeanEntry root, Map<RuntimeBeanEntry, InstanceConfig> cache) {
86         Map<String, InstanceRuntime> children = Maps.newHashMap();
87         for (RuntimeBeanEntry child : root.getChildren()) {
88             children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
89         }
90
91         return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
92     }
93
94     private Map<String, String> createJmxToYangMap(List<RuntimeBeanEntry> children) {
95         Map<String, String> jmxToYangNamesForChildRbe = Maps.newHashMap();
96         for (RuntimeBeanEntry rbe : children) {
97             jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
98         }
99         return jmxToYangNamesForChildRbe;
100     }
101
102     private static void checkXml(XmlElement xml) throws UnexpectedElementException, UnexpectedNamespaceException, MissingNameSpaceException {
103         xml.checkName(XmlNetconfConstants.GET);
104         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
105
106         // Filter option - unsupported
107         if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0){
108             throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + XmlNetconfConstants.GET);
109         }
110     }
111
112     @Override
113     protected String getOperationName() {
114         return XmlNetconfConstants.GET;
115     }
116
117     @Override
118     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
119         checkXml(xml);
120
121         final Set<ObjectName> runtimeBeans = getConfigRegistryClient().lookupRuntimeBeans();
122
123         //Transaction provider required only for candidate datastore
124         final Set<ObjectName> configBeans = Datastore.getInstanceQueryStrategy(Datastore.running, null)
125                 .queryInstances(getConfigRegistryClient());
126
127         final Map<String, Map<String, ModuleRuntime>> moduleRuntimes = createModuleRuntimes(getConfigRegistryClient(),
128                 yangStoreSnapshot.getModuleMXBeanEntryMap());
129         final Map<String, Map<String, ModuleConfig>> moduleConfigs = EditConfig.transformMbeToModuleConfigs(
130                 getConfigRegistryClient(), yangStoreSnapshot.getModuleMXBeanEntryMap());
131
132         final Runtime runtime = new Runtime(moduleRuntimes, moduleConfigs);
133
134         final Element element = runtime.toXml(runtimeBeans, configBeans, document);
135
136         logger.trace("{} operation successful", XmlNetconfConstants.GET);
137
138         return element;
139     }
140 }