BUG-997: make sure we carry YANG text around 80/9580/1
authorRobert Varga <rovarga@cisco.com>
Fri, 1 Aug 2014 17:12:38 +0000 (19:12 +0200)
committerRobert Varga <rovarga@cisco.com>
Fri, 1 Aug 2014 17:14:56 +0000 (19:14 +0200)
Unfortunately we need the schema source for module builder, in order to
support netconf monitoring. This is not right, as that component should
be talking to the schema repository to get the text version of a
particular module.

Change-Id: I3360c5b060f63b415f6248ae6e05d41136247ed0
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/URLSchemaContextResolver.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ASTSchemaSource.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TextToASTTransformer.java

index f0363222e1c7723f0732eead795c3c30e632f4f9..8ee18e58a5a562d40bb65e0de26fef2b9ffebb58 100644 (file)
@@ -245,8 +245,7 @@ public class URLSchemaContextResolver {
                     walker.walk(yangModelParser, entry.getValue());
                     ModuleBuilder moduleBuilder = yangModelParser.getModuleBuilder();
 
-                    // FIXME: do we need to lug this around?
-                    // moduleBuilder.setSource(source);
+                    moduleBuilder.setSource(sources.get(entry.getKey()).iterator().next().getYangText());
                     sourceToBuilder.put(entry.getKey(), moduleBuilder);
                 }
                 LOG.debug("Modules ready for integration");
index fb5aa9bff264afac3171b61f1c6290a7ad7712be..ab353a04ad54c7b221e3bebbc194362d93e6cc6e 100644 (file)
@@ -31,11 +31,13 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
     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 +52,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 +104,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;
+    }
 }
index 5c43a53bb5ef1d143c524ffa68f1b877a2d05fe6..ce9d6a8ce9331ea2bf1d0149257cb17e46fc630b 100644 (file)
@@ -6,8 +6,11 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
+import com.google.common.base.Charsets;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
+import com.google.common.io.CharStreams;
+import com.google.common.io.InputSupplier;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListeningExecutorService;
@@ -80,7 +83,16 @@ public final class TextToASTTransformer implements SchemaSourceTransformer<YangT
                     walker.walk(validator, ctx);
                     LOG.debug("Model {} validated successfully", source);
 
-                    return ASTSchemaSource.create(source.getIdentifier().getName(), ctx);
+                    // Backwards compatibility
+                    final String text = CharStreams.toString(
+                            CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
+                                @Override
+                                public InputStream getInput() throws IOException {
+                                    return source.openStream();
+                                }
+                            }, Charsets.UTF_8));
+
+                    return ASTSchemaSource.create(source.getIdentifier().getName(), ctx, text);
                 }
             }
         }), MAPPER);