Fix race conditions between config-manager and persister.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / get / Get.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.get;
10
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;
31
32 import javax.management.ObjectName;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38 public class Get extends AbstractConfigNetconfOperation {
39
40     public static final String GET = "get";
41
42     private final YangStoreSnapshot yangStoreSnapshot;
43     private static final Logger logger = LoggerFactory.getLogger(Get.class);
44
45     public Get(YangStoreSnapshot yangStoreSnapshot, ConfigRegistryClient configRegistryClient,
46             String netconfSessionIdForReporting) {
47         super(configRegistryClient, netconfSessionIdForReporting);
48         this.yangStoreSnapshot = yangStoreSnapshot;
49     }
50
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();
54
55         for (String namespace : mBeanEntries.keySet()) {
56
57             Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
58             Map<String, ModuleMXBeanEntry> entriesFromNamespace = mBeanEntries.get(namespace);
59             for (String module : entriesFromNamespace.keySet()) {
60
61                 ModuleMXBeanEntry mbe = entriesFromNamespace.get(module);
62
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()));
67                     if (rbe.isRoot())
68                         root = rbe;
69                 }
70
71                 if (root == null)
72                     continue;
73
74                 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
75                 ModuleRuntime moduleRuntime = new ModuleRuntime(module, rootInstanceRuntime);
76                 innerMap.put(module, moduleRuntime);
77             }
78
79             retVal.put(namespace, innerMap);
80         }
81         return retVal;
82     }
83
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));
88         }
89
90         return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
91     }
92
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());
97         }
98         return jmxToYangNamesForChildRbe;
99     }
100
101     private static void checkXml(XmlElement xml) {
102         xml.checkName(GET);
103         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
104
105         // Filter option - unsupported
106         if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
107             throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for " + GET);
108     }
109
110     @Override
111     protected String getOperationName() {
112         return GET;
113     }
114
115     @Override
116     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
117         try {
118             checkXml(xml);
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);
131         }
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);
136
137         final Element element = runtime.toXml(runtimeBeans, document);
138
139         logger.info("{} operation successful", GET);
140
141         return element;
142     }
143 }