Merge "Add service reference binding to config registry."
[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 org.opendaylight.controller.config.util.ConfigRegistryClient;
13 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
14 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
15 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
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.getconfig.GetConfig;
28 import org.opendaylight.controller.netconf.util.xml.XmlElement;
29 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34
35 import javax.management.ObjectName;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40
41 public class Get extends AbstractConfigNetconfOperation {
42
43     private final YangStoreSnapshot yangStoreSnapshot;
44     private static final Logger logger = LoggerFactory.getLogger(Get.class);
45
46     public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
47             String netconfSessionIdForReporting) {
48         super(configRegistryClient, netconfSessionIdForReporting);
49         this.yangStoreSnapshot = yangStoreSnapshot;
50     }
51
52     private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(ConfigRegistryClient configRegistryClient,
53             Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
54         Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
55
56         for (String namespace : mBeanEntries.keySet()) {
57
58             Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
59             Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
60             for (String module : entriesFromNamespace.keySet()) {
61
62                 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
63
64                 Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
65                 RuntimeBeanEntry root = null;
66                 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
67                     cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap()));
68                     if (rbe.isRoot())
69                         root = rbe;
70                 }
71
72                 if (root == null)
73                     continue;
74
75                 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
76                 ModuleRuntime moduleRuntime = new ModuleRuntime(module, 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) {
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     @Override
112     protected String getOperationName() {
113         return XmlNetconfConstants.GET;
114     }
115
116     @Override
117     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
118         try {
119             checkXml(xml);
120         } catch (final IllegalArgumentException e) {
121             logger.warn("Error parsing xml", e);
122             final Map<String, String> errorInfo = new HashMap<>();
123             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
124             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
125                     ErrorSeverity.error, errorInfo);
126         } catch (final UnsupportedOperationException e) {
127             logger.warn("Unsupported", e);
128             final Map<String, String> errorInfo = new HashMap<>();
129             errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for 'get'");
130             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
131                     ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
132         }
133
134         final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
135
136         //Transaction provider required only for candidate datastore
137         final Set<ObjectName> configBeans = Datastore.getInstanceQueryStrategy(Datastore.running, null)
138                 .queryInstances(configRegistryClient);
139
140         final Map<String, Map<String, ModuleRuntime>> moduleRuntimes = createModuleRuntimes(configRegistryClient,
141                 yangStoreSnapshot.getModuleMXBeanEntryMap());
142         final Map<String, Map<String, ModuleConfig>> moduleConfigs = GetConfig.transform(configRegistryClient,
143                 yangStoreSnapshot.getModuleMXBeanEntryMap());
144
145         final Runtime runtime = new Runtime(moduleRuntimes, moduleConfigs);
146
147         final Element element = runtime.toXml(runtimeBeans, configBeans, document);
148
149         logger.info("{} operation successful", XmlNetconfConstants.GET);
150
151         return element;
152     }
153 }