Merge "Fixed bug in generation of return type for list key properties."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlDocumentUtils.java
index f60c3ca5c8361b14b71c1f40ceab37a0336c3ebb..3b020429cc55de3638dabd6d0f4619db56335b36 100644 (file)
@@ -10,8 +10,12 @@ package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 import static com.google.common.base.Preconditions.checkState;
 
 import java.net.URI;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Random;
 import java.util.Set;
 
 import javax.activation.UnsupportedDataTypeException;
@@ -22,14 +26,29 @@ import javax.xml.parsers.ParserConfigurationException;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.*;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -121,7 +140,7 @@ public class XmlDocumentUtils {
         } else {
             ret = doc.createElementNS(null, dataType.getLocalName());
         }
-        if (data instanceof AttributesContainer) {
+        if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
             for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
                 ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
                         attribute.getValue());
@@ -133,7 +152,6 @@ public class XmlDocumentUtils {
 
     public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
             DataSchemaNode schema, XmlCodecProvider codecProvider) {
-
         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
 
         if (baseType instanceof IdentityrefTypeDefinition) {
@@ -146,24 +164,104 @@ public class XmlDocumentUtils {
                 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
                 element.setTextContent(prefix + ":" + value.getLocalName());
             } else {
-                logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(), //
-                        baseType.getQName().getLocalName(), //
-                        node.getValue().getClass());
-                element.setTextContent(String.valueOf(node.getValue()));
+                Object value = node.getValue();
+                logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(),
+                        baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
+                if (value != null) {
+                    element.setTextContent(String.valueOf(value));
+                }
+            }
+        } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
+            if (node.getValue() instanceof InstanceIdentifier) {
+                // Map< key = namespace, value = prefix>
+                Map<String, String> prefixes = new HashMap<>();
+                InstanceIdentifier instanceIdentifier = (InstanceIdentifier) node.getValue();
+                StringBuilder textContent = new StringBuilder();
+                for (PathArgument pathArgument : instanceIdentifier.getPath()) {
+                    textContent.append("/");
+                    writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
+                    if (pathArgument instanceof NodeIdentifierWithPredicates) {
+                        Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
+
+                        for (QName keyValue : predicates.keySet()) {
+                            String predicateValue = String.valueOf(predicates.get(keyValue));
+                            textContent.append("[");
+                            writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
+                            textContent.append("='");
+                            textContent.append(predicateValue);
+                            textContent.append("'");
+                            textContent.append("]");
+                        }
+                    } else if (pathArgument instanceof NodeWithValue) {
+                        textContent.append("[.='");
+                        textContent.append(((NodeWithValue)pathArgument).getValue());
+                        textContent.append("'");
+                        textContent.append("]");
+                    }
+                }
+                element.setTextContent(textContent.toString());
+
+            } else {
+                Object value = node.getValue();
+                logger.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
+                        .getNamespace(), //
+                        baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
+                if (value != null) {
+                    element.setTextContent(String.valueOf(value));
+                }
             }
         } else {
             if (node.getValue() != null) {
-                try {
-                    String value = codecProvider.codecFor(baseType).serialize(node.getValue());
-                    element.setTextContent(value);
-                } catch (ClassCastException e) {
+                final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
+                if (codec != null) {
+                    try {
+                        final String text = codec.serialize(node.getValue());
+                        element.setTextContent(text);
+                    } catch (ClassCastException e) {
+                        logger.error("Provided node did not have type required by mapping. Using stream instead.", e);
+                        element.setTextContent(String.valueOf(node.getValue()));
+                    }
+                } else {
+                    logger.error("Failed to find codec for {}, falling back to using stream", baseType);
                     element.setTextContent(String.valueOf(node.getValue()));
-                    logger.error("Provided node did not have type required by mapping. Using stream instead. {}", e);
                 }
             }
         }
     }
 
+    private static void writeIdentifierWithNamespacePrefix(Element element, StringBuilder textContent, QName qName,
+            Map<String, String> prefixes) {
+        String namespace = qName.getNamespace().toString();
+        String prefix = prefixes.get(namespace);
+        if (prefix == null) {
+            prefix = qName.getPrefix();
+            if (prefix == null || prefix.isEmpty() || prefixes.containsValue(prefix)) {
+                prefix = generateNewPrefix(prefixes.values());
+            }
+        }
+
+        element.setAttribute("xmlns:" + prefix, namespace.toString());
+        textContent.append(prefix);
+        prefixes.put(namespace, prefix);
+
+        textContent.append(":");
+        textContent.append(qName.getLocalName());
+    }
+
+    private static String generateNewPrefix(Collection<String> prefixes) {
+        StringBuilder result = null;
+        Random random = new Random();
+        do {
+            result = new StringBuilder();
+            for (int i = 0; i < 4; i++) {
+                int randomNumber = 0x61 + (Math.abs(random.nextInt()) % 26);
+                result.append(Character.toChars(randomNumber));
+            }
+        } while (prefixes.contains(result.toString()));
+
+        return result.toString();
+    }
+
     public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
         TypeDefinition<?> superType = type;
         while (superType.getBaseType() != null) {
@@ -228,7 +326,13 @@ public class XmlDocumentUtils {
             XmlCodecProvider codecProvider) {
         TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
         String text = xmlElement.getTextContent();
-        Object value = codec.deserialize(text);
+        Object value;
+        if (codec != null) {
+            value = codec.deserialize(text);
+
+        } else {
+            value = xmlElement.getTextContent();
+        }
         return new SimpleNodeTOImpl<Object>(schema.getQName(), null, value);
     }
 
@@ -236,7 +340,13 @@ public class XmlDocumentUtils {
             XmlCodecProvider codecProvider) {
         TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
         String text = xmlElement.getTextContent();
-        Object value = codec.deserialize(text);
+        Object value;
+        if (codec != null) {
+            value = codec.deserialize(text);
+
+        } else {
+            value = xmlElement.getTextContent();
+        }
         return new SimpleNodeTOImpl<Object>(schema.getQName(), null, value);
     }
 
@@ -259,7 +369,7 @@ public class XmlDocumentUtils {
                 } else if (dsn instanceof ChoiceNode) {
                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
-                        if (foundDsn != null) {
+                        if (foundDsn != null && foundDsn.isPresent()) {
                             return foundDsn;
                         }
                     }
@@ -312,12 +422,69 @@ public class XmlDocumentUtils {
                                 toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
                     }
                 }
-                return Optional.<Node<?>> fromNullable(toDomNode(element));
+                return Optional.<Node<?>> fromNullable(toDomNode(input));
             }
 
         });
 
     }
+    
+    /**
+     * Converts XML Document containing notification data from Netconf device to
+     * Data DOM Nodes. <br>
+     * By specification defined in <a
+     * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
+     * there are xml elements containing notifications metadata, like eventTime
+     * or root notification element which specifies namespace for which is
+     * notification defined in yang model. Those elements MUST be stripped off
+     * notifications body. This method returns pure notification body which
+     * begins in element which is equal to notifications name defined in
+     * corresponding yang model. Rest of notification metadata are obfuscated,
+     * thus Data DOM contains only pure notification body.
+     * 
+     * @param document
+     *            XML Document containing notification body
+     * @param notifications
+     *            Notifications Definition Schema
+     * @return Data DOM Nodes containing xml notification body definition or
+     *         <code>null</code> if there is no NotificationDefinition with
+     *         Element with equal notification QName defined in XML Document.
+     */
+    public static CompositeNode notificationToDomNodes(final Document document,
+            final Optional<Set<NotificationDefinition>> notifications) {
+        if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
+            final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
+
+            for (int i = 0; i < originChildNodes.getLength(); i++) {
+                org.w3c.dom.Node child = originChildNodes.item(i);
+                if (child instanceof Element) {
+                    final Element childElement = (Element) child;
+                    final QName partialQName = qNameFromElement(childElement);
+                    final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
+                            notifications.get());
+                    if (notificationDef.isPresent()) {
+                        final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
+                        final List<Node<?>> domNodes = toDomNodes(childElement,
+                                Optional.<Set<DataSchemaNode>> fromNullable(dataNodes));
+                        return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    private static Optional<NotificationDefinition> findNotification(final QName notifName,
+            final Set<NotificationDefinition> notifications) {
+        if ((notifName != null) && (notifications != null)) {
+            for (final NotificationDefinition notification : notifications) {
+                if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
+                    return Optional.<NotificationDefinition>fromNullable(notification);
+                }
+            }
+        }
+        return Optional.<NotificationDefinition>absent();
+    }
 
     private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
         ImmutableList.Builder<T> ret = ImmutableList.<T> builder();