Merge "Bug 2731: Discard changes only when transaction exist."
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / InstanceConfig.java
index b8870e51ce8bd0907655054a0b9c4566e1343cf4..ba7b2f20e44058d7b9af4663003d36e4654400ea 100644 (file)
@@ -9,13 +9,19 @@
 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
 
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-import com.google.common.collect.Multimap;
-import org.opendaylight.controller.config.util.ConfigRegistryClient;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import javax.management.ObjectName;
+import javax.management.openmbean.OpenType;
+import org.opendaylight.controller.config.util.BeanReader;
 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeReadingStrategy;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectXmlReader;
@@ -25,83 +31,82 @@ import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attrib
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.ObjectResolver;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.AttributeWritingStrategy;
 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
+import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
-import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
+import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-import javax.management.ObjectName;
-import javax.management.openmbean.OpenType;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
 public final class InstanceConfig {
-    private static final Logger logger = LoggerFactory.getLogger(InstanceConfig.class);
+    private static final Logger LOG = LoggerFactory.getLogger(InstanceConfig.class);
 
     private final Map<String, AttributeIfc> yangToAttrConfig;
+    private final String nullableDummyContainerName;
     private final Map<String, AttributeIfc> jmxToAttrConfig;
-    private final ConfigRegistryClient configRegistryClient;
+    private final BeanReader configRegistryClient;
+
+    public InstanceConfig(BeanReader configRegistryClient, Map<String, AttributeIfc> yangNamesToAttributes,
+                          String nullableDummyContainerName) {
 
-    public InstanceConfig(ConfigRegistryClient configRegistryClient, Map<String, AttributeIfc> yangNamesToAttributes) {
         this.yangToAttrConfig = yangNamesToAttributes;
+        this.nullableDummyContainerName = nullableDummyContainerName;
         this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
         this.configRegistryClient = configRegistryClient;
     }
 
-    private Map<String, Object> getMappedConfiguration(ObjectName on, ServiceRegistryWrapper depTracker) {
+    private Map<String, Object> getMappedConfiguration(ObjectName on) {
 
         // TODO make field, mappingStrategies can be instantiated only once
-        Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper(depTracker)
+        Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper()
                 .prepareMapping(jmxToAttrConfig);
 
         Map<String, Object> toXml = Maps.newHashMap();
 
         for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
-
             // Skip children runtime beans as they are mapped by InstanceRuntime
-            if (configDefEntry.getValue() instanceof RuntimeBeanEntry)
+            if (configDefEntry.getValue() instanceof RuntimeBeanEntry){
                 continue;
-
+            }
             Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
             try {
                 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
                         .get(configDefEntry.getKey());
                 Optional<?> a = attributeMappingStrategy.mapAttribute(value);
-                if (a.isPresent() == false)
+                if (!a.isPresent()){
                     continue;
-
+                }
                 toXml.put(configDefEntry.getValue().getAttributeYangName(), a.get());
             } catch (Exception e) {
                 throw new IllegalStateException("Unable to map value " + value + " to attribute "
                         + configDefEntry.getKey(), e);
             }
         }
-
         return toXml;
     }
 
-    public Element toXml(ObjectName on, ServiceRegistryWrapper depTracker, String namespace, Document document, Element rootElement) {
-
-        Element cfgElement = rootElement;
-
+    public Element toXml(ObjectName on, String namespace, Document document, Element rootElement) {
         Map<String, AttributeWritingStrategy> strats = new ObjectXmlWriter().prepareWriting(yangToAttrConfig, document);
-
-        Map<String, Object> mappedConfig = getMappedConfiguration(on, depTracker);
-
+        Map<String, Object> mappedConfig = getMappedConfiguration(on);
+        Element parentElement;
+        if (nullableDummyContainerName != null) {
+            Element dummyElement = XmlUtil.createElement(document, nullableDummyContainerName, Optional.of(namespace));
+            rootElement.appendChild(dummyElement);
+            parentElement = dummyElement;
+        } else {
+            parentElement = rootElement;
+        }
         for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
             try {
-                strats.get(mappingEntry.getKey()).writeElement(cfgElement, namespace, mappingEntry.getValue());
+                strats.get(mappingEntry.getKey()).writeElement(parentElement, namespace, mappingEntry.getValue());
             } catch (Exception e) {
                 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
                         + mappingEntry.getValue(), e);
             }
         }
-
-        return cfgElement;
+        return rootElement;
     }
 
     private void resolveConfiguration(InstanceConfigElementResolved mappedConfig, ServiceRegistryWrapper depTracker) {
@@ -116,7 +121,7 @@ public final class InstanceConfig {
             try {
                 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
                         .get(attributeName);
-                logger.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
+                LOG.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
 
                 value.resolveValue(attributeResolvingStrategy, attributeName);
                 value.setJmxName(
@@ -129,43 +134,70 @@ public final class InstanceConfig {
     }
 
     public InstanceConfigElementResolved fromXml(XmlElement moduleElement, ServiceRegistryWrapper services, String moduleNamespace,
-            EditStrategyType defaultStrategy, Multimap<String, String> providedServices) {
+                                                 EditStrategyType defaultStrategy,
+                                                 Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) throws NetconfDocumentedException {
         Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
 
-        Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig);
+        Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig, identityMap);
         List<XmlElement> recognisedChildren = Lists.newArrayList();
 
-        XmlElement type = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
-        XmlElement name = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
-        List<XmlElement> typeAndName = Lists.newArrayList(type, name);
+        XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
+        XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
+        List<XmlElement> typeAndNameElements = Lists.newArrayList(typeElement, nameElement);
+
+        // if dummy container was defined in yang, set moduleElement to its content
+        if (nullableDummyContainerName != null) {
+            int size = moduleElement.getChildElements().size();
+            int expectedChildNodes = 1 + typeAndNameElements.size();
+            if (size > expectedChildNodes) {
+                throw new NetconfDocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
+                        nameElement.getTextContent() + " - Expected " + expectedChildNodes +" child nodes, " +
+                        "one of them with name " + nullableDummyContainerName +
+                        ", got " + size + " elements.");
+            }
+            if (size == expectedChildNodes) {
+                try {
+                    moduleElement = moduleElement.getOnlyChildElement(nullableDummyContainerName, moduleNamespace);
+                } catch (NetconfDocumentedException e) {
+                    throw new NetconfDocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
+                            nameElement.getTextContent() + " - Expected child node with name " + nullableDummyContainerName +
+                            "." + e.getMessage());
+                }
+            } // else 2 elements, no need to descend
+        }
 
         for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
             List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(),
-                    recognisedChildren, typeAndName);
+                    recognisedChildren, typeAndNameElements);
             AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
             retVal.put(readStratEntry.getKey(), readElement);
         }
 
