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.runtime.InstanceRuntime;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.ModuleRuntime;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.Runtime;
24 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
32 import javax.management.ObjectName;
33 import java.util.HashMap;
34 import java.util.List;
38 public class Get extends AbstractConfigNetconfOperation {
40 public static final String GET = "get";
42 private final YangStoreSnapshot yangStoreSnapshot;
43 private static final Logger logger = LoggerFactory.getLogger(Get.class);
45 public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
46 String netconfSessionIdForReporting) {
47 super(configRegistryClient, netconfSessionIdForReporting);
48 this.yangStoreSnapshot = yangStoreSnapshot;
51 private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(ConfigRegistryClient configRegistryClient,
52 Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
53 Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
55 for (String namespace : mBeanEntries.keySet()) {
57 Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
58 Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
59 for (String module : entriesFromNamespace.keySet()) {
61 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
63 Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
64 RuntimeBeanEntry root = null;
65 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
66 cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap()));
74 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
75 ModuleRuntime moduleRuntime = new ModuleRuntime(module, rootInstanceRuntime);
76 innerMap.put(module, moduleRuntime);
79 retVal.put(namespace, innerMap);
84 private InstanceRuntime createInstanceRuntime(RuntimeBeanEntry root, Map<RuntimeBeanEntry, InstanceConfig> cache) {
85 Map<String, InstanceRuntime> children = Maps.newHashMap();
86 for (RuntimeBeanEntry child : root.getChildren()) {
87 children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
90 return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
93 private Map<String, String> createJmxToYangMap(List<RuntimeBeanEntry> children) {
94 Map<String, String> jmxToYangNamesForChildRbe = Maps.newHashMap();
95 for (RuntimeBeanEntry rbe : children) {
96 jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
98 return jmxToYangNamesForChildRbe;
101 private static void checkXml(XmlElement xml) {
103 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
105 // Filter option - unsupported
106 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
107 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + GET);
111 protected String getOperationName() {
116 protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
119 } catch (final IllegalArgumentException e) {
120 logger.warn("Error parsing xml", e);
121 final Map<String, String> errorInfo = new HashMap<>();
122 errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
123 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
124 ErrorSeverity.error, errorInfo);
125 } catch (final UnsupportedOperationException e) {
126 logger.warn("Unsupported", e);
127 final Map<String, String> errorInfo = new HashMap<>();
128 errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for 'get'");
129 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
130 ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
132 final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
133 final Map<String, Map<String, ModuleRuntime>> moduleMappings = createModuleRuntimes(configRegistryClient,
134 yangStoreSnapshot.getModuleMXBeanEntryMap());
135 final Runtime runtime = new Runtime(moduleMappings);
137 final Element element = runtime.toXml(runtimeBeans, document);
139 logger.info("{} operation successful", GET);