BUG-6497: Do not lose augmentation statement order
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / IdentityRefSpecificationImpl.java
index bd2b1e7c0a67a5a05fdcd86f7073d5b93697edc8..2deb59dc921133516e27ae36c1d72de7e9855275 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,17 +7,27 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import com.google.common.base.Preconditions;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
+import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityRefSpecificationEffectiveStatementImpl;
 
-public class IdentityRefSpecificationImpl extends
-        AbstractDeclaredStatement<String> implements
-        TypeStatement.IdentityRefSpecification {
+public class IdentityRefSpecificationImpl extends AbstractDeclaredStatement<String> implements TypeStatement.IdentityRefSpecification {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
+            .TYPE)
+            .add(Rfc6020Mapping.BASE, 1, 1)
+            .build();
 
     protected IdentityRefSpecificationImpl(
             StmtContext<String, TypeStatement.IdentityRefSpecification, ?> context) {
@@ -29,12 +39,11 @@ public class IdentityRefSpecificationImpl extends
             AbstractStatementSupport<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement.IdentityRefSpecification>> {
 
         public Definition() {
-            super(Rfc6020Mapping.BASE);
+            super(Rfc6020Mapping.TYPE);
         }
 
         @Override
-        public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value)
-                throws SourceException {
+        public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
             return value;
         }
 
@@ -46,8 +55,32 @@ public class IdentityRefSpecificationImpl extends
 
         @Override
         public EffectiveStatement<String, TypeStatement.IdentityRefSpecification> createEffective(
-                StmtContext<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement.IdentityRefSpecification>> ctx) {
-            throw new UnsupportedOperationException();
+                StmtContext<String, TypeStatement.IdentityRefSpecification, EffectiveStatement<String, TypeStatement
+                        .IdentityRefSpecification>> ctx) {
+            return new IdentityRefSpecificationEffectiveStatementImpl(ctx);
+        }
+
+        @Override
+        public void onFullDefinitionDeclared(StmtContext.Mutable<String, IdentityRefSpecification,
+                EffectiveStatement<String, IdentityRefSpecification>> stmt) {
+            final StmtContext<QName, ?, ?> baseStmt = StmtContextUtils.findFirstDeclaredSubstatement(stmt,
+                    BaseStatement.class);
+            Preconditions.checkArgument(baseStmt != null, "The \"base\" statement, which is a substatement to the " +
+                    "\"type\"\n statement, MUST be present if the type is \"identityref\" in source '%s'", stmt
+                    .getStatementSourceReference());
+            final QName baseIdentity = baseStmt.getStatementArgument();
+            final StmtContext<?, IdentityStatement, EffectiveStatement<QName, IdentityStatement>> stmtCtx = stmt
+                    .getFromNamespace(IdentityNamespace.class, baseIdentity);
+            Preconditions.checkArgument(stmtCtx != null, "Referenced base identity '%s' doesn't exist " +
+                        "in " + "given scope " + "(module, imported submodules), source: '%s'", baseIdentity
+                    .getLocalName(), stmt.getStatementSourceReference());
+        }
+
+        @Override
+        public void onStatementDefinitionDeclared(StmtContext.Mutable<String, IdentityRefSpecification,
+                EffectiveStatement<String, IdentityRefSpecification>> stmt) {
+            super.onStatementDefinitionDeclared(stmt);
+            SUBSTATEMENT_VALIDATOR.validate(stmt);
         }
     }
 
@@ -56,4 +89,10 @@ public class IdentityRefSpecificationImpl extends
         return argument();
     }
 
+    @Nonnull
+    @Override
+    public BaseStatement getBase() {
+        return firstDeclared(BaseStatement.class);
+    }
+
 }