Fixed YANG union type parsing. Changed module builders storing from HashMap to Linked... 47/347/1
authorMartin Vitez <mvitez@cisco.com>
Wed, 15 May 2013 15:44:29 +0000 (17:44 +0200)
committerMartin Vitez <mvitez@cisco.com>
Wed, 15 May 2013 15:44:29 +0000 (17:44 +0200)
Signed-off-by: Martin Vitez <mvitez@cisco.com>
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/builder/impl/UnionTypeBuilder.java
opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/main/java/org/opendaylight/controller/yang/parser/impl/YangParserImpl.java

index fd9a3aa..a184e23 100644 (file)
@@ -33,10 +33,14 @@ import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder
  */
 public class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
         TypeDefinitionBuilder, Builder {
+    private final static String CLASS_NAME = UnionTypeBuilder.class
+            .getSimpleName();
+
     private final int line;
     private final List<TypeDefinition<?>> types;
     private final List<TypeDefinitionBuilder> typedefs;
     private final UnionType instance;
+    private boolean built;
 
     private final List<String> actualPath;
     private final URI namespace;
@@ -89,40 +93,43 @@ public class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
 
     @Override
     public UnionType build() {
-        for (TypeDefinitionBuilder tdb : typedefs) {
-            types.add(tdb.build());
+        if (built) {
+            return instance;
+        } else {
+            for (TypeDefinitionBuilder tdb : typedefs) {
+                types.add(tdb.build());
+            }
+            built = true;
+            return instance;
         }
-        return instance;
     }
 
     @Override
     public void setPath(final SchemaPath schemaPath) {
-        throw new IllegalStateException("Can not set path to "
-                + UnionTypeBuilder.class.getSimpleName());
+        throw new IllegalStateException("Can not set path to " + CLASS_NAME);
     }
 
     @Override
     public void setDescription(final String description) {
         throw new IllegalStateException("Can not set description to "
-                + UnionTypeBuilder.class.getSimpleName());
+                + CLASS_NAME);
     }
 
     @Override
     public void setReference(final String reference) {
         throw new IllegalStateException("Can not set reference to "
-                + UnionTypeBuilder.class.getSimpleName());
+                + CLASS_NAME);
     }
 
     @Override
     public void setStatus(final Status status) {
-        throw new IllegalStateException("Can not set status to "
-                + UnionTypeBuilder.class.getSimpleName());
+        throw new IllegalStateException("Can not set status to " + CLASS_NAME);
     }
 
     @Override
     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
         throw new IllegalStateException("Can not add unknown node to "
-                + UnionTypeBuilder.class.getSimpleName());
+                + CLASS_NAME);
     }
 
     @Override
index 42b348a..7c2aa66 100644 (file)
@@ -19,6 +19,8 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -122,7 +124,7 @@ public class YangParserImpl implements YangModelParser {
 
     private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuilders(
             final List<InputStream> yangFileStreams) {
-        final Map<String, TreeMap<Date, ModuleBuilder>> modules = new HashMap<String, TreeMap<Date, ModuleBuilder>>();
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = new LinkedHashMap<String, TreeMap<Date, ModuleBuilder>>();
         final ParseTreeWalker walker = new ParseTreeWalker();
         final List<ParseTree> trees = parseStreams(yangFileStreams);
         final ModuleBuilder[] builders = new ModuleBuilder[trees.size()];
@@ -140,7 +142,22 @@ public class YangParserImpl implements YangModelParser {
         // module dependency graph sorted
         List<ModuleSimple> sorted = new ModuleDependencySort(builders).sort();
 
-        for (ModuleBuilder builder : builders) {
+        // TODO FIX THIS ASAP!
+        // FIXME this is just temp workaround the ModuleDependencySort MUST
+        // RETURN ordered List
+        // of SORTED and DEPENDECNY RESOLVED MODULE BUILDERS!!!!!!
+        final List<ModuleBuilder> orderedBuilders = new ArrayList<ModuleBuilder>();
+        for (final ModuleSimple ms : sorted) {
+            for (int i = 0; i < builders.length; ++i) {
+                if (ms.getName().equals(builders[i].getName())
+                        && ms.getRevision().equals(builders[i].getRevision())) {
+                    orderedBuilders.add(builders[i]);
+                }
+            }
+        }
+        // FIXME END OF WORKAROUND
+
+        for (ModuleBuilder builder : orderedBuilders) {
             final String builderName = builder.getName();
             Date builderRevision = builder.getRevision();
             if (builderRevision == null) {
@@ -193,7 +210,7 @@ public class YangParserImpl implements YangModelParser {
         resolveAugments(modules);
 
         // build
-        final Set<Module> result = new HashSet<Module>();
+        final Set<Module> result = new LinkedHashSet<Module>();
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules
                 .entrySet()) {
             final Map<Date, Module> modulesByRevision = new HashMap<Date, Module>();
@@ -485,7 +502,7 @@ public class YangParserImpl implements YangModelParser {
         List<PatternConstraint> patterns = Collections.emptyList();
         Integer fractionDigits = null;
         if (referencedType == null) {
-            final TypeDefinitionBuilder tdb = (TypeDefinitionBuilder) nodeToResolve;
+            final TypeDefinitionBuilder tdb = nodeToResolve.getTypedef();
             ranges = tdb.getRanges();
             constraints.addRanges(ranges);
             lengths = tdb.getLengths();
@@ -657,7 +674,7 @@ public class YangParserImpl implements YangModelParser {
             for (AugmentationSchemaBuilder augmentBuilder : module
                     .getAddedAugments()) {
 
-                if(!augmentBuilder.isResolved()) {
+                if (!augmentBuilder.isResolved()) {
                     final SchemaPath augmentTargetSchemaPath = augmentBuilder
                             .getTargetPath();
                     final List<QName> path = augmentTargetSchemaPath.getPath();
@@ -675,7 +692,8 @@ public class YangParserImpl implements YangModelParser {
                     for (DataSchemaNodeBuilder child : dependentModule
                             .getChildNodes()) {
                         final QName childQName = child.getQName();
-                        if (childQName.getLocalName().equals(qname.getLocalName())) {
+                        if (childQName.getLocalName().equals(
+                                qname.getLocalName())) {
                             currentParent = child;
                             i++;
                             break;
@@ -702,7 +720,8 @@ public class YangParserImpl implements YangModelParser {
                     }
 
                     final QName currentQName = currentParent.getQName();
-                    final QName lastAugmentPathElement = path.get(path.size() - 1);
+                    final QName lastAugmentPathElement = path
+                            .get(path.size() - 1);
                     if (currentQName.getLocalName().equals(
                             lastAugmentPathElement.getLocalName())) {
                         ParserUtils.fillAugmentTarget(augmentBuilder,
@@ -710,13 +729,13 @@ public class YangParserImpl implements YangModelParser {
                         ((AugmentationTargetBuilder) currentParent)
                                 .addAugmentation(augmentBuilder);
                         SchemaPath oldPath = currentParent.getPath();
-                        augmentBuilder.setTargetPath(new SchemaPath(oldPath.getPath(), oldPath.isAbsolute()));
+                        augmentBuilder.setTargetPath(new SchemaPath(oldPath
+                                .getPath(), oldPath.isAbsolute()));
                         augmentBuilder.setResolved(true);
                         module.augmentResolved();
                     }
                 }
 
-
             }
         }
     }
@@ -1130,9 +1149,13 @@ public class YangParserImpl implements YangModelParser {
 
         @Override
         public Module findModuleByName(final String name, final Date revision) {
-            if ((name != null) && (revision != null)) {
+            if (name != null) {
                 for (final Module module : modules) {
-                    if (module.getName().equals(name)
+                    if (revision == null) {
+                        if (module.getName().equals(name)) {
+                            return module;
+                        }
+                    } else if (module.getName().equals(name)
                             && module.getRevision().equals(revision)) {
                         return module;
                     }