Add the ability to build effective model 99/68399/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Feb 2018 09:37:11 +0000 (10:37 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 19 Feb 2018 22:41:37 +0000 (23:41 +0100)
The effective model consists of ModuleEffectiveStatements
which are guaranteed to have unique QNameModules. Express
that in YangParser.

Change-Id: Id094c40cf5dcf49e6289d3b929f4abf2f9cb6436
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-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/EffectiveSchemaContext.java

index 02db3878dff77cfd87662db901e81efeb78a2a92..1560104c19d59dbba181bf51cb54dd9627553dcf 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.collect.SetMultimap;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import javax.annotation.Nonnull;
 import javax.annotation.concurrent.NotThreadSafe;
@@ -19,6 +20,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 
 /**
@@ -138,9 +140,23 @@ public interface YangParser {
      * Build the declared view of a combined view of declared statements.
      *
      * @return Ordered collection of declared statements from requested sources.
+     * @throws YangSyntaxErrorException When a syntactic error is encountered.
      */
     List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException;
 
+    /**
+     * Build the effective view of a combined view of effective statements. Note that this representation, unlike
+     * {@link #buildDeclaredModel()} does not expose submodules as top-level contracts. These are available from their
+     * respective parent modules.
+     *
+     * @return Effective module statements indexed by their QNameModule.
+     * @throws YangSyntaxErrorException When a syntactic error is encountered.
+     */
+    // FIXME: 3.0.0: Make this method non-default
+    default Map<QNameModule, ModuleEffectiveStatement> buildEffectiveModel() throws YangParserException {
+        throw new UnsupportedOperationException(getClass() + " does not implement buildEffectiveModel()");
+    }
+
     /**
      * Build effective {@link SchemaContext}
      *
index 36f44975be0868d1ddcafa3b214247b24f42a036..c8faca678e132a6f789cf7604d12fb653c99ca88 100644 (file)
@@ -7,19 +7,24 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
+import static com.google.common.collect.ImmutableMap.toImmutableMap;
 import static java.util.Objects.requireNonNull;
+import static java.util.function.Function.identity;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.SetMultimap;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import javax.xml.transform.TransformerException;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
@@ -94,6 +99,20 @@ final class YangParserImpl implements YangParser {
         }
     }
 
+    @Override
+    public Map<QNameModule, ModuleEffectiveStatement> buildEffectiveModel() throws YangParserException {
+        final List<EffectiveStatement<?, ?>> effectiveStatements;
+        try {
+            effectiveStatements = buildAction.buildEffective().getRootEffectiveStatements();
+        } catch (ReactorException e) {
+            throw decodeReactorException(e);
+        }
+
+        return effectiveStatements.stream()
+                .filter(ModuleEffectiveStatement.class::isInstance).map(ModuleEffectiveStatement.class::cast)
+                .collect(toImmutableMap(ModuleEffectiveStatement::localQNameModule, identity()));
+    }
+
     @Override
     public SchemaContext buildSchemaContext() throws YangParserException {
         try {
index b284ffa602d9bb6cf3122cf1f16902e6c23d0c3c..f2c9eb731f14ef4c77329db5454317c90f3ff61b 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
+import com.google.common.annotations.Beta;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableList;
@@ -49,7 +50,7 @@ public final class EffectiveSchemaContext extends SimpleSchemaContext {
         return rootDeclaredStatements;
     }
 
-    @VisibleForTesting
+    @Beta
     public List<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
         return rootEffectiveStatements;
     }