Revert "Bug 4640: Change semantic-version to openconfig-version"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / ASTSchemaSource.java
index ab57ab74f9b57a4ae99376eab334885a65bbae77..44aaeba0bd208df7f8a3b31826e597e9c35de093 100644 (file)
@@ -7,13 +7,19 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
+import static org.opendaylight.yangtools.yang.model.api.Module.DEFAULT_SEMANTIC_VERSION;
+
 import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
+import java.util.Optional;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
 
@@ -29,38 +35,32 @@ import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
  */
 @Beta
 public final class ASTSchemaSource implements SchemaSourceRepresentation {
-    public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
-        @Override
-        public SourceIdentifier apply(@Nonnull final ASTSchemaSource input) {
-            Preconditions.checkNotNull(input);
-            return input.getIdentifier();
-        }
-    };
-    public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
-        @Override
-        public YangModelDependencyInfo apply(@Nonnull final ASTSchemaSource input) {
-            Preconditions.checkNotNull(input);
-            return input.getDependencyInformation();
-        }
-    };
-    public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
-        @Override
-        public ParserRuleContext apply(@Nonnull final ASTSchemaSource input) {
-            Preconditions.checkNotNull(input);
-            return input.getAST();
-        }
-    };
+    @Deprecated
+    public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER =
+        ASTSchemaSource::getIdentifier;
+    @Deprecated
+    public static final Function<ASTSchemaSource, SourceIdentifier> GET_SEMVER_IDENTIFIER =
+        ASTSchemaSource::getSemVerIdentifier;
+    @Deprecated
+    public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO =
+        ASTSchemaSource::getDependencyInformation;
+    @Deprecated
+    public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = ASTSchemaSource::getAST;
 
     private final YangModelDependencyInfo depInfo;
+    private final SemVerSourceIdentifier semVerId;
     private final ParserRuleContext tree;
     private final SourceIdentifier id;
-    private final String text;
+    private final String symbolicName;
 
-    private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo, final String text) {
+    private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final SemVerSourceIdentifier semVerId,
+            @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo,
+            @Nullable final String symbolicName) {
         this.depInfo = Preconditions.checkNotNull(depInfo);
         this.tree = Preconditions.checkNotNull(tree);
         this.id = Preconditions.checkNotNull(id);
-        this.text = text;
+        this.semVerId = Preconditions.checkNotNull(semVerId);
+        this.symbolicName = symbolicName;
     }
 
     /**
@@ -72,36 +72,100 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
      * @return A new representation instance.
      * @throws YangSyntaxErrorException if we fail to extract dependency information.
      */
-    public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
+    public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree)
+            throws YangSyntaxErrorException {
         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
         final SourceIdentifier id = getSourceId(depInfo);
-        return new ASTSchemaSource(id, tree, depInfo, null);
+        final SemVerSourceIdentifier semVerId = getSemVerSourceId(depInfo);
+        return new ASTSchemaSource(id, semVerId, tree, depInfo, null);
     }
 
     private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
         final String name = depInfo.getName();
