Improved sorting of augmentations before code generation. 17/7217/5
authorMartin Vitez <mvitez@cisco.com>
Mon, 19 May 2014 13:36:24 +0000 (15:36 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 29 May 2014 04:35:34 +0000 (04:35 +0000)
Change-Id: I6471f4849fa57a717d85d61293305b0853b851cb
Signed-off-by: Martin Vitez <mvitez@cisco.com>
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.xtend
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/api/AugmentationSchemaBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/AugmentationSchemaBuilderImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/Comparators.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java

index ec1ec8fc6af0bed3c0ffb33ddae6fad66656ac92..82a191eb44288371129f55d67c3ae9b4be6d3320 100644 (file)
@@ -79,6 +79,7 @@ import org.opendaylight.yangtools.yang.binding.BindingMapping
 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase
 
 import com.google.common.collect.Sets
+import java.util.TreeSet
 
 public class BindingGeneratorImpl implements BindingGenerator {
 
@@ -364,19 +365,45 @@ public class BindingGeneratorImpl implements BindingGenerator {
         checkState(module.augmentations !== null, "Augmentations Set cannot be NULL.");
 
         val Set<AugmentationSchema> augmentations = module.augmentations;
-        val List<AugmentationSchema> sortedAugmentations = new ArrayList(augmentations);
+        var List<AugmentationSchema> sortedAugmentations = getSortedOrNull(augmentations)
+        if (sortedAugmentations != null) {
+            return sortedAugmentations
+        }
+        sortedAugmentations = new ArrayList(augmentations);
         Collections.sort(sortedAugmentations,
             [ augSchema1, augSchema2 |
-                if (augSchema1.targetPath.path.size() > augSchema2.targetPath.path.size()) {
-                    return 1;
-                } else if (augSchema1.targetPath.path.size() < augSchema2.targetPath.path.size()) {
-                    return -1;
+                val Iterator<QName> thisIt = augSchema1.targetPath.getPath().iterator();
+                val Iterator<QName> otherIt = augSchema2.getTargetPath().getPath().iterator();
+                while (thisIt.hasNext()) {
+                    if (otherIt.hasNext()) {
+                        val int comp = thisIt.next().compareTo(otherIt.next());
+                        if (comp != 0) {
+                            return comp
+                        }
+                    } else {
+                        return 1
+                    }
+                }
+                if (otherIt.hasNext()) {
+                    return -1
                 }
-                return 0;
+                return 0
             ]);
         return sortedAugmentations;
     }
 
+    private def List<AugmentationSchema> getSortedOrNull(Collection<AugmentationSchema> collection) {
+        val TreeSet<AugmentationSchema> set = new TreeSet()
+        for (e : collection) {
+            if (e instanceof Comparable<?>) {
+                set.add(e)
+            } else {
+                return null
+            }
+        }
+        return new ArrayList(set.toArray)
+    }
+
     /**
      * Converts whole <b>module</b> to <code>GeneratedType</code> object.
      * Firstly is created the module builder object from which is vally
index ebe587afef8d23fcc68f119036c677d7d09548e9..6b1e49e1ed1d954a2d53b13be978c454a416b2b2 100644 (file)
@@ -7,7 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.api;
 
-import org.opendaylight.yangtools.yang.model.api.*;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
 
 /**
  * Interface for builders of 'augment' statement.
@@ -80,4 +82,6 @@ public interface AugmentationSchemaBuilder extends DataNodeContainerBuilder {
      */
     void setResolved(boolean resolved);
 
+    int getOrder();
+
 }
index d908f93b0272d92d9c38bf6da3ef5f31f688ace5..3b75d1190cab0ccd863bf60c9883e9483e0f099d 100644 (file)
@@ -7,13 +7,17 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -36,13 +40,9 @@ import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-
 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
         AugmentationSchemaBuilder {
+    private final int order;
     private AugmentationSchemaImpl instance;
     private String whenCondition;
 
@@ -57,8 +57,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
     private boolean resolved;
     private AugmentationSchemaBuilder copyOf;
 
-    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
+    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, int order) {
         super(moduleName, line, null);
+        this.order = order;
         this.augmentTargetStr = augmentTargetStr;
         targetPath = ParserUtils.parseXPathString(augmentTargetStr);
     }
@@ -89,7 +90,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             return instance;
         }
 
-        instance = new AugmentationSchemaImpl(targetPath);
+        instance = new AugmentationSchemaImpl(targetPath, order);
 
         instance.description = description;
         instance.reference = reference;
@@ -227,6 +228,11 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         this.targetNodeSchemaPath = path;
     }
 
+    @Override
+    public int getOrder() {
+        return order;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 17;
@@ -282,7 +288,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         copyOf = old;
     }
 
