Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / config / InstanceConfig.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.mapping.config;
10
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;
15 import java.util.Map;
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;
43
44 public final class InstanceConfig {
45     private static final Logger LOG = LoggerFactory.getLogger(InstanceConfig.class);
46
47     private final Map<String, AttributeIfc> yangToAttrConfig;
48     private final String nullableDummyContainerName;
49     private final Map<String, AttributeIfc> jmxToAttrConfig;
50     private final BeanReader configRegistryClient;
51
52     public InstanceConfig(final BeanReader configRegistryClient, final Map<String, AttributeIfc> yangNamesToAttributes,
53                           final String nullableDummyContainerName) {
54
55         this.yangToAttrConfig = yangNamesToAttributes;
56         this.nullableDummyContainerName = nullableDummyContainerName;
57         this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
58         this.configRegistryClient = configRegistryClient;
59     }
60
61     @SuppressWarnings("IllegalCatch")
62     private Map<String, Object> getMappedConfiguration(final ObjectName on, final EnumResolver enumResolver) {
63
64         // TODO make field, mappingStrategies can be instantiated only once
65         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper()
66                 .prepareMapping(jmxToAttrConfig, enumResolver);
67
68         Map<String, Object> toXml = Maps.newHashMap();
69
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) {
73                 continue;
74             }
75             Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
76             try {
77                 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
78                         .get(configDefEntry.getKey());
79                 Optional<?> attribute = attributeMappingStrategy.mapAttribute(value);
80                 if (!attribute.isPresent()) {
81                     continue;
82                 }
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);
87             }
88         }
89         return toXml;
90     }
91
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;
102         } else {
103             parentElement = rootElement;
104         }
105         for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
106             try {
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);
111             }
112         }
113         return rootElement;
114     }
115
116     private void resolveConfiguration(final InstanceConfigElementResolved mappedConfig,
117                                       final ServiceRegistryWrapper depTracker, final EnumResolver enumResolver) {
118
119         // TODO make field, resolvingStrategies can be instantiated only once
120         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
121                 depTracker).prepareResolving(yangToAttrConfig, enumResolver);
122
123         for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
124             AttributeConfigElement value = configDefEntry.getValue();
125             String attributeName = configDefEntry.getKey();
126             try {
127                 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
128                         .get(attributeName);
129                 LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName,
130                         attributeResolvingStrategy);
131
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,
136                         e);
137             }
138         }
139     }
140
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();
147
148         Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig,
149                 identityMap);
150         List<XmlElement> recognisedChildren = Lists.newArrayList();
151
152         XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
153         XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.NAME_KEY);
154         List<XmlElement> typeAndNameElements = Lists.newArrayList(typeElement, nameElement);
155
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.");
164             }
165             if (size == expectedChildNodes) {
166                 try {
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);
172                 }
173             } // else 2 elements, no need to descend
174         }
175
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);
181         }
182
183         recognisedChildren.addAll(typeAndNameElements);
184         try {
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());
191         }
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);
195
196         InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("")
197                 ? new InstanceConfigElementResolved(retVal, defaultStrategy)
198                 : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
199
200         resolveConfiguration(instanceConfigElementResolved, services, enumResolver);
201         return instanceConfigElementResolved;
202     }
203
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
217             // config elements
218             foundConfigNodes.removeAll(typeAndName);
219             LOG.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
220                     moduleElement);
221         } else {
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);
231             }
232         }
233         recognisedChildren.addAll(foundConfigNodes);
234         return foundConfigNodes;
235     }
236
237     private static Map<String, AttributeIfc> reverseMap(final Map<String, AttributeIfc> yangNameToAttr) {
238         Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
239
240         for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
241             reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
242         }
243         return reversednameToAtr;
244     }
245 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.