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 public static final String GET = "get";
45 private final YangStoreSnapshot yangStoreSnapshot;
46 private static final Logger logger = LoggerFactory.getLogger(Get.class);
48 public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
49 String netconfSessionIdForReporting) {
50 super(configRegistryClient, netconfSessionIdForReporting);
51 this.yangStoreSnapshot = yangStoreSnapshot;
54 private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(ConfigRegistryClient configRegistryClient,
55 Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
56 Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
58 for (String namespace : mBeanEntries.keySet()) {
60 Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
61 Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
62 for (String module : entriesFromNamespace.keySet()) {
64 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
66 Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
67 RuntimeBeanEntry root = null;
68 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
69 cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap()));
77 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
78 ModuleRuntime moduleRuntime = new ModuleRuntime(module, rootInstanceRuntime);
79 innerMap.put(module, moduleRuntime);
82 retVal.put(namespace, innerMap);
87 private InstanceRuntime createInstanceRuntime(RuntimeBeanEntry root, Map<RuntimeBeanEntry, InstanceConfig> cache) {
88 Map<String, InstanceRuntime> children = Maps.newHashMap();
89 for (RuntimeBeanEntry child : root.getChildren()) {
90 children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
93 return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
96 private Map<String, String> createJmxToYangMap(List<RuntimeBeanEntry> children) {
97 Map<String, String> jmxToYangNamesForChildRbe = Maps.newHashMap();
98 for (RuntimeBeanEntry rbe : children) {
99 jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
101 return jmxToYangNamesForChildRbe;
104 private static void checkXml(XmlElement xml) {
106 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
108 // Filter option - unsupported
109 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
110 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + GET);
114 protected String getOperationName() {
119 protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
122 } catch (final IllegalArgumentException e) {
123 logger.warn("Error parsing xml", e);
124 final Map<String, String> errorInfo = new HashMap<>();
125 errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
126 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
127 ErrorSeverity.error, errorInfo);
128 } catch (final UnsupportedOperationException e) {
129 logger.warn("Unsupported", e);
130 final Map<String, String> errorInfo = new HashMap<>();
131 errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for 'get'");
132 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
133 ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
136 final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
138 //Transaction provider required only for candidate datastore
139 final Set<ObjectName> configBeans = Datastore.getInstanceQueryStrategy(Datastore.running, null)
140 .queryInstances(configRegistryClient);
142 final Map<String, Map<String, ModuleRuntime>> moduleRuntimes = createModuleRuntimes(configRegistryClient,
143 yangStoreSnapshot.getModuleMXBeanEntryMap());
144 final Map<String, Map<String, ModuleConfig>> moduleConfigs = GetConfig.transform(configRegistryClient,
145 yangStoreSnapshot.getModuleMXBeanEntryMap());
147 final Runtime runtime = new Runtime(moduleRuntimes, moduleConfigs);
149 final Element element = runtime.toXml(runtimeBeans, configBeans, document);
151 logger.info("{} operation successful", GET);