2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.get;
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;
35 import javax.management.ObjectName;
36 import java.util.HashMap;
37 import java.util.List;
41 public class Get extends AbstractConfigNetconfOperation {
43 private final YangStoreSnapshot yangStoreSnapshot;
44 private static final Logger logger = LoggerFactory.getLogger(Get.class);
46 public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
47 String netconfSessionIdForReporting) {
48 super(configRegistryClient, netconfSessionIdForReporting);
49 this.yangStoreSnapshot = yangStoreSnapshot;
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();
56 for (String namespace : mBeanEntries.keySet()) {
58 Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
59 Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
60 for (String module : entriesFromNamespace.keySet()) {
62 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
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()));
75 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
76 ModuleRuntime moduleRuntime = new ModuleRuntime(module, rootInstanceRuntime);
77 innerMap.put(module, moduleRuntime);
80 retVal.put(namespace, innerMap);
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));
91 return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
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());
99 return jmxToYangNamesForChildRbe;
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);
106 // Filter option - unsupported
107 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
108 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + XmlNetconfConstants.GET);
112 protected String getOperationName() {
113 return XmlNetconfConstants.GET;
117 protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
134 final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
136 //Transaction provider required only for candidate datastore
137 final Set<ObjectName> configBeans = Datastore.getInstanceQueryStrategy(Datastore.running, null)
138 .queryInstances(configRegistryClient);
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());
145 final Runtime runtime = new Runtime(moduleRuntimes, moduleConfigs);
147 final Element element = runtime.toXml(runtimeBeans, configBeans, document);
149 logger.info("{} operation successful", XmlNetconfConstants.GET);