Merge "Fixed incorrect test location."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / ASTSchemaSource.java
index fb5aa9bff264afac3171b61f1c6290a7ad7712be..5ff245962e11d94856e82c40834863959186fc2a 100644 (file)
@@ -6,6 +6,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
+import com.google.common.annotations.Beta;
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 
@@ -27,15 +29,37 @@ import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
  * passes basic semantic validation and we were able to extract dependency
  * information.
  */
+@Beta
 public final class ASTSchemaSource implements SchemaSourceRepresentation {
+    public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
+        @Override
+        public SourceIdentifier apply(final ASTSchemaSource input) {
+            return input.getIdentifier();
+        }
+    };
+    public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
+        @Override
+        public YangModelDependencyInfo apply(final ASTSchemaSource input) {
+            return input.getDependencyInformation();
+        }
+    };
+    public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
+        @Override
+        public ParserRuleContext apply(final ASTSchemaSource input) {
+            return input.getAST();
+        }
+    };
+
     private final YangModelDependencyInfo depInfo;
     private final ParserRuleContext tree;
     private final SourceIdentifier id;
+    private final String text;
 
-    private ASTSchemaSource(final @Nonnull SourceIdentifier id, @Nonnull final ParserRuleContext tree, final @Nonnull YangModelDependencyInfo depInfo) {
+    private ASTSchemaSource(final @Nonnull SourceIdentifier id, @Nonnull final ParserRuleContext tree, final @Nonnull YangModelDependencyInfo depInfo, final @Nonnull String text) {
         this.depInfo = Preconditions.checkNotNull(depInfo);
         this.tree = Preconditions.checkNotNull(tree);
         this.id = Preconditions.checkNotNull(id);
+        this.text = Preconditions.checkNotNull(text);
     }
 
     /**
@@ -50,9 +74,28 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
     public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree) throws YangSyntaxErrorException {
         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
         final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
-        return new ASTSchemaSource(id, tree, depInfo);
+        return new ASTSchemaSource(id, tree, depInfo, null);
     }
 
+    /**
+     * 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
+     * @return A new representation instance.
+     * @throws YangSyntaxErrorException if we fail to extract dependency information.
+     *
+     * @deprecated Migration only, will be removed as soon as the migration is completed.
+     */
+    @Deprecated
+    public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
+        final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
+        final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
+        return new ASTSchemaSource(id, tree, depInfo, text);
+    }
+
+
     @Override
     public SourceIdentifier getIdentifier() {
         return id;
@@ -83,4 +126,17 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
     public @Nonnull 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
+    public @Nonnull String getYangText() {
+        return text;
+    }
 }