-    private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware {
+    private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware,
+            Comparable<AugmentationSchemaImpl> {
+        private final int order;
         private SchemaPath targetPath;
         private RevisionAwareXPath whenCondition;
         private ImmutableSet<DataSchemaNode> childNodes;
@@ -296,8 +304,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         private ImmutableList<UnknownSchemaNode> unknownNodes;
         private AugmentationSchema copyOf;
 
-        private AugmentationSchemaImpl(final SchemaPath targetPath) {
+        public AugmentationSchemaImpl(final SchemaPath targetPath, final int order) {
             this.targetPath = targetPath;
+            this.order = order;
         }
 
         public void setCopyOf(final AugmentationSchema build) {
@@ -442,6 +451,26 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             sb.append("]");
             return sb.toString();
         }
+
+        @Override
+        public int compareTo(AugmentationSchemaImpl o) {
+            Iterator<QName> thisIt = this.targetPath.getPath().iterator();
+            Iterator<QName> otherIt = o.getTargetPath().getPath().iterator();
+            while (thisIt.hasNext()) {
+                if (otherIt.hasNext()) {
+                    int comp = thisIt.next().compareTo(otherIt.next());
+                    if (comp != 0) {
+                        return comp;
+                    }
+                } else {
+                    return 1;
+                }
+            }
+            if (otherIt.hasNext()) {
+                return -1;
+            }
+            return this.order - o.order;
+        }
     }
 
 }
index 1b6f19a0697da38d6e2011fa6336b88e85e5ae2a..e8716365d4adf6a88d16734074d1cd5055507ae2 100644 (file)
@@ -13,6 +13,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.Deque;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
@@ -82,7 +83,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
 
     private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
 
-    private final Set<AugmentationSchema> augments = new HashSet<>();
+    private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
     private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
     private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
 
@@ -523,8 +524,8 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return builder;
     }
 
-    public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
-        final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
+    public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr, final int order) {
+        final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr, order);
 
         Builder parent = getActualNode();
         builder.setParent(parent);
index cc4ac33d3afee232f834b229efdeb5cf1d557f95..bf7f399b10996f723083aaee0e2c8dd5a51cb131 100644 (file)
@@ -108,6 +108,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
 
     private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
     private final Stack<Stack<QName>> actualPath = new Stack<>();
+    private int augmentOrder;
 
     private void addNodeToPath(QName name) {
         actualPath.peek().push(name);
@@ -327,7 +328,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         enterLog(AUGMENT_STR, augmentPath, line);
         actualPath.push(new Stack<QName>());
 
-        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
+        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
@@ -610,7 +611,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String augmentPath = stringFromNode(ctx);
         enterLog(AUGMENT_STR, augmentPath, line);
 
-        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
+        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
index ed33bac1e9b631928c73b8e91f0e2bdee6597ebf..ed3ddd6afff815ebda5fe1fdbabf577f20325388 100644 (file)
@@ -7,21 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
-import java.net.URI;
 import java.util.Comparator;
-import java.util.Date;
-
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 
 public final class Comparators {
 
-    /**
-     * Comparator based on alphabetical order of qname's local name.
-     */
-    public static final QNameComparator QNAME_COMP = new QNameComparator();
-
     /**
      * Comparator based on alphabetical order of local name of SchemaNode's qname.
      */
@@ -35,61 +26,10 @@ public final class Comparators {
     private Comparators() {
     }
 
-    private static final class QNameComparator implements Comparator<QName> {
-        @Override
-        public int compare(QName o1, QName o2) {
-            return o1.getLocalName().compareTo(o2.getLocalName());
-        }
-    }
-
     private static final class SchemaNodeComparator implements Comparator<SchemaNode> {
         @Override
         public int compare(SchemaNode o1, SchemaNode o2) {
-            QName q1 = o1.getQName();
-            QName q2 = o2.getQName();
-            int result = q1.getLocalName().compareTo(q2.getLocalName());
-            if (result == 0) {
-                URI ns1 = q1.getNamespace();
-                URI ns2 = q2.getNamespace();
-                if (ns1 == null && ns2 == null) {
-                    Date rev1 = q1.getRevision();
-                    Date rev2 = q2.getRevision();
-
-                    if (rev1 == null && rev2 == null) {
-                        String p1 = q1.getPrefix();
-                        String p2 = q2.getPrefix();
-                        if (p1 == null && p2 == null) {
-                            throw new IllegalArgumentException("Failed to sort nodes: " + o1 + ", " + o2);
-                        }
-                        if (p1 == null || p2 == null) {
-                            if (p1 == null) {
-                                return -1;
-                            } else {
-                                return 1;
-                            }
-                        }
-                        return p1.compareTo(p2);
-                    }
-                    if (rev1 == null || rev2 == null) {
-                        if (rev1 == null) {
-                            return -1;
-                        } else {
-                            return -2;
-                        }
-                    }
-                    return rev1.compareTo(rev2);
-                }
-                if (ns1 == null || ns2 == null) {
-                    if (ns1 == null) {
-                        return -1;
-                    } else {
-                        return 1;
-                    }
-                }
-                return ns1.toString().compareTo(ns2.toString());
-            } else {
-                return result;
-            }
+            return o1.getQName().compareTo(o2.getQName());
         }
     }
 
index 311d506e0fa583b3f1fbfee65439d3774eefcda9..2b3e29bb7faf95abe89cf257a24835f4da7267f6 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.util;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
@@ -404,7 +403,7 @@ public final class CopyUtils {
 
     private static AugmentationSchemaBuilder copyAugment(final AugmentationSchemaBuilder old, final Builder newParent) {
         AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
-                newParent.getLine(), old.getTargetPathAsString());
+                newParent.getLine(), old.getTargetPathAsString(), old.getOrder());
         copy.setParent(newParent);
         copy.setCopyOf(old);
         copy.setDescription(old.getDescription());