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 java.util.HashMap;
12 import java.util.List;
16 import javax.management.ObjectName;
18 import org.opendaylight.controller.config.util.ConfigRegistryClient;
19 import org.opendaylight.controller.config.util.ConfigTransactionClient;
20 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
21 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
22 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
25 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
26 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleConfig;
29 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
30 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.InstanceRuntime;
31 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.ModuleRuntime;
32 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.runtime.Runtime;
33 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
34 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
35 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
36 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
37 import org.opendaylight.controller.netconf.util.xml.XmlElement;
38 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
44 import com.google.common.collect.Maps;
46 public class Get extends AbstractConfigNetconfOperation {
48 private final YangStoreSnapshot yangStoreSnapshot;
49 private static final Logger logger = LoggerFactory.getLogger(Get.class);
50 private final TransactionProvider transactionProvider;
52 public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
53 String netconfSessionIdForReporting, TransactionProvider transactionProvider) {
54 super(configRegistryClient, netconfSessionIdForReporting);
55 this.yangStoreSnapshot = yangStoreSnapshot;
56 this.transactionProvider = transactionProvider;
59 private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(ConfigRegistryClient configRegistryClient,
60 Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
61 Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
63 for (String namespace : mBeanEntries.keySet()) {
65 Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
66 Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
67 for (String module : entriesFromNamespace.keySet()) {
69 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
71 Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
72 RuntimeBeanEntry root = null;
73 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
74 cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap()));
82 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
83 ModuleRuntime moduleRuntime = new ModuleRuntime(module, rootInstanceRuntime);
84 innerMap.put(module, moduleRuntime);
87 retVal.put(namespace, innerMap);
92 private InstanceRuntime createInstanceRuntime(RuntimeBeanEntry root, Map<RuntimeBeanEntry, InstanceConfig> cache) {
93 Map<String, InstanceRuntime> children = Maps.newHashMap();
94 for (RuntimeBeanEntry child : root.getChildren()) {
95 children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
98 return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
101 private Map<String, String> createJmxToYangMap(List<RuntimeBeanEntry> children) {
102 Map<String, String> jmxToYangNamesForChildRbe = Maps.newHashMap();
103 for (RuntimeBeanEntry rbe : children) {
104 jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
106 return jmxToYangNamesForChildRbe;
109 private static void checkXml(XmlElement xml) {
110 xml.checkName(XmlNetconfConstants.GET);
111 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
113 // Filter option - unsupported
114 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
115 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + XmlNetconfConstants.GET);
119 protected String getOperationName() {
120 return XmlNetconfConstants.GET;
124 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
127 } catch (final IllegalArgumentException e) {
128 logger.warn("Error parsing xml", e);
129 final Map<String, String> errorInfo = new HashMap<>();
130 errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
131 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
132 ErrorSeverity.error, errorInfo);
133 } catch (final UnsupportedOperationException e) {
134 logger.warn("Unsupported", e);
135 final Map<String, String> errorInfo = new HashMap<>();
136 errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for 'get'");
137 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
138 ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
141 final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
143 //Transaction provider required only for candidate datastore
144 final Set<ObjectName> configBeans = Datastore.getInstanceQueryStrategy(Datastore.running, null)
145 .queryInstances(configRegistryClient);
147 final Map<String, Map<String, ModuleRuntime>> moduleRuntimes = createModuleRuntimes(configRegistryClient,
148 yangStoreSnapshot.getModuleMXBeanEntryMap());
149 final Map<String, Map<String, ModuleConfig>> moduleConfigs = EditConfig.transformMbeToModuleConfigs(
150 configRegistryClient, yangStoreSnapshot.getModuleMXBeanEntryMap());
152 final Runtime runtime = new Runtime(moduleRuntimes, moduleConfigs);
154 ObjectName txOn = transactionProvider.getOrCreateTransaction();
155 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(txOn);
156 final Element element = runtime.toXml(runtimeBeans, configBeans, document, new ServiceRegistryWrapper(ta));
158 logger.trace("{} operation successful", XmlNetconfConstants.GET);