Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / BaseStatementImpl.java
index 5460fd238f66e6fb42674069392f8d24e0527d23..aa4085a5092edbfc8a8bbdc157dbad3fc9b9c965 100644 (file)
@@ -7,8 +7,22 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BaseEffectiveStatementImpl;
+import java.util.Collection;
+import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
+import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
+import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
+import java.util.LinkedList;
+import java.util.List;
+import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BaseEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
@@ -49,6 +63,41 @@ public class BaseStatementImpl extends AbstractDeclaredStatement<QName>
             return new BaseEffectiveStatementImpl(ctx);
         }
 
+        @Override
+        public void onStatementDefinitionDeclared(
+                final Mutable<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> baseStmtCtx)
+                throws SourceException {
+            final Mutable<?, ?, ?> baseParentCtx = baseStmtCtx.getParentContext();
+            if(StmtContextUtils.producesDeclared(baseParentCtx, IdentityStatement.class)) {
+
+                final QName baseIdentityQName = baseStmtCtx.getStatementArgument();
+                ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(ModelProcessingPhase.STATEMENT_DEFINITION);
+                final Prerequisite<StmtContext<?, ?, ?>> requiresPrereq = baseIdentityAction.requiresCtx(baseStmtCtx, IdentityNamespace.class, baseIdentityQName, ModelProcessingPhase.STATEMENT_DEFINITION);
+                final Prerequisite<?> mutatesPrereq = baseIdentityAction.mutatesCtx(baseParentCtx, ModelProcessingPhase.STATEMENT_DEFINITION);
+
+                baseIdentityAction.apply( new InferenceAction() {
+
+                    @Override
+                    public void apply() throws InferenceException {
+                        List<StmtContext<?, ?, ?>> derivedIdentities = baseStmtCtx.getFromNamespace(DerivedIdentitiesNamespace.class, baseStmtCtx.getStatementArgument());
+                        if(derivedIdentities == null) {
+                            derivedIdentities = new LinkedList<>();
+                            baseStmtCtx.addToNs(DerivedIdentitiesNamespace.class, baseIdentityQName, derivedIdentities);
+                        }
+                        derivedIdentities.add(baseParentCtx);
+                    }
+
+                    @Override
+                    public void prerequisiteFailed(
+                            Collection<? extends Prerequisite<?>> failed)
+                            throws InferenceException {
+                            throw new InferenceException("Unable to resolve identity "+baseParentCtx.getStatementArgument()+" and base identity " + baseStmtCtx.getStatementArgument(), baseStmtCtx
+                                    .getStatementSourceReference());
+                    }
+
+                });
+            }
+        }
     }
 
     @Override