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.util.ConfigTransactionClient;
14 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
15 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
16 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleConfig;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.InstanceRuntime;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.ModuleRuntime;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.Runtime;
26 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
27 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
28 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
29 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
30 import org.opendaylight.controller.netconf.util.xml.XmlElement;
31 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
37 import javax.management.ObjectName;
38 import java.util.HashMap;
39 import java.util.List;
43 public class Get extends AbstractConfigNetconfOperation {
45 private final YangStoreSnapshot yangStoreSnapshot;
46 private static final Logger logger = LoggerFactory.getLogger(Get.class);
47 private final TransactionProvider transactionProvider;
49 public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
50 String netconfSessionIdForReporting, TransactionProvider transactionProvider) {
51 super(configRegistryClient, netconfSessionIdForReporting);
52 this.yangStoreSnapshot = yangStoreSnapshot;
53 this.transactionProvider = transactionProvider;
56 private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(ConfigRegistryClient configRegistryClient,
57 Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
58 Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
60 for (String namespace : mBeanEntries.keySet()) {
62 Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
63 Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
64 for (String module : entriesFromNamespace.keySet()) {
66 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
68 Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
69 RuntimeBeanEntry root = null;
70 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
71 cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap()));
79 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
80 ModuleRuntime moduleRuntime = new ModuleRuntime(module, rootInstanceRuntime);
81 innerMap.put(module, moduleRuntime);
84 retVal.put(namespace, innerMap);
89 private InstanceRuntime createInstanceRuntime(RuntimeBeanEntry root, Map<RuntimeBeanEntry, InstanceConfig> cache) {
90 Map<String, InstanceRuntime> children = Maps.newHashMap();
91 for (RuntimeBeanEntry child : root.getChildren()) {
92 children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
95 return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
98 private Map<String, String> createJmxToYangMap(List<RuntimeBeanEntry> children) {
99 Map<String, String> jmxToYangNamesForChildRbe = Maps.newHashMap();
100 for (RuntimeBeanEntry rbe : children) {
101 jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
103 return jmxToYangNamesForChildRbe;
106 private static void checkXml(XmlElement xml) {
107 xml.checkName(XmlNetconfConstants.GET);
108 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
110 // Filter option - unsupported
111 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
112 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + XmlNetconfConstants.GET);
116 protected String getOperationName() {
117 return XmlNetconfConstants.GET;
121 protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
124 } catch (final IllegalArgumentException e) {
125 logger.warn("Error parsing xml", e);
126 final Map<String, String> errorInfo = new HashMap<>();
127 errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
128 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
129 ErrorSeverity.error, errorInfo);
130 } catch (final UnsupportedOperationException e) {
131 logger.warn("Unsupported", e);
132 final Map<String, String> errorInfo = new HashMap<>();
133 errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for 'get'");
134 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
135 ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
138 final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
140 //Transaction provider required only for candidate datastore
141 final Set<ObjectName> configBeans = Datastore.getInstanceQueryStrategy(Datastore.running, null)
142 .queryInstances(configRegistryClient);
144 final Map<String, Map<String, ModuleRuntime>> moduleRuntimes = createModuleRuntimes(configRegistryClient,
145 yangStoreSnapshot.getModuleMXBeanEntryMap());
146 final Map<String, Map<String, ModuleConfig>> moduleConfigs = GetConfig.transform(configRegistryClient,
147 yangStoreSnapshot.getModuleMXBeanEntryMap());
149 final Runtime runtime = new Runtime(moduleRuntimes, moduleConfigs);
151 ObjectName txOn = transactionProvider.getOrCreateTransaction();
152 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(txOn);
153 final Element element = runtime.toXml(runtimeBeans, configBeans, document, ta);
155 logger.info("{} operation successful", XmlNetconfConstants.GET);