From 35aa0e92fa29618e64b3dffa77d2b35f9b70ecab Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 22 Jan 2020 14:25:15 +0100 Subject: [PATCH] Do not tolerate duplicate identities We have a unit test asserting identity definition squashing -- which really is papering over the parser accepting the identities. As per RFC7950, identity declarations must be unique, and we should report them through normal SourceException. JIRA: YANGTOOLS-1075 Change-Id: I98ce8615bcdb502cb79caa23fc9c3a127f850ea9 Signed-off-by: Robert Varga --- .../AbstractIdentityStatementSupport.java | 7 ++++++- .../yangtools/yang/stmt/IdentityStmtTest.java | 20 ++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/identity/AbstractIdentityStatementSupport.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/identity/AbstractIdentityStatementSupport.java index b1037dbd21..a90edf3443 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/identity/AbstractIdentityStatementSupport.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/identity/AbstractIdentityStatementSupport.java @@ -16,6 +16,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSup import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; 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.source.SourceException; abstract class AbstractIdentityStatementSupport extends AbstractQNameStatementSupport { @@ -43,6 +44,10 @@ abstract class AbstractIdentityStatementSupport @Override public final void onStatementDefinitionDeclared( final Mutable stmt) { - stmt.addToNs(IdentityNamespace.class, stmt.coerceStatementArgument(), stmt); + final QName qname = stmt.coerceStatementArgument(); + final StmtContext prev = stmt.getFromNamespace(IdentityNamespace.class, qname); + SourceException.throwIf(prev != null, stmt.getStatementSourceReference(), "Duplicate identity definition %s", + qname); + stmt.addToNs(IdentityNamespace.class, qname, stmt); } } diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/IdentityStmtTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/IdentityStmtTest.java index a58ece2738..6f04c422c1 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/IdentityStmtTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/IdentityStmtTest.java @@ -5,16 +5,18 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.yangtools.yang.stmt; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource; +import com.google.common.base.Throwables; import java.util.Iterator; import java.util.Set; import org.junit.Test; @@ -24,6 +26,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; public class IdentityStmtTest { @@ -102,13 +105,12 @@ public class IdentityStmtTest { @Test public void duplicateIdentityTest() throws ReactorException { - SchemaContext result = RFC7950Reactors.defaultReactor().newBuild() - .addSource(DUPLICATE_IDENTITY_MODULE) - .buildEffective(); - assertNotNull(result); - - Module testModule = result.findModules("duplicate-identity-test").iterator().next(); - Set identities = testModule.getIdentities(); - assertEquals(1, identities.size()); + try { + RFC7950Reactors.defaultReactor().newBuild().addSource(DUPLICATE_IDENTITY_MODULE).buildEffective(); + fail("Duplicate identities should have been detected"); + } catch (SomeModifiersUnresolvedException e) { + final SourceException cause = Throwables.getCauseAs(e, SourceException.class); + assertThat(cause.getMessage(), startsWith("Duplicate identity definition ")); + } } } -- 2.36.6