Teach YANG parser to retain DeclarationReference
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / notification / NotificationStatementRFC7950Support.java
index 6125160cf219382f7d2a60af3382f3f69b78a72d..f6a54d3bafaa5a0993c75a999f0904dffde794e2 100644 (file)
@@ -11,10 +11,11 @@ import com.google.common.annotations.Beta;
 import com.google.common.collect.ImmutableSet;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
@@ -25,8 +26,8 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
  */
 @Beta
 public final class NotificationStatementRFC7950Support extends AbstractNotificationStatementSupport {
-    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
-            .NOTIFICATION)
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
+        SubstatementValidator.builder(YangStmtMapping.NOTIFICATION)
             .addAny(YangStmtMapping.ANYDATA)
             .addAny(YangStmtMapping.ANYXML)
             .addAny(YangStmtMapping.CHOICE)
@@ -44,32 +45,23 @@ public final class NotificationStatementRFC7950Support extends AbstractNotificat
             .addAny(YangStmtMapping.USES)
             .build();
 
-    private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS = ImmutableSet.of(
-            YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
-    private static final NotificationStatementRFC7950Support INSTANCE = new NotificationStatementRFC7950Support();
+    private static final ImmutableSet<StatementDefinition> ILLEGAL_PARENTS =
+        ImmutableSet.of(YangStmtMapping.NOTIFICATION, YangStmtMapping.RPC, YangStmtMapping.ACTION);
 
-    private NotificationStatementRFC7950Support() {
-        // Hidden
-    }
-
-    public static NotificationStatementRFC7950Support getInstance() {
-        return INSTANCE;
+    public NotificationStatementRFC7950Support(final YangParserConfiguration config) {
+        super(config);
     }
 
     @Override
-    public EffectiveStatement<QName, NotificationStatement> createEffective(
-            final StmtContext<QName, NotificationStatement, EffectiveStatement<QName, NotificationStatement>> ctx) {
-        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(ctx, ILLEGAL_PARENTS),
-            ctx.getStatementSourceReference(),
-            "Notification %s is defined within an rpc, action, or another notification",
-            ctx.getStatementArgument());
-        SourceException.throwIf(!StmtContextUtils.hasAncestorOfTypeWithChildOfType(ctx, YangStmtMapping.LIST,
-            YangStmtMapping.KEY), ctx.getStatementSourceReference(),
-            "Notification %s is defined within a list that has no key statement", ctx.getStatementArgument());
-        SourceException.throwIf(StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.CASE),
-            ctx.getStatementSourceReference(), "Notification %s is defined within a case statement",
-            ctx.getStatementArgument());
-        return new NotificationEffectiveStatementImpl(ctx);
+    public void onStatementAdded(final Mutable<QName, NotificationStatement, NotificationEffectiveStatement> stmt) {
+        final QName argument = stmt.argument();
+        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), stmt,
+            "Notification %s is defined within an rpc, action, or another notification", argument);
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), stmt,
+            "Notification %s is defined within a case statement", argument);
+        StmtContextUtils.validateNoKeylessListAncestorOf(stmt, "Notification");
+
+        super.onStatementAdded(stmt);
     }
 
     @Override