-        recognisedChildren.addAll(typeAndName);
-        moduleElement.checkUnrecognisedElements(recognisedChildren);
-
+        recognisedChildren.addAll(typeAndNameElements);
+        try {
+            moduleElement.checkUnrecognisedElements(recognisedChildren);
+        } catch (NetconfDocumentedException e) {
+            throw new NetconfDocumentedException("Error reading module " + typeElement.getTextContent() + " : " +
+                    nameElement.getTextContent() + " - " +
+                    e.getMessage(), e.getErrorType(), e.getErrorTag(),e.getErrorSeverity(),e.getErrorInfo());
+        }
         // TODO: add check for conflicts between global and local edit strategy
         String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
 
         InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(
-                retVal, defaultStrategy, providedServices) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy, providedServices);
+                retVal, defaultStrategy) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
 
         resolveConfiguration(instanceConfigElementResolved, services);
         return instanceConfigElementResolved;
     }
 
     private List<XmlElement> getConfigNodes(XmlElement moduleElement, String moduleNamespace, String name,
-            List<XmlElement> recognisedChildren, List<XmlElement> typeAndName) {
+            List<XmlElement> recognisedChildren, List<XmlElement> typeAndName) throws NetconfDocumentedException {
         List<XmlElement> foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name, moduleNamespace);
         if (foundConfigNodes.isEmpty()) {
-            logger.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
-            logger.debug("Trying lookup of config nodes without specified namespace");
+            LOG.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
+            LOG.debug("Trying lookup of config nodes without specified namespace");
             foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name,
                     XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
             // In case module type or name element is not present in config it
@@ -173,15 +205,19 @@ public final class InstanceConfig {
             // We need to remove config type and name from available module
             // config elements
             foundConfigNodes.removeAll(typeAndName);
-            logger.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
+            LOG.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
                     moduleElement);
         } else {
             List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
                     XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
             foundWithoutNamespaceNodes.removeAll(typeAndName);
-            Preconditions.checkState(foundWithoutNamespaceNodes.isEmpty(),
-                    "Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
-                    foundWithoutNamespaceNodes);
+            if (!foundWithoutNamespaceNodes.isEmpty()){
+                throw new NetconfDocumentedException(String.format("Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
+                        foundWithoutNamespaceNodes),
+                        NetconfDocumentedException.ErrorType.application,
+                        NetconfDocumentedException.ErrorTag.invalid_value,
+                        NetconfDocumentedException.ErrorSeverity.error);
+            }
         }
 
         recognisedChildren.addAll(foundConfigNodes);