BUG-4688: Disconnect Module and ModuleIdentifier 19/64519/4
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 19 Oct 2017 09:59:03 +0000 (11:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 19 Oct 2017 18:10:02 +0000 (20:10 +0200)
ModuleIdentifier is a utility used to identify module source, not really
a Module, since Modules are identified by various attributes in different
contexts. Disconnect these two interfaces so they can evolve independently.

Also eliminate SchemaContext.getAllModuleIdentifiers() and move its
funcitonality to SchemaContextUtil, where it can be applied to any
SchemaContext.

Change-Id: I4fd858d7b962d7dd219b6dc570c239d56028c6ff
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/Module.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/ModuleIdentifier.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaContext.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/FilteringSchemaContextProxy.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SimpleSchemaContext.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6961Test.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveSchemaContextTest.java

index 68ae7c6108b79dce326d3a44d76468e9bf9cc5e2..c0357940e7b4b13ecff05a52b2f2ace44ca7fbd9 100644 (file)
@@ -7,11 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.model.api;
 
+import java.net.URI;
+import java.util.Date;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import javax.annotation.concurrent.Immutable;
 import org.opendaylight.yangtools.concepts.SemVer;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 
 /**
@@ -61,12 +65,70 @@ import org.opendaylight.yangtools.yang.common.YangVersion;
     </code>
  */
 @Immutable
-public interface Module extends DataNodeContainer, ModuleIdentifier, NotificationNodeContainer {
+public interface Module extends DataNodeContainer, NotificationNodeContainer {
     /**
      * Default semantic version of Module.
      */
     SemVer DEFAULT_SEMANTIC_VERSION = SemVer.create(0, 0, 0);
 
+    /**
+     * Returns the name of the module which is specified as argument of YANG
+     * {@link Module <b><font color="#FF0000">module</font></b>} keyword.
+     *
+     * @return string with the name of the module
+     */
+    String getName();
+
+    /**
+     * Returns a {@link QNameModule}, which contains the namespace and
+     * the revision of the module.
+     *
+     * @return QNameModule identifier.
+     */
+    QNameModule getQNameModule();
+
+    /**
+     * Returns the namespace of the module which is specified as argument of
+     * YANG {@link Module <b><font color="#00FF00">namespace</font></b>}
+     * keyword. If you need both namespace and revision, please consider using
+     * {@link #getQNameModule()}.
+     *
+     * @return URI format of the namespace of the module
+     */
+    default URI getNamespace() {
+        return getQNameModule().getNamespace();
+    }
+
+    /**
+     * Returns the revision date for the module. If you need both namespace and
+     * revision, please consider using {@link #getQNameModule()}.
+     *
+     * @return date of the module revision which is specified as argument of
+     *         YANG {@link Module <b><font color="#339900">revison</font></b>}
+     *         keyword
+     */
+    // FIXME: BUG-4688: should return Optional<Revision>
+    @Nullable default Date getRevision() {
+        return getQNameModule().getRevision();
+    }
+
+    /**
+     * Returns the semantic version of yang module.
+     *
+     * <p>
+     * If the semantic version is not specified, default semantic version of
+     * module is returned.
+     *
+     * @return SemVer semantic version of yang module which is specified as
+     *         argument of
+     *         (urn:opendaylight:yang:extension:semantic-version?revision
+     *         =2016-02-02)semantic-version statement
+     */
+    // FIXME: Should return Optional<SemVer>
+    default SemVer getSemanticVersion() {
+        return Module.DEFAULT_SEMANTIC_VERSION;
+    }
+
     /**
      * Returns the prefix of the module.
      *
index fac4b62b4c0d9beb2f7994d96312fae562839ce7..aafbea9ce0cbeaee31648e290e6fa26a6d86c888 100644 (file)
@@ -12,8 +12,15 @@ import java.net.URI;
 import java.util.Date;
 import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 
-
+/**
+ * Module identifier. This "identifier" is deprecated and is to be removed in 2.0.0.
+ * @author Robert Varga
+ *
+ * @deprecated Use {@link SourceIdentifier} instead.
+ */
+@Deprecated
 public interface ModuleIdentifier {
     /**
      * Returns a {@link QNameModule}, which contains the namespace and
index 7ced70c351704157e7375633ff1314d4a92b759a..5faaa0628f0754c64923ace974601702e7d30cf1 100644 (file)
@@ -115,9 +115,4 @@ public interface SchemaContext extends ContainerSchemaNode {
         }
         return null;
     }
-
-    /**
-     * Get all module and submodule identifiers.
-     */
-    Set<ModuleIdentifier> getAllModuleIdentifiers();
 }
index 1fb765d11c136dbc35b80a0f92d93e9d73a980eb..47c6e1ab97586f9fdace51ffe751ab3970794ca1 100644 (file)
@@ -12,7 +12,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSet.Builder;
 import com.google.common.collect.ImmutableSetMultimap;
 import com.google.common.collect.Lists;
@@ -254,13 +253,7 @@ public final class FilteringSchemaContextProxy extends AbstractSchemaContext {
 
         @Override
         public String toString() {
-
             return String.format("ModuleId{name='%s', rev=%s}",name,rev);
         }
     }
-
-    @Override
-    public Set<ModuleIdentifier> getAllModuleIdentifiers() {
-        return ImmutableSet.copyOf(filteredModules);
-    }
 }
index 906fb2ccaf3e7229ca7bf0eff287cab650ff5c93..6eb4062712c3d0833f08879834ece069ee3c849e 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -32,6 +33,7 @@ import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
@@ -283,6 +285,31 @@ public final class SchemaContextUtil {
         return null;
     }
 
+    /**
+     * Extract the identifiers of all modules and submodules which were used to create a particular SchemaContext.
+     *
+     * @param context SchemaContext to be examined
+     * @return Set of ModuleIdentifiers.
+     */
+    public static Set<ModuleIdentifier> getConstituentModuleIdentifiers(final SchemaContext context) {
+        final Set<ModuleIdentifier> ret = new HashSet<>();
+
+        for (Module module : context.getModules()) {
+            ret.add(moduleToIdentifier(module));
+
+            for (Module submodule : module.getSubmodules()) {
+                ret.add(moduleToIdentifier(submodule));
+            }
+        }
+
+        return ret;
+    }
+
+    private static ModuleIdentifier moduleToIdentifier(final Module module) {
+        return ModuleIdentifierImpl.create(module.getName(), Optional.of(module.getNamespace()),
+            Optional.of(module.getRevision()));
+    }
+
     private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
 
         Preconditions.checkArgument(module != null, "Parent reference cannot be NULL");
index 3538763425e170c271b9bb7929f02b6a5cee503d..0968726442fcadbc9ecfcc0afb0aa3e583135493 100644 (file)
@@ -15,12 +15,9 @@ 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.HashSet;
-import java.util.Optional;
 import java.util.Set;
 import java.util.TreeMap;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 
 /**
  * Simple subclass of {@link AbstractSchemaContext} which performs some amount of indexing to speed up common
@@ -31,7 +28,6 @@ import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 public class SimpleSchemaContext extends AbstractSchemaContext {
     private final SetMultimap<URI, Module> namespaceToModules;
     private final SetMultimap<String, Module> nameToModules;
-    private final Set<ModuleIdentifier> moduleIdentifiers;
     private final Set<Module> modules;
 
     protected SimpleSchemaContext(final Set<Module> modules) {
@@ -53,18 +49,13 @@ public class SimpleSchemaContext extends AbstractSchemaContext {
             AbstractSchemaContext::createModuleSet);
         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(),
             AbstractSchemaContext::createModuleSet);
-        final Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
         for (Module m : modules) {
             nameMap.put(m.getName(), m);
             nsMap.put(m.getNamespace(), m);
-            modIdBuilder.add(ModuleIdentifierImpl.create(m.getName(), Optional.of(m.getNamespace()),
-                Optional.of(m.getRevision())));
-            resolveSubmoduleIdentifiers(m.getSubmodules(), modIdBuilder);
         }
 
         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
-        moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
     }
 
     /**
@@ -75,24 +66,11 @@ public class SimpleSchemaContext extends AbstractSchemaContext {
         return new SimpleSchemaContext(modules);
     }
 
-    private static void resolveSubmoduleIdentifiers(final Set<Module> submodules,
-            final Set<ModuleIdentifier> modIdBuilder) {
-        for (Module submodule : submodules) {
-            modIdBuilder.add(ModuleIdentifierImpl.create(submodule.getName(),
-                Optional.of(submodule.getNamespace()), Optional.of(submodule.getRevision())));
-        }
-    }
-
     @Override
     public final Set<Module> getModules() {
         return modules;
     }
 
-    @Override
-    public final Set<ModuleIdentifier> getAllModuleIdentifiers() {
-        return moduleIdentifiers;
-    }
-
     @Override
     public final String toString() {
         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
index 4a5c381874463684b86c3780a9d43292fc03d506..6c80f1147063e1697713cbfaa77afe5fd45ed0c8 100644 (file)
@@ -22,6 +22,7 @@ import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 import org.opendaylight.yangtools.yang.model.util.SimpleSchemaContext;
 
 public class Bug6961Test {
@@ -38,12 +39,13 @@ public class Bug6961Test {
         final Set<ModuleIdentifier> testSet = ImmutableSet.of(foo, sub1Foo, sub2Foo, bar, sub1Bar, baz);
         final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6961/");
         assertNotNull(context);
-        final Set<ModuleIdentifier> allModuleIdentifiers = context.getAllModuleIdentifiers();
+        final Set<ModuleIdentifier> allModuleIdentifiers = SchemaContextUtil.getConstituentModuleIdentifiers(context);
         assertNotNull(allModuleIdentifiers);
         assertEquals(6, allModuleIdentifiers.size());
         final SchemaContext schemaContext = SimpleSchemaContext.forModules(context.getModules());
         assertNotNull(schemaContext);
-        final Set<ModuleIdentifier> allModuleIdentifiersResolved = schemaContext.getAllModuleIdentifiers();
+        final Set<ModuleIdentifier> allModuleIdentifiersResolved = SchemaContextUtil.getConstituentModuleIdentifiers(
+            schemaContext);
         assertNotNull(allModuleIdentifiersResolved);
         assertEquals(6, allModuleIdentifiersResolved.size());
         assertEquals(allModuleIdentifiersResolved, allModuleIdentifiers);
index 4ae03bef60fac823e843bf5d11b7e1cb27cffb98..29d6d5768186d48d210664d10b10c5c609d347c5 100644 (file)
@@ -104,7 +104,7 @@ public class EffectiveSchemaContextTest {
                 "foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-21"));
         assertNotNull(fooModule);
 
-        assertEquals(3, schemaContext.getAllModuleIdentifiers().size());
+        assertEquals(3, schemaContext.getModules().size());
         assertEquals(3, ((EffectiveSchemaContext) schemaContext).getRootDeclaredStatements().size());
         assertEquals(3,((EffectiveSchemaContext) schemaContext).getRootEffectiveStatements().size());