Merge "Make sure NormalizedNodeContainer uses Collection"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / NodeUtils.java
index 6aa9406d9e522bee5d51ba4834e393f572c4251d..10e79e2bf3d91ec9cf476967064a94cbf966a37c 100644 (file)
@@ -7,13 +7,20 @@
  */
 package org.opendaylight.yangtools.yang.data.impl;
 
+import com.google.common.base.Function;
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import java.util.AbstractMap.SimpleEntry;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Stack;
-
+import javax.annotation.Nonnull;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -22,13 +29,13 @@ import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.NodeModification;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
@@ -37,16 +44,22 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Element;
 
-import com.google.common.base.Joiner;
-import com.google.common.collect.Lists;
-
 /**
  * @author michal.rehak
- * 
+ *
+ * @deprecated Use {@link NormalizedNodes} instead.
  */
+@Deprecated
 public abstract class NodeUtils {
-
+    private static final Joiner DOT_JOINER = Joiner.on(".");
     private static final Logger LOG = LoggerFactory.getLogger(NodeUtils.class);
+    private static final Function<QName, String> LOCALNAME_FUNCTION = new Function<QName, String>() {
+        @Override
+        public String apply(final @Nonnull QName input) {
+            Preconditions.checkNotNull(input);
+            return input.getLocalName();
+        }
+    };
 
     /**
      *
@@ -57,7 +70,7 @@ public abstract class NodeUtils {
      * @param node
      * @return node path up till root node
      */
-    public static String buildPath(Node<?> node) {
+    public static String buildPath(final Node<?> node) {
         List<String> breadCrumbs = new ArrayList<>();
         Node<?> tmpNode = node;
         while (tmpNode != null) {
@@ -65,7 +78,7 @@ public abstract class NodeUtils {
             tmpNode = tmpNode.getParent();
         }
 
-        return Joiner.on(".").join(breadCrumbs);
+        return DOT_JOINER.join(breadCrumbs);
     }
 
     /**
@@ -73,7 +86,7 @@ public abstract class NodeUtils {
      * @return dom tree, containing same node structure, yang nodes are
      *         associated to dom nodes as user data
      */
-    public static org.w3c.dom.Document buildShadowDomTree(CompositeNode treeRootNode) {
+    public static org.w3c.dom.Document buildShadowDomTree(final CompositeNode treeRootNode) {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         org.w3c.dom.Document doc = null;
         try {
@@ -84,7 +97,7 @@ public abstract class NodeUtils {
             return null;
         }
 
-        Stack<SimpleEntry<org.w3c.dom.Node, Node<?>>> jobQueue = new Stack<>();
+        final Deque<SimpleEntry<org.w3c.dom.Node, Node<?>>> jobQueue = new ArrayDeque<>();
         jobQueue.push(new SimpleEntry<org.w3c.dom.Node, Node<?>>(doc, treeRootNode));
 
         while (!jobQueue.isEmpty()) {
@@ -96,7 +109,9 @@ public abstract class NodeUtils {
             itemEl.setUserData(USER_KEY_NODE, item, null);
             if (item instanceof SimpleNode<?>) {
                 Object value = ((SimpleNode<?>) item).getValue();
-                itemEl.setTextContent(String.valueOf(value));
+                if(value != null) {
+                    itemEl.setTextContent(String.valueOf(value));
+                }
             }
             if (item instanceof NodeModification) {
                 ModifyAction modificationAction = ((NodeModification) item).getModificationAction();
@@ -108,7 +123,7 @@ public abstract class NodeUtils {
             jointPlace.appendChild(itemEl);
 
             if (item instanceof CompositeNode) {
-                for (Node<?> child : ((CompositeNode) item).getChildren()) {
+                for (Node<?> child : ((CompositeNode) item).getValue()) {
                     jobQueue.push(new SimpleEntry<org.w3c.dom.Node, Node<?>>(itemEl, child));
                 }
             }
@@ -124,7 +139,7 @@ public abstract class NodeUtils {
      * @throws XPathExpressionException
      */
     @SuppressWarnings("unchecked")
-    public static <T> T findNodeByXpath(org.w3c.dom.Document doc, String xpathEx) throws XPathExpressionException {
+    public static <T> T findNodeByXpath(final org.w3c.dom.Document doc, final String xpathEx) throws XPathExpressionException {
         T userNode = null;
         XPathFactory xPathfactory = XPathFactory.newInstance();
         XPath xpath = xPathfactory.newXPath();
@@ -140,14 +155,14 @@ public abstract class NodeUtils {
 
     /**
      * build NodeMap, where key = qName; value = node
-     * 
+     *
      * @param value
      * @return map of children, where key = qName and value is list of children
      *         groupped by qName
      */
-    public static Map<QName, List<Node<?>>> buildNodeMap(List<Node<?>> value) {
-        Map<QName, List<Node<?>>> nodeMapTmp = new HashMap<>();
-        if (value == null || value.isEmpty()) {
+    public static Map<QName, List<Node<?>>> buildNodeMap(final List<Node<?>> value) {
+        Map<QName, List<Node<?>>> nodeMapTmp = Maps.newLinkedHashMap();
+        if (value == null) {
             throw new IllegalStateException("nodeList should not be null or empty");
         }
         for (Node<?> node : value) {
@@ -165,16 +180,16 @@ public abstract class NodeUtils {
      * @param context
      * @return map of lists, where key = path; value = {@link DataSchemaNode}
      */
-    public static Map<String, ListSchemaNode> buildMapOfListNodes(SchemaContext context) {
+    public static Map<String, ListSchemaNode> buildMapOfListNodes(final SchemaContext context) {
         Map<String, ListSchemaNode> mapOfLists = new HashMap<>();
 
-        Stack<DataSchemaNode> jobQueue = new Stack<>();
+        final Deque<DataSchemaNode> jobQueue = new ArrayDeque<>();
         jobQueue.addAll(context.getDataDefinitions());
 
         while (!jobQueue.isEmpty()) {
             DataSchemaNode dataSchema = jobQueue.pop();
             if (dataSchema instanceof ListSchemaNode) {
-                mapOfLists.put(schemaPathToPath(dataSchema.getPath().getPath()), (ListSchemaNode) dataSchema);
+                mapOfLists.put(schemaPathToPath(dataSchema.getPath().getPathFromRoot()), (ListSchemaNode) dataSchema);
             }
 
             if (dataSchema instanceof DataNodeContainer) {
@@ -189,22 +204,18 @@ public abstract class NodeUtils {
      * @param qNamesPath
      * @return path
      */
-    private static String schemaPathToPath(List<QName> qNamesPath) {
-        List<String> pathSeed = new ArrayList<>();
-        for (QName qNameItem : qNamesPath) {
-            pathSeed.add(qNameItem.getLocalName());
-        }
-        return Joiner.on(".").join(pathSeed);
+    private static String schemaPathToPath(final Iterable<QName> qNamesPath) {
+        return DOT_JOINER.join(Iterables.transform(qNamesPath, LOCALNAME_FUNCTION));
     }
 
     /**
      * add given node to it's parent's list of children
-     * 
+     *
      * @param newNode
      */
-    public static void fixParentRelation(Node<?> newNode) {
+    public static void fixParentRelation(final Node<?> newNode) {
         if (newNode.getParent() != null) {
-            List<Node<?>> siblings = newNode.getParent().getChildren();
+            List<Node<?>> siblings = newNode.getParent().getValue();
             if (!siblings.contains(newNode)) {
                 siblings.add(newNode);
             }
@@ -213,12 +224,12 @@ public abstract class NodeUtils {
 
     /**
      * crawl all children of given node and assign it as their parent
-     * 
+     *
      * @param parentNode
      */
-    public static void fixChildrenRelation(CompositeNode parentNode) {
-        if (parentNode.getChildren() != null) {
-            for (Node<?> child : parentNode.getChildren()) {
+    public static void fixChildrenRelation(final CompositeNode parentNode) {
+        if (parentNode.getValue() != null) {
+            for (Node<?> child : parentNode.getValue()) {
                 if (child instanceof AbstractNodeTO<?>) {
                     ((AbstractNodeTO<?>) child).setParent(parentNode);
                 }
@@ -231,7 +242,7 @@ public abstract class NodeUtils {
      * @param dataMap
      * @return list of values of map, found by given keys
      */
-    public static <T, K> List<K> collectMapValues(List<T> keys, Map<T, K> dataMap) {
+    public static <T, K> List<K> collectMapValues(final List<T> keys, final Map<T, K> dataMap) {
         List<K> valueSubList = new ArrayList<>();
         for (T key : keys) {
             valueSubList.add(dataMap.get(key));
@@ -244,7 +255,7 @@ public abstract class NodeUtils {
      * @param nodes
      * @return list of children in list of appropriate type
      */
-    public static List<Node<?>> buildChildrenList(Node<?>... nodes) {
+    public static List<Node<?>> buildChildrenList(final Node<?>... nodes) {
         return Lists.newArrayList(nodes);
     }