yang-parser-impl supports ASTSchemaSource 11/67811/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 1 Feb 2018 11:27:33 +0000 (12:27 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 1 Feb 2018 11:35:21 +0000 (12:35 +0100)
yang-parser-rfc7950 supports bridging parsing from ASTSchemaSource,
which is an intermediate product of parsing from YangTextSchemaSource.

Make sure we propagate this capability from YangParserImpl, so that
users can efficiently integrate with it. While we're in the area
teach YangStatementStreamSource how to instantiate itself from
an ASTSchemaSource and add ASTSchemaSource.toString().

Change-Id: I0b6440b6fc4e7522c70261cffcef0afafed8ba53
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-api/src/main/java/org/opendaylight/yangtools/yang/model/parser/api/YangParser.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/ASTSchemaSource.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/repo/YangStatementStreamSource.java

index ecd143798e6b09c62f7a403d695e4e3ee7dcd819..02db3878dff77cfd87662db901e81efeb78a2a92 100644 (file)
@@ -32,7 +32,8 @@ import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation
 public interface YangParser {
     /**
      * Return enumeration of concrete types of {@link SchemaSourceRepresentation} parsers created from this factory
-     * support. Users can use this
+     * support. Users can use this information prepare the source they have to a representation which will be accepted
+     * by this parser.
      *
      * @return Enumeration of supported schema source representations.
      */
index a101aa835fc9f9fcce4aa5953fb92c258ea44eab..36f44975be0868d1ddcafa3b214247b24f42a036 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YinDomSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
@@ -38,7 +39,8 @@ import org.xml.sax.SAXException;
 
 final class YangParserImpl implements YangParser {
     private static final Collection<Class<? extends SchemaSourceRepresentation>> REPRESENTATIONS = ImmutableList.of(
-        YangTextSchemaSource.class, YinDomSchemaSource.class, YinXmlSchemaSource.class, YinTextSchemaSource.class);
+        ASTSchemaSource.class, YangTextSchemaSource.class, YinDomSchemaSource.class, YinXmlSchemaSource.class,
+        YinTextSchemaSource.class);
 
     private final BuildAction buildAction;
 
@@ -109,7 +111,9 @@ final class YangParserImpl implements YangParser {
     private static StatementStreamSource sourceToStatementStream(final SchemaSourceRepresentation source)
             throws IOException, YangSyntaxErrorException {
         requireNonNull(source);
-        if (source instanceof YangTextSchemaSource) {
+        if (source instanceof ASTSchemaSource) {
+            return YangStatementStreamSource.create((ASTSchemaSource) source);
+        } else if (source instanceof YangTextSchemaSource) {
             return YangStatementStreamSource.create((YangTextSchemaSource) source);
         } else if (source instanceof YinDomSchemaSource) {
             return YinStatementStreamSource.create((YinDomSchemaSource) source);
index aba479bba015fbe9a09b31df599f80c553fa30a3..63e7a9998df50395ae39228676b725055850cf37 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
 import java.util.Optional;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -33,16 +34,16 @@ import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 public final class ASTSchemaSource implements SchemaSourceRepresentation {
     private final YangModelDependencyInfo depInfo;
     private final SemVerSourceIdentifier semVerId;
+    private final SourceIdentifier identifier;
     private final ParserRuleContext tree;
-    private final SourceIdentifier id;
     private final String symbolicName;
 
-    private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final SemVerSourceIdentifier semVerId,
+    private ASTSchemaSource(@Nonnull final SourceIdentifier identifier, @Nonnull final SemVerSourceIdentifier semVerId,
             @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo,
             @Nullable final String symbolicName) {
         this.depInfo = requireNonNull(depInfo);
         this.tree = requireNonNull(tree);
-        this.id = requireNonNull(id);
+        this.identifier = requireNonNull(identifier);
         this.semVerId = requireNonNull(semVerId);
         this.symbolicName = symbolicName;
     }
@@ -80,7 +81,7 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
 
     @Override
     public SourceIdentifier getIdentifier() {
-        return id;
+        return identifier;
     }
 
     @Override
@@ -118,6 +119,11 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
         return depInfo;
     }
 
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this).add("identifier", identifier).toString();
+    }
+
     private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
         final String name = depInfo.getName();
         return depInfo.getFormattedRevision() == null ? RevisionSourceIdentifier.create(name)
index e14ef46abf3671660aecb59b99d749e713a310b2..3921192c91605859726a64c815051498d856031f 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
@@ -73,6 +75,14 @@ public final class YangStatementStreamSource implements StatementStreamSource {
         this.context = Preconditions.checkNotNull(context);
     }
 
+    /**
+     * Create a {@link YangStatementStreamSource} for a {@link YangTextSchemaSource}.
+     *
+     * @param source YangTextSchemaSource, must not be null
+     * @return A new {@link YangStatementStreamSource}
+     * @throws IOException When we fail to read the source
+     * @throws YangSyntaxErrorException If the source fails basic parsing
+     */
     public static YangStatementStreamSource create(final YangTextSchemaSource source) throws IOException,
             YangSyntaxErrorException {
         final StatementContext context;
@@ -85,6 +95,19 @@ public final class YangStatementStreamSource implements StatementStreamSource {
         return new YangStatementStreamSource(source.getIdentifier(), parser, context);
     }
 
+    /**
+     * Create a {@link YangStatementStreamSource} for a {@link ASTSchemaSource}.
+     *
+     * @param source YangTextSchemaSource, must not be null
+     * @return A new {@link YangStatementStreamSource}
+     */
+    public static YangStatementStreamSource create(final ASTSchemaSource source) {
+        final ParserRuleContext ast = source.getAST();
+        checkArgument(ast instanceof StatementContext,
+                "Unsupported context class %s for source %s", ast.getClass(), source.getIdentifier());
+        return create(source.getIdentifier(), (StatementContext) ast, source.getSymbolicName().orElse(null));
+    }
+
     public static YangStatementStreamSource create(final SourceIdentifier identifier, final StatementContext context,
         final String symbolicName) {
         return new YangStatementStreamSource(identifier, new YangStatementParserListenerImpl(symbolicName), context);