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.mapping.config;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Maps;
14 import java.util.Date;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import javax.management.ObjectName;
19 import javax.management.openmbean.OpenType;
20 import org.opendaylight.controller.config.util.BeanReader;
21 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeReadingStrategy;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectXmlReader;
28 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
29 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
30 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.AttributeResolvingStrategy;
31 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.ObjectResolver;
32 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.AttributeWritingStrategy;
33 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
34 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
35 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
36 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
37 import org.opendaylight.controller.netconf.util.xml.XmlElement;
38 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
44 public final class InstanceConfig {
45 private static final Logger LOG = LoggerFactory.getLogger(InstanceConfig.class);
47 private final Map<String, AttributeIfc> yangToAttrConfig;
48 private final String nullableDummyContainerName;
49 private final Map<String, AttributeIfc> jmxToAttrConfig;
50 private final BeanReader configRegistryClient;
52 public InstanceConfig(BeanReader configRegistryClient, Map<String, AttributeIfc> yangNamesToAttributes,
53 String nullableDummyContainerName) {
55 this.yangToAttrConfig = yangNamesToAttributes;
56 this.nullableDummyContainerName = nullableDummyContainerName;
57 this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
58 this.configRegistryClient = configRegistryClient;
61 private Map<String, Object> getMappedConfiguration(ObjectName on, final EnumResolver enumResolver) {
63 // TODO make field, mappingStrategies can be instantiated only once
64 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper()
65 .prepareMapping(jmxToAttrConfig, enumResolver);
67 Map<String, Object> toXml = Maps.newHashMap();
69 for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
70 // Skip children runtime beans as they are mapped by InstanceRuntime
71 if (configDefEntry.getValue() instanceof RuntimeBeanEntry){
74 Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
76 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
77 .get(configDefEntry.getKey());
78 Optional<?> a = attributeMappingStrategy.mapAttribute(value);
82 toXml.put(configDefEntry.getValue().getAttributeYangName(), a.get());
83 } catch (Exception e) {
84 throw new IllegalStateException("Unable to map value " + value + " to attribute "
85 + configDefEntry.getKey(), e);
91 public Element toXml(ObjectName on, String namespace, Document document, Element rootElement, final EnumResolver enumResolver) {
92 Map<String, AttributeWritingStrategy> strats = new ObjectXmlWriter().prepareWriting(yangToAttrConfig, document);
93 Map<String, Object> mappedConfig = getMappedConfiguration(on, enumResolver);
94 Element parentElement;
95 if (nullableDummyContainerName != null) {
96 Element dummyElement = XmlUtil.createElement(document, nullableDummyContainerName, Optional.of(namespace));
97 rootElement.appendChild(dummyElement);
98 parentElement = dummyElement;
100 parentElement = rootElement;
102 for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
104 strats.get(mappingEntry.getKey()).writeElement(parentElement, namespace, mappingEntry.getValue());
105 } catch (Exception e) {
106 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
107 + mappingEntry.getValue(), e);
113 private void resolveConfiguration(InstanceConfigElementResolved mappedConfig, ServiceRegistryWrapper depTracker, final EnumResolver enumResolver) {
115 // TODO make field, resolvingStrategies can be instantiated only once
116 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
117 depTracker).prepareResolving(yangToAttrConfig, enumResolver);
119 for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
120 AttributeConfigElement value = configDefEntry.getValue();
121 String attributeName = configDefEntry.getKey();
123 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
125 LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
127 value.resolveValue(attributeResolvingStrategy, attributeName);
129 yangToAttrConfig.get(attributeName).getUpperCaseCammelCase());
130 } catch (Exception e) {
131 throw new IllegalStateException("Unable to resolve value " + value
132 + " to attribute " + attributeName, e);
137 public InstanceConfigElementResolved fromXml(XmlElement moduleElement, ServiceRegistryWrapper services, String moduleNamespace,
138 EditStrategyType defaultStrategy,
139 Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap, final EnumResolver enumResolver) throws NetconfDocumentedException {
140 Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
142 Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig, identityMap);
143 List<XmlElement> recognisedChildren = Lists.newArrayList();
145 XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
146 XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
147 List<XmlElement> typeAndNameElements = Lists.newArrayList(typeElement, nameElement);
149 // if dummy container was defined in yang, set moduleElement to its content
150 if (nullableDummyContainerName != null) {
151 int size = moduleElement.getChildElements().size();
152 int expectedChildNodes = 1 + typeAndNameElements.size();
153 if (size > expectedChildNodes) {
154 throw new NetconfDocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
155 nameElement.getTextContent() + " - Expected " + expectedChildNodes +" child nodes, " +
156 "one of them with name " + nullableDummyContainerName +
157 ", got " + size + " elements.");
159 if (size == expectedChildNodes) {
161 moduleElement = moduleElement.getOnlyChildElement(nullableDummyContainerName, moduleNamespace);
162 } catch (NetconfDocumentedException e) {
163 throw new NetconfDocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
164 nameElement.getTextContent() + " - Expected child node with name " + nullableDummyContainerName +
165 "." + e.getMessage());
167 } // else 2 elements, no need to descend
170 for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
171 List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(),
172 recognisedChildren, typeAndNameElements);
173 AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
174 retVal.put(readStratEntry.getKey(), readElement);
177 recognisedChildren.addAll(typeAndNameElements);
179 moduleElement.checkUnrecognisedElements(recognisedChildren);
180 } catch (NetconfDocumentedException e) {
181 throw new NetconfDocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
182 nameElement.getTextContent() + " - " +
183 e.getMessage(), e.getErrorType(), e.getErrorTag(),e.getErrorSeverity(),e.getErrorInfo());
185 // TODO: add check for conflicts between global and local edit strategy
186 String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
187 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
189 InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(
190 retVal, defaultStrategy) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
192 resolveConfiguration(instanceConfigElementResolved, services, enumResolver);
193 return instanceConfigElementResolved;
196 private List<XmlElement> getConfigNodes(XmlElement moduleElement, String moduleNamespace, String name,
197 List<XmlElement> recognisedChildren, List<XmlElement> typeAndName) throws NetconfDocumentedException {
198 List<XmlElement> foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name, moduleNamespace);
199 if (foundConfigNodes.isEmpty()) {
200 LOG.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
201 LOG.debug("Trying lookup of config nodes without specified namespace");
202 foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name,
203 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
204 // In case module type or name element is not present in config it
205 // would be matched with config type or name
206 // We need to remove config type and name from available module
208 foundConfigNodes.removeAll(typeAndName);
209 LOG.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
212 List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
213 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
214 foundWithoutNamespaceNodes.removeAll(typeAndName);
215 if (!foundWithoutNamespaceNodes.isEmpty()){
216 throw new NetconfDocumentedException(String.format("Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
217 foundWithoutNamespaceNodes),
218 NetconfDocumentedException.ErrorType.application,
219 NetconfDocumentedException.ErrorTag.invalid_value,
220 NetconfDocumentedException.ErrorSeverity.error);
224 recognisedChildren.addAll(foundConfigNodes);
225 return foundConfigNodes;
228 private static Map<String, AttributeIfc> reverseMap(Map<String, AttributeIfc> yangNameToAttr) {
229 Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
231 for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
232 reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
235 return reversednameToAtr;