2 * Copyright (c) 2015, 2017 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.config.facade.xml.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.List;
16 import java.util.Map.Entry;
17 import javax.management.ObjectName;
18 import javax.management.openmbean.OpenType;
19 import org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping;
20 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
21 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeReadingStrategy;
22 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.ObjectXmlReader;
23 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy;
24 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper;
25 import org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving.AttributeResolvingStrategy;
26 import org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving.ObjectResolver;
27 import org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.AttributeWritingStrategy;
28 import org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.ObjectXmlWriter;
29 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
30 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
31 import org.opendaylight.controller.config.util.BeanReader;
32 import org.opendaylight.controller.config.util.xml.DocumentedException;
33 import org.opendaylight.controller.config.util.xml.XmlElement;
34 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
35 import org.opendaylight.controller.config.util.xml.XmlUtil;
36 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
37 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
38 import org.opendaylight.yangtools.yang.common.Revision;
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(final BeanReader configRegistryClient, final Map<String, AttributeIfc> yangNamesToAttributes,
53 final String nullableDummyContainerName) {
55 this.yangToAttrConfig = yangNamesToAttributes;
56 this.nullableDummyContainerName = nullableDummyContainerName;
57 this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
58 this.configRegistryClient = configRegistryClient;
61 @SuppressWarnings("IllegalCatch")
62 private Map<String, Object> getMappedConfiguration(final ObjectName on, final EnumResolver enumResolver) {
64 // TODO make field, mappingStrategies can be instantiated only once
65 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper()
66 .prepareMapping(jmxToAttrConfig, enumResolver);
68 Map<String, Object> toXml = Maps.newHashMap();
70 for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
71 // Skip children runtime beans as they are mapped by InstanceRuntime
72 if (configDefEntry.getValue() instanceof RuntimeBeanEntry) {
75 Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
77 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
78 .get(configDefEntry.getKey());
79 Optional<?> attribute = attributeMappingStrategy.mapAttribute(value);
80 if (!attribute.isPresent()) {
83 toXml.put(configDefEntry.getValue().getAttributeYangName(), attribute.get());
84 } catch (final RuntimeException e) {
85 throw new IllegalStateException("Unable to map value " + value + " to attribute "
86 + configDefEntry.getKey(), e);
92 @SuppressWarnings("IllegalCatch")
93 public Element toXml(final ObjectName on, final String namespace, final Document document,
94 final Element rootElement, final EnumResolver enumResolver) {
95 Map<String, AttributeWritingStrategy> strats = new ObjectXmlWriter().prepareWriting(yangToAttrConfig, document);
96 Map<String, Object> mappedConfig = getMappedConfiguration(on, enumResolver);
97 Element parentElement;
98 if (nullableDummyContainerName != null) {
99 Element dummyElement = XmlUtil.createElement(document, nullableDummyContainerName, Optional.of(namespace));
100 rootElement.appendChild(dummyElement);
101 parentElement = dummyElement;
103 parentElement = rootElement;
105 for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
107 strats.get(mappingEntry.getKey()).writeElement(parentElement, namespace, mappingEntry.getValue());
108 } catch (final RuntimeException e) {
109 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
110 + mappingEntry.getValue(), e);
116 private void resolveConfiguration(final InstanceConfigElementResolved mappedConfig,
117 final ServiceRegistryWrapper depTracker, final EnumResolver enumResolver) {
119 // TODO make field, resolvingStrategies can be instantiated only once
120 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
121 depTracker).prepareResolving(yangToAttrConfig, enumResolver);
123 for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
124 AttributeConfigElement value = configDefEntry.getValue();
125 String attributeName = configDefEntry.getKey();
127 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
129 LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName,
130 attributeResolvingStrategy);
132 value.resolveValue(attributeResolvingStrategy, attributeName);
133 value.setJmxName(yangToAttrConfig.get(attributeName).getUpperCaseCammelCase());
134 } catch (final DocumentedException e) {
135 throw new IllegalStateException("Unable to resolve value " + value + " to attribute " + attributeName,
141 public InstanceConfigElementResolved fromXml(XmlElement moduleElement, final ServiceRegistryWrapper services,
142 final String moduleNamespace, final EditStrategyType defaultStrategy,
143 final Map<String, Map<Optional<Revision>, IdentityMapping>> identityMap,
144 final EnumResolver enumResolver)
145 throws DocumentedException {
146 Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
148 Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig,
150 List<XmlElement> recognisedChildren = Lists.newArrayList();
152 XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
153 XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.NAME_KEY);
154 List<XmlElement> typeAndNameElements = Lists.newArrayList(typeElement, nameElement);
156 // if dummy container was defined in yang, set moduleElement to its content
157 if (nullableDummyContainerName != null) {
158 int size = moduleElement.getChildElements().size();
159 int expectedChildNodes = 1 + typeAndNameElements.size();
160 if (size > expectedChildNodes) {
161 throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : "
162 + nameElement.getTextContent() + " - Expected " + expectedChildNodes + " child nodes, "
163 + "one of them with name " + nullableDummyContainerName + ", got " + size + " elements.");
165 if (size == expectedChildNodes) {
167 moduleElement = moduleElement.getOnlyChildElement(nullableDummyContainerName, moduleNamespace);
168 } catch (final DocumentedException e) {
169 throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : "
170 + nameElement.getTextContent() + " - Expected child node with name "
171 + nullableDummyContainerName + "." + e.getMessage(), e);
173 } // else 2 elements, no need to descend
176 for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
177 List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(),
178 recognisedChildren, typeAndNameElements);
179 AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
180 retVal.put(readStratEntry.getKey(), readElement);
183 recognisedChildren.addAll(typeAndNameElements);
185 moduleElement.checkUnrecognisedElements(recognisedChildren);
186 } catch (final DocumentedException e) {
187 throw new DocumentedException(
188 "Error reading module " + typeElement.getTextContent() + " : " + nameElement.getTextContent()
189 + " - " + e.getMessage(), e,
190 e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
192 // TODO: add check for conflicts between global and local edit strategy
193 String perInstanceEditStrategy = moduleElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY,
194 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
196 InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("")
197 ? new InstanceConfigElementResolved(retVal, defaultStrategy)
198 : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
200 resolveConfiguration(instanceConfigElementResolved, services, enumResolver);
201 return instanceConfigElementResolved;
204 private List<XmlElement> getConfigNodes(final XmlElement moduleElement, final String moduleNamespace,
205 final String name, final List<XmlElement> recognisedChildren,
206 final List<XmlElement> typeAndName)
207 throws DocumentedException {
208 List<XmlElement> foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name, moduleNamespace);
209 if (foundConfigNodes.isEmpty()) {
210 LOG.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
211 LOG.debug("Trying lookup of config nodes without specified namespace");
212 foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name,
213 XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
214 // In case module type or name element is not present in config it
215 // would be matched with config type or name
216 // We need to remove config type and name from available module
218 foundConfigNodes.removeAll(typeAndName);
219 LOG.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
222 List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
223 XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
224 foundWithoutNamespaceNodes.removeAll(typeAndName);
225 if (!foundWithoutNamespaceNodes.isEmpty()) {
226 throw new DocumentedException(
227 String.format("Element %s present multiple times with different namespaces: %s, %s", name,
228 foundConfigNodes, foundWithoutNamespaceNodes),
229 DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.INVALID_VALUE,
230 DocumentedException.ErrorSeverity.ERROR);
233 recognisedChildren.addAll(foundConfigNodes);
234 return foundConfigNodes;
237 private static Map<String, AttributeIfc> reverseMap(final Map<String, AttributeIfc> yangNameToAttr) {
238 Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
240 for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
241 reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
243 return reversednameToAtr;