+        return depInfo.getFormattedRevision() == null ? RevisionSourceIdentifier.create(name)
+                : RevisionSourceIdentifier.create(name, depInfo.getFormattedRevision());
+    }
+
+    private static SemVerSourceIdentifier getSemVerSourceId(final YangModelDependencyInfo depInfo) {
         return depInfo.getFormattedRevision() == null
-                ? new SourceIdentifier(name)
-                : new SourceIdentifier(name, depInfo.getFormattedRevision());
+                ? SemVerSourceIdentifier.create(depInfo.getName(),
+                    depInfo.getSemanticVersion().or(DEFAULT_SEMANTIC_VERSION))
+                        : SemVerSourceIdentifier.create(depInfo.getName(), depInfo.getFormattedRevision(),
+                            depInfo.getSemanticVersion().or(DEFAULT_SEMANTIC_VERSION));
     }
 
     /**
      * Create a new instance of AST representation for a abstract syntax tree,
      * performing minimal semantic analysis to acquire dependency information.
      *
-     * @param name YANG source name. Used only for error reporting.
-     * @param tree ANTLR abstract syntax tree
-     * @param text YANG text source
+     * @param identifier
+     *            SourceIdentifier of yang schema source.
+     * @param tree
+     *            ANTLR abstract syntax tree
+     * @param text
+     *            YANG text source
      * @return A new representation instance.
-     * @throws YangSyntaxErrorException if we fail to extract dependency information.
+     * @throws YangSyntaxErrorException
+     *             if we fail to extract dependency information.
      *
-     * @deprecated Migration only, will be removed as soon as the migration is completed.
+     * @deprecated Use {@link #create(SourceIdentifier, ParserRuleContext)} instead.
      */
     @Deprecated
-    public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
-        final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
+    public static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
+            @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
+        return create(identifier, tree);
+    }
+
+    /**
+     * Create a new instance of AST representation for a abstract syntax tree, performing minimal semantic analysis
+     * to acquire dependency information.
+     *
+     * @param identifier
+     *            SourceIdentifier of yang schema source.
+     * @param tree
+     *            ANTLR abstract syntax tree
+     * @return A new representation instance.
+     * @throws YangSyntaxErrorException
+     *             if we fail to extract dependency information.
+     */
+    public static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
+            @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
+        return create(identifier, null, tree);
+    }
+
+    /**
+     * Create a new instance of AST representation for a abstract syntax tree, performing minimal semantic analysis
+     * to acquire dependency information.
+     *
+     * @param symbolicName
+     *            Symbolic name
+     * @param identifier
+     *            SourceIdentifier of yang schema source.
+     * @param tree
+     *            ANTLR abstract syntax tree
+     * @return A new representation instance.
+     * @throws YangSyntaxErrorException
+     *             if we fail to extract dependency information.
+     */
+    public static ASTSchemaSource create(@Nonnull final String symbolicName, @Nonnull final SourceIdentifier identifier,
+            @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
+        return create(identifier, symbolicName, tree);
+    }
+
+    private static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
+            @Nullable final String symbolicName, @Nonnull final ParserRuleContext tree)
+                    throws YangSyntaxErrorException {
+        final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(identifier.getName(), tree);
         final SourceIdentifier id = getSourceId(depInfo);
-        return new ASTSchemaSource(id, tree, depInfo, text);
+
+        final SemVerSourceIdentifier semVerId;
+        if (identifier instanceof SemVerSourceIdentifier && !depInfo.getSemanticVersion().isPresent()) {
+            semVerId = (SemVerSourceIdentifier) identifier;
+        } else {
+            semVerId = getSemVerSourceId(depInfo);
+        }
+
+        return new ASTSchemaSource(id, semVerId, tree, depInfo, symbolicName);
     }
 
 
@@ -110,6 +174,16 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
         return id;
     }
 
+    @Override
+    public Optional<String> getSymbolicName() {
+        return Optional.ofNullable(symbolicName);
+    }
+
+    public SemVerSourceIdentifier getSemVerIdentifier() {
+        return semVerId;
+    }
+
+    @Nonnull
     @Override
     public Class<? extends SchemaSourceRepresentation> getType() {
         return ASTSchemaSource.class;
@@ -135,17 +209,4 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
     @Nonnull public YangModelDependencyInfo getDependencyInformation() {
         return depInfo;
     }
-
-    /**
-     * Return the semantically-equivalent text YANG text source.
-     *
-     * @return YANG text source
-     * @deprecated Used for migration purposes. Users are advised to use the
-     *             schema repository to acquire the representation of their
-     *             choice. Will be removed as soon as the migration is completed.
-     */
-    @Deprecated
-    @Nonnull public String getYangText() {
-        return text;
-    }
 }