}
}
- private void resolveIdentity(final ModuleBuilder module, final IdentitySchemaNodeBuilder identity) {
+ private void resolveIdentity(final ModuleBuilder module,
+ final IdentitySchemaNodeBuilder identity) {
final String baseIdentityName = identity.getBaseIdentityName();
if (baseIdentityName != null) {
IdentitySchemaNodeBuilder result = null;
final int line = identity.getLine();
String[] splittedBase = baseIdentityName.split(":");
if (splittedBase.length > 2) {
- throw new YangParseException(module.getName(), line, "Failed to parse identityref base: "
- + baseIdentityName);
+ throw new YangParseException(module.getName(), line,
+ "Failed to parse identityref base: "
+ + baseIdentityName);
}
String prefix = splittedBase[0];
String name = splittedBase[1];
- ModuleBuilder dependentModule = BuilderUtils.getModuleByPrefix(module, prefix);
- result = BuilderUtils.findIdentity(dependentModule.getAddedIdentities(), name);
+
+ if (prefix.equals(module.getPrefix())
+ && name.equals(identity.getQName().getLocalName())) {
+ throw new YangParseException(module.getName(),
+ identity.getLine(),
+ "Failed to parse base, identity name equals base identity name: "
+ + baseIdentityName);
+ }
+
+ ModuleBuilder dependentModule = BuilderUtils.getModuleByPrefix(
+ module, prefix);
+ result = BuilderUtils.findIdentity(
+ dependentModule.getAddedIdentities(), name);
} else {
- result = BuilderUtils.findIdentity(module.getAddedIdentities(), baseIdentityName);
+ if (baseIdentityName.equals(identity.getQName().getLocalName())) {
+ throw new YangParseException(module.getName(),
+ identity.getLine(),
+ "Failed to parse base, identity name equals base identity name: "
+ + baseIdentityName);
+ }
+ result = BuilderUtils.findIdentity(module.getAddedIdentities(),
+ baseIdentityName);
}
identity.setBaseIdentity(result);
}
--- /dev/null
+package org.opendaylight.yangtools.yang.parser.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.ModuleImport;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.parser.util.YangParseException;
+
+public class YangParserIdentityTest {
+
+ // base identity name equals identity name
+ @Test(expected = YangParseException.class)
+ public void testParsingIdentityTestModuleShouldThrowYangParseException()
+ throws IOException, URISyntaxException, YangSyntaxErrorException {
+ File yang = new File(getClass().getResource(
+ "/identity/identitytest.yang").toURI());
+ InputStream stream = new FileInputStream(yang);
+ TestUtils.loadModule(stream);
+ }
+
+ // same module prefixed base identity name equals identity name
+ @Test(expected = YangParseException.class)
+ public void testParsingPrefixIdentityTestModuleShouldThrowYangParseException()
+ throws URISyntaxException, IOException, YangSyntaxErrorException {
+ File yang = new File(getClass().getResource(
+ "/identity/prefixidentitytest.yang").toURI());
+ InputStream stream = new FileInputStream(yang);
+ TestUtils.loadModule(stream);
+ }
+
+ // imported module prefixed base identity name equals identity name, but
+ // prefix differs
+ @Test
+ public void testParsingImportPrefixIdentityTestModuleShouldThrowYangParseException()
+ throws URISyntaxException, IOException, YangSyntaxErrorException {
+ Set<Module> modules = TestUtils.loadModules(getClass().getResource(
+ "/identity/import").toURI());
+ Module module = TestUtils.findModule(modules,
+ "prefiximportidentitytest");
+ Set<ModuleImport> imports = module.getImports();
+ assertEquals(imports.size(), 1);
+ ModuleImport dummy = TestUtils.findImport(imports, "dummy");
+ assertNotEquals(dummy.getPrefix(), module.getPrefix());
+ }
+}
--- /dev/null
+module identitytest {
+ yang-version 1;
+ namespace "urn:test.identitytest";
+ prefix "idtest";
+
+ organization "opendaylight";
+ contact "http://www.opendaylight.org";
+
+ revision "2014-09-17" {
+ description "Initial import";
+ }
+
+ identity test {
+ base test;
+ }
+}
--- /dev/null
+module dummy {
+ yang-version 1;
+ namespace "urn:test.dummy";
+ prefix "dummy";
+
+ organization "opendaylight";
+ contact "http://www.opendaylight.org";
+
+ revision "2014-09-24" {
+ description "Initial import";
+ }
+
+ identity prefiximportidentitytest {
+ }
+}
--- /dev/null
+module prefiximportidentitytest {
+ yang-version 1;
+ namespace "urn:test.prefiximportidentitytest";
+ prefix "prefiximportidentitytest";
+
+ import dummy { prefix dummy; revision-date 2014-09-24; }
+
+ organization "opendaylight";
+ contact "http://www.opendaylight.org";
+
+ revision "2014-09-24" {
+ description "Initial import";
+ }
+
+ identity prefiximportidentitytest {
+ base dummy:prefiximportidentitytest;
+ }
+}
--- /dev/null
+module prefixidentitytest {
+ yang-version 1;
+ namespace "urn:test.prefixidentitytest";
+ prefix prefixtest;
+
+ organization "opendaylight";
+ contact "http://www.opendaylight.org";
+
+ revision "2014-09-24" {
+ description "Initial import";
+ }
+
+ identity prefixtest {
+ base prefixtest:prefixtest;
+ }
+}