Bug 6873: [Yang 1.1] Add support for "require-instance" in leafref
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SourceSpecificContext.java
index 7b30b53f3932d434a7a34ae22e9027e3a543c485..dadc28d952e0a8920f7e3bc64cf5ddc4cbb8ec7e 100644 (file)
@@ -11,8 +11,9 @@ import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableTable;
 import com.google.common.collect.Multimap;
+import com.google.common.collect.Table;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -21,13 +22,15 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Mutable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
-import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 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.meta.IdentifierNamespace;
@@ -39,6 +42,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Namesp
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementDefinitionNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
@@ -63,6 +67,7 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnionSpecificationImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc7950.LeafrefSpecificationRfc7950Support;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -82,11 +87,22 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
 
         @Override
         public StatementContextBase build() {
+            /*
+             * If root is null or root version is other than default,
+             * we need to create new root.
+             */
             if (root == null) {
                 root = new RootStatementContext(this, SourceSpecificContext.this);
+            } else if (!RootStatementContext.DEFAULT_VERSION.equals(root.getRootVersion())
+                    && inProgressPhase == ModelProcessingPhase.SOURCE_LINKAGE) {
+                root = new RootStatementContext(this, SourceSpecificContext.this, root.getRootVersion());
             } else {
-                Preconditions.checkState(root.getIdentifier().equals(createIdentifier()),
-                    "Root statement was already defined as %s.", root.getIdentifier());
+                final QName rootStatement = root.definition().getStatementName();
+                final String rootArgument = root.rawStatementArgument();
+
+                Preconditions.checkState(Objects.equals(getDefinition().getStatementName(), rootStatement)
+                    && Objects.equals(getRawArgument(), rootArgument),
+                    "Root statement was already defined as '%s %s'.", rootStatement, rootArgument);
             }
             root.resetLists();
             return root;
@@ -94,17 +110,19 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(SourceSpecificContext.class);
-    private static final Map<String, StatementSupport<?, ?, ?>> BUILTIN_TYPE_SUPPORTS =
-            ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
-            .put(TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
-            .put(TypeUtils.UNION, new UnionSpecificationImpl.Definition())
-            .put(TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
-            .put(TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
-            .put(TypeUtils.BITS, new BitsSpecificationImpl.Definition())
-            .put(TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
-            .put(TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition())
-            .build();
-    private static final QName TYPE = Rfc6020Mapping.TYPE.getStatementName();
+    private static final Table<YangVersion, String, StatementSupport<?, ?, ?>> BUILTIN_TYPE_SUPPORTS =
+            ImmutableTable.<YangVersion, String, StatementSupport<?, ?, ?>>builder()
+            .put(YangVersion.VERSION_1, TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
+            .put(YangVersion.VERSION_1, TypeUtils.UNION, new UnionSpecificationImpl.Definition())
+            .put(YangVersion.VERSION_1, TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
+            .put(YangVersion.VERSION_1, TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
+            .put(YangVersion.VERSION_1_1, TypeUtils.LEAF_REF, new LeafrefSpecificationRfc7950Support())
+            .put(YangVersion.VERSION_1, TypeUtils.BITS, new BitsSpecificationImpl.Definition())
+            .put(YangVersion.VERSION_1, TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
+            .put(YangVersion.VERSION_1, TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition())
+                    .build();
+
+    private static final QName TYPE = YangStmtMapping.TYPE.getStatementName();
 
     private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
     private final QNameToStatementDefinitionMap qNameToStmtDefMap = new QNameToStatementDefinitionMap();
@@ -147,7 +165,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
             }
         }
 
-        StatementDefinitionContext<?, ?, ?> def = currentContext.getStatementDefinition(name);
+        StatementDefinitionContext<?, ?, ?> def = currentContext.getStatementDefinition(getRootVersion(), name);
 
         if (def == null) {
             final StatementSupport<?, ?, ?> extension = qNameToStmtDefMap.get(name);
@@ -155,7 +173,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
                 def = new StatementDefinitionContext<>(extension);
             } else {
                 // type-body-stmts
-                def = resolveTypeBodyStmts(name.getLocalName());
+                def = resolveTypeBodyStmts(name.getLocalName(), getRootVersion());
             }
         } else if (current != null && current.definition().getRepresentingClass().equals(UnknownStatementImpl.class)) {
             /*
@@ -190,6 +208,15 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         return root;
     }
 
+    /**
+     * Return version of root statement context.
+     *
+     * @return version of root statement context
+     */
+    YangVersion getRootVersion() {
+        return root != null ? root.getRootVersion() : RootStatementContext.DEFAULT_VERSION;
+    }
+
     DeclaredStatement<?> buildDeclared() {
         return root.buildDeclared();
     }
@@ -200,8 +227,15 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
 
     void startPhase(final ModelProcessingPhase phase) {
         @Nullable final ModelProcessingPhase previousPhase = phase.getPreviousPhase();
-        Preconditions.checkState(Objects.equals(previousPhase, finishedPhase));
-        Preconditions.checkState(modifiers.get(previousPhase).isEmpty());
+        Verify.verify(Objects.equals(previousPhase, finishedPhase),
+            "Phase sequencing violation: previous phase should be %s, source %s has %s", previousPhase, source,
+            finishedPhase);
+
+        final Collection<ModifierImpl> previousModifiers = modifiers.get(previousPhase);
+        Preconditions.checkState(previousModifiers.isEmpty(),
+            "Previous phase %s has unresolved modifiers %s in source %s",
+            previousPhase, previousModifiers, source);
+
         inProgressPhase = phase;
         LOG.debug("Source {} started phase {}", source, phase);
     }
@@ -362,8 +396,14 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         }
     }
 
-    private static StatementDefinitionContext<?, ?, ?> resolveTypeBodyStmts(final String typeArgument) {
-        final StatementSupport<?, ?, ?> support = BUILTIN_TYPE_SUPPORTS.get(typeArgument);
+    private static StatementDefinitionContext<?, ?, ?> resolveTypeBodyStmts(final String typeArgument,
+            final YangVersion version) {
+        StatementSupport<?, ?, ?> support = BUILTIN_TYPE_SUPPORTS.get(version, typeArgument);
+
+        if (support == null) {
+            support = BUILTIN_TYPE_SUPPORTS.get(YangVersion.VERSION_1, typeArgument);
+        }
+
         return support == null ? null : new StatementDefinitionContext<>(support);
     }
 
@@ -396,7 +436,9 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
 
     private QNameToStatementDefinition stmtDef() {
         // regular YANG statements and extension supports added
-        qNameToStmtDefMap.putAll(currentContext.getSupportsForPhase(inProgressPhase).getDefinitions());
+        final StatementSupportBundle supportsForPhase = currentContext.getSupportsForPhase(inProgressPhase);
+        qNameToStmtDefMap.putAll(supportsForPhase.getCommonDefinitions());
+        qNameToStmtDefMap.putAll(supportsForPhase.getDefinitionsSpecificForVersion(getRootVersion()));
 
         // No further actions needed
         if (inProgressPhase != ModelProcessingPhase.FULL_DECLARATION) {
@@ -419,4 +461,8 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
 
         return qNameToStmtDefMap;
     }
+
+    public Set<YangVersion> getSupportedVersions() {
+        return currentContext.getSupportedVersions();
+    }
 }