BUG-576: modified parser to handle augmentation of extension instances.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / SchemaContextImpl.java
index 6f81e7adaf66bfa951ccda2a25f2ab4eec886ea8..319f189043f4743daedc326834caeea4c2e12d1d 100644 (file)
@@ -8,6 +8,29 @@
 package org.opendaylight.yangtools.yang.parser.impl;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSetMultimap;
+import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import javax.annotation.concurrent.Immutable;
+
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
@@ -26,29 +49,64 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
 
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeMap;
-
+@Immutable
 final class SchemaContextImpl implements SchemaContext {
-    private final Set<Module> modules;
+    private static final Comparator<Module> REVISION_COMPARATOR = new Comparator<Module>() {
+        @Override
+        public int compare(final Module o1, final Module o2) {
+            if (o2.getRevision() == null) {
+                return -1;
+            }
+
+            return o2.getRevision().compareTo(o1.getRevision());
+        }
+    };
+
+    private static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = new Supplier<TreeSet<Module>>() {
+        @Override
+        public TreeSet<Module> get() {
+            return new TreeSet<>(REVISION_COMPARATOR);
+        }
+    };
+
     private final Map<ModuleIdentifier, String> identifiersToSources;
+    private final SetMultimap<URI, Module> namespaceToModules;
+    private final SetMultimap<String, Module> nameToModules;
+    private final Set<Module> modules;
+
+    SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
+        this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
+
+        /*
+         * Instead of doing this on each invocation of getModules(), pre-compute
+         * it once and keep it around -- better than the set we got in.
+         */
+        this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
+
+        /*
+         * The most common lookup is from Namespace->Module.
+         *
+         * RESTCONF performs lookups based on module name only, where it wants
+         * to receive the latest revision
+         *
+         * Invest some quality time in building up lookup tables for both.
+         */
+        final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
+                new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
+        final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
+                new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
+        for (Module m : modules) {
+            nameMap.put(m.getName(), m);
+            nsMap.put(m.getNamespace(), m);
+        }
 
-    SchemaContextImpl(final Set<Module> modules, Map<ModuleIdentifier, String> identifiersToSources) {
-        this.modules = modules;
-        this.identifiersToSources = identifiersToSources;
+        namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
+        nameToModules = ImmutableSetMultimap.copyOf(nameMap);
     }
 
     @Override
     public Set<DataSchemaNode> getDataDefinitions() {
-        final Set<DataSchemaNode> dataDefs = new HashSet<DataSchemaNode>();
+        final Set<DataSchemaNode> dataDefs = new HashSet<>();
         for (Module m : modules) {
             dataDefs.addAll(m.getChildNodes());
         }
@@ -57,13 +115,12 @@ final class SchemaContextImpl implements SchemaContext {
 
     @Override
     public Set<Module> getModules() {
-        List<Module> sorted = ModuleDependencySort.sort(modules.toArray(new Module[modules.size()]));
-        return new LinkedHashSet<Module>(sorted);
+        return modules;
     }
 
     @Override
     public Set<NotificationDefinition> getNotifications() {
-        final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
+        final Set<NotificationDefinition> notifications = new HashSet<>();
         for (Module m : modules) {
             notifications.addAll(m.getNotifications());
         }
@@ -72,7 +129,7 @@ final class SchemaContextImpl implements SchemaContext {
 
     @Override
     public Set<RpcDefinition> getOperations() {
-        final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
+        final Set<RpcDefinition> rpcs = new HashSet<>();
         for (Module m : modules) {
             rpcs.addAll(m.getRpcs());
         }
@@ -81,7 +138,7 @@ final class SchemaContextImpl implements SchemaContext {
 
     @Override
     public Set<ExtensionDefinition> getExtensions() {
-        final Set<ExtensionDefinition> extensions = new HashSet<ExtensionDefinition>();
+        final Set<ExtensionDefinition> extensions = new HashSet<>();
         for (Module m : modules) {
             extensions.addAll(m.getExtensionSchemaNodes());
         }
@@ -90,53 +147,29 @@ final class SchemaContextImpl implements SchemaContext {
 
     @Override
     public Module findModuleByName(final String name, final Date revision) {
-        if (name != null) {
-            for (final Module module : modules) {
-                if (revision == null) {
-                    if (module.getName().equals(name)) {
-                        return module;
-                    }
-                } else if (module.getName().equals(name) && module.getRevision().equals(revision)) {
-                    return module;
-                }
+        for (final Module module : nameToModules.get(name)) {
+            if (revision == null || revision.equals(module.getRevision())) {
+                return module;
             }
         }
+
         return null;
     }
 
     @Override
     public Set<Module> findModuleByNamespace(final URI namespace) {
-        final Set<Module> ret = new HashSet<Module>();
-        if (namespace != null) {
-            for (final Module module : modules) {
-                if (module.getNamespace().equals(namespace)) {
-                    ret.add(module);
-                }
-            }
-        }
-        return ret;
+        final Set<Module> ret = namespaceToModules.get(namespace);
+        return ret == null ? Collections.<Module>emptySet() : ret;
     }
 
     @Override
-    public Module findModuleByNamespaceAndRevision(URI namespace, Date revision) {
-        if (namespace != null) {
-            Set<Module> modules = findModuleByNamespace(namespace);
-
-            if (revision == null) {
-                TreeMap<Date, Module> map = new TreeMap<Date, Module>();
-                for (Module module : modules) {
-                    map.put(module.getRevision(), module);
-                }
-                if (map.isEmpty()) {
-                    return null;
-                }
-                return map.lastEntry().getValue();
-            } else {
-                for (Module module : modules) {
-                    if (module.getRevision().equals(revision)) {
-                        return(module);
-                    }
-                }
+    public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
+        if (namespace == null) {
+            return null;
+        }
+        for (Module module : findModuleByNamespace(namespace)) {
+            if (revision == null || revision.equals(module.getRevision())) {
+                return module;
             }
         }
         return null;
@@ -224,27 +257,25 @@ final class SchemaContextImpl implements SchemaContext {
     }
 
     @Override
-    public DataSchemaNode getDataChildByName(QName name) {
-        DataSchemaNode result = null;
+    public DataSchemaNode getDataChildByName(final QName name) {
         for (Module module : modules) {
-            result = module.getDataChildByName(name);
+            final DataSchemaNode result = module.getDataChildByName(name);
             if (result != null) {
-                break;
+                return result;
             }
         }
-        return result;
+        return null;
     }
 
     @Override
-    public DataSchemaNode getDataChildByName(String name) {
-        DataSchemaNode result = null;
+    public DataSchemaNode getDataChildByName(final String name) {
         for (Module module : modules) {
-            result = module.getDataChildByName(name);
+            final DataSchemaNode result = module.getDataChildByName(name);
             if (result != null) {
-                break;
+                return result;
             }
         }
-        return result;
+        return null;
     }
 
     @Override
@@ -269,8 +300,15 @@ final class SchemaContextImpl implements SchemaContext {
     }
 
     @Override
-    public Optional<String> getModuleSource(ModuleIdentifier moduleIdentifier) {
+    public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
         String maybeSource = identifiersToSources.get(moduleIdentifier);
         return Optional.fromNullable(maybeSource);
     }
+
+    @Override
+    public String toString() {
+        return "SchemaContextImpl{" +
+                "modules=" + modules +
+                '}';
+    }
 }