Add AbstractIdentityRefSpecificationSupport defences 21/95221/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 18 Feb 2021 00:45:17 +0000 (01:45 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 18 Feb 2021 00:45:50 +0000 (01:45 +0100)
We are jugling statements here, make sure we verify our expectations.

Change-Id: I26568cc52ede5b3a40f688cf53f2689fba3b11ed
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractIdentityRefSpecificationSupport.java

index 123b6f16a91b7d0c27534df59134ee9b40f81955..48f07aa55d0665321f7db63ef0311d873325a43f 100644 (file)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.collect.ImmutableList;
 import java.util.Collection;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -17,7 +20,6 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.IdentityRefSpecification;
 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
 import org.opendaylight.yangtools.yang.model.ri.type.IdentityrefTypeBuilder;
@@ -78,9 +80,11 @@ abstract class AbstractIdentityRefSpecificationSupport
         for (final EffectiveStatement<?, ?> subStmt : substatements) {
             if (subStmt instanceof BaseEffectiveStatement) {
                 final QName identityQName = ((BaseEffectiveStatement) subStmt).argument();
-                final StmtContext<?, IdentityStatement, IdentityEffectiveStatement> identityCtx =
-                        stmt.getFromNamespace(IdentityNamespace.class, identityQName);
-                builder.addIdentity((IdentitySchemaNode) identityCtx.buildEffective());
+                final IdentityEffectiveStatement baseIdentity =
+                    verifyNotNull(stmt.getFromNamespace(IdentityNamespace.class, identityQName)).buildEffective();
+                verify(baseIdentity instanceof IdentitySchemaNode, "Statement %s is not an IdentitySchemaNode",
+                    baseIdentity);
+                builder.addIdentity((IdentitySchemaNode) baseIdentity);
             }
         }