config-manager-facade-xml: final parameters
[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 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.Date;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import javax.management.ObjectName;
19 import javax.management.openmbean.OpenType;
20 import org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping;
21 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeConfigElement;
22 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.AttributeReadingStrategy;
23 import org.opendaylight.controller.config.facade.xml.mapping.attributes.fromxml.ObjectXmlReader;
24 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.AttributeMappingStrategy;
25 import org.opendaylight.controller.config.facade.xml.mapping.attributes.mapping.ObjectMapper;
26 import org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving.AttributeResolvingStrategy;
27 import org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving.ObjectResolver;
28 import org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.AttributeWritingStrategy;
29 import org.opendaylight.controller.config.facade.xml.mapping.attributes.toxml.ObjectXmlWriter;
30 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
31 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
32 import org.opendaylight.controller.config.util.BeanReader;
33 import org.opendaylight.controller.config.util.xml.DocumentedException;
34 import org.opendaylight.controller.config.util.xml.XmlElement;
35 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
36 import org.opendaylight.controller.config.util.xml.XmlUtil;
37 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
38 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
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     private Map<String, Object> getMappedConfiguration(final ObjectName on, final EnumResolver enumResolver) {
62
63         // TODO make field, mappingStrategies can be instantiated only once
64         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper()
65                 .prepareMapping(jmxToAttrConfig, enumResolver);
66
67         Map<String, Object> toXml = Maps.newHashMap();
68
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){
72                 continue;
73             }
74             Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
75             try {
76                 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
77                         .get(configDefEntry.getKey());
78                 Optional<?> a = attributeMappingStrategy.mapAttribute(value);
79                 if (!a.isPresent()){
80                     continue;
81                 }
82                 toXml.put(configDefEntry.getValue().getAttributeYangName(), a.get());
83             } catch (final Exception e) {
84                 throw new IllegalStateException("Unable to map value " + value + " to attribute "
85                         + configDefEntry.getKey(), e);
86             }
87         }
88         return toXml;
89     }
90
91     public Element toXml(final ObjectName on, final String namespace, final Document document, final 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;
99         } else {
100             parentElement = rootElement;
101         }
102         for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
103             try {
104                 strats.get(mappingEntry.getKey()).writeElement(parentElement, namespace, mappingEntry.getValue());
105             } catch (final Exception e) {
106                 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
107                         + mappingEntry.getValue(), e);
108             }
109         }
110         return rootElement;
111     }
112
113     private void resolveConfiguration(final InstanceConfigElementResolved mappedConfig, final ServiceRegistryWrapper depTracker, final EnumResolver enumResolver) {
114
115         // TODO make field, resolvingStrategies can be instantiated only once
116         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
117                 depTracker).prepareResolving(yangToAttrConfig, enumResolver);
118
119         for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
120             AttributeConfigElement value = configDefEntry.getValue();
121             String attributeName = configDefEntry.getKey();
122             try {
123                 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
124                         .get(attributeName);
125                 LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
126
127                 value.resolveValue(attributeResolvingStrategy, attributeName);
128                 value.setJmxName(
129                         yangToAttrConfig.get(attributeName).getUpperCaseCammelCase());
130             } catch (final Exception e) {
131                 throw new IllegalStateException("Unable to resolve value " + value
132                         + " to attribute " + attributeName, e);
133             }
134         }
135     }
136
137     public InstanceConfigElementResolved fromXml(XmlElement moduleElement, final ServiceRegistryWrapper services, final String moduleNamespace,
138                                                  final EditStrategyType defaultStrategy,
139                                                  final Map<String, Map<Date, IdentityMapping>> identityMap, final EnumResolver enumResolver) throws DocumentedException {
140         Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
141
142         Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig, identityMap);
143         List<XmlElement> recognisedChildren = Lists.newArrayList();
144
145         XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
146         XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.NAME_KEY);
147         List<XmlElement> typeAndNameElements = Lists.newArrayList(typeElement, nameElement);
148
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 DocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
155                         nameElement.getTextContent() + " - Expected " + expectedChildNodes +" child nodes, " +
156                         "one of them with name " + nullableDummyContainerName +
157                         ", got " + size + " elements.");
158             }
159             if (size == expectedChildNodes) {
160                 try {
161                     moduleElement = moduleElement.getOnlyChildElement(nullableDummyContainerName, moduleNamespace);
162                 } catch (final DocumentedException e) {
163                     throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
164                             nameElement.getTextContent() + " - Expected child node with name " + nullableDummyContainerName +
165                             "." + e.getMessage());
166                 }
167             } // else 2 elements, no need to descend
168         }
169
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);
175         }
176
177         recognisedChildren.addAll(typeAndNameElements);
178         try {
179             moduleElement.checkUnrecognisedElements(recognisedChildren);
180         } catch (final DocumentedException e) {
181             throw new DocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
182                     nameElement.getTextContent() + " - " +
183                     e.getMessage(), e.getErrorType(), e.getErrorTag(),e.getErrorSeverity(),e.getErrorInfo());
184         }
185         // TODO: add check for conflicts between global and local edit strategy
186         String perInstanceEditStrategy = moduleElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY,
187                 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
188
189         InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(
190                 retVal, defaultStrategy) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
191
192         resolveConfiguration(instanceConfigElementResolved, services, enumResolver);
193         return instanceConfigElementResolved;
194     }
195
196     private List<XmlElement> getConfigNodes(final XmlElement moduleElement, final String moduleNamespace, final String name,
197             final List<XmlElement> recognisedChildren, final List<XmlElement> typeAndName) throws DocumentedException {
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                     XmlMappingConstants.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
207             // config elements
208             foundConfigNodes.removeAll(typeAndName);
209             LOG.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
210                     moduleElement);
211         } else {
212             List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
213                     XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
214             foundWithoutNamespaceNodes.removeAll(typeAndName);
215             if (!foundWithoutNamespaceNodes.isEmpty()){
216                 throw new DocumentedException(String.format("Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
217                         foundWithoutNamespaceNodes),
218                         DocumentedException.ErrorType.APPLICATION,
219                         DocumentedException.ErrorTag.INVALID_VALUE,
220                         DocumentedException.ErrorSeverity.ERROR);
221             }
222         }
223
224         recognisedChildren.addAll(foundConfigNodes);
225         return foundConfigNodes;
226     }
227
228     private static Map<String, AttributeIfc> reverseMap(final Map<String, AttributeIfc> yangNameToAttr) {
229         Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
230
231         for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
232             reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
233         }
234
235         return reversednameToAtr;
236     }
237
238 }

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