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

©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.