From: Jozef Gloncak Date: Tue, 16 Dec 2014 15:49:40 +0000 (+0100) Subject: BUG 2468 - IdentityValuesDTO to string class cast exception. X-Git-Tag: release/lithium~728 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=508aa010e688575a1c8eeb840d00a5e3f39c25fa BUG 2468 - IdentityValuesDTO to string class cast exception. If leaf had type which is definined in other module (imported) via typedef and this type is leafref then it wasn't possible correctly resolve base type of referenced type. New method for this resolution was implemented and is now called. Also test for this case was added Merge precondition: yangtools: https://git.opendaylight.org/gerrit/13683 Change-Id: If00db3a6297b5a0ea657acb469f4f49f350f8c63 Signed-off-by: Jozef Gloncak --- diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index ded398a33d..e24500a76c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -86,6 +86,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; import org.opendaylight.yangtools.yang.model.util.EmptyType; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; @@ -110,7 +111,15 @@ public class RestconfImpl implements RestconfService { } } + private static class TypeDef { + public final TypeDefinition typedef; + public final QName qName; + TypeDef(final TypeDefinition typedef, final QName qName) { + this.typedef = typedef; + this.qName = qName; + } + } private final static RestconfImpl INSTANCE = new RestconfImpl(); @@ -1322,11 +1331,16 @@ public class RestconfImpl implements RestconfService { final DOMMountPoint mountPoint) { final Object value = simpleNode.getValue(); Object inputValue = value; - TypeDefinition typeDefinition = this.typeDefinition(schema); + TypeDef typeDef = this.typeDefinition(schema); + TypeDefinition typeDefinition = typeDef != null ? typeDef.typedef : null; // For leafrefs, extract the type it is pointing to if(typeDefinition instanceof LeafrefTypeDefinition) { - typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), schema); + if (schema.getQName().equals(typeDef.qName)) { + typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), schema); + } else { + typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), typeDef.qName); + } } if (typeDefinition instanceof IdentityrefTypeDefinition) { @@ -1498,29 +1512,25 @@ public class RestconfImpl implements RestconfService { } } - private TypeDefinition _typeDefinition(final LeafSchemaNode node) { - TypeDefinition baseType = node.getType(); + private TypeDef typeDefinition(final TypeDefinition type, final QName nodeQName) { + TypeDefinition baseType = type; + QName qName = nodeQName; while (baseType.getBaseType() != null) { + if (baseType instanceof ExtendedType) { + qName = baseType.getQName(); + } baseType = baseType.getBaseType(); } - return baseType; - } - - private TypeDefinition typeDefinition(final LeafListSchemaNode node) { - TypeDefinition baseType = node.getType(); - while (baseType.getBaseType() != null) { - baseType = baseType.getBaseType(); - } + return new TypeDef(baseType, qName); - return baseType; } - private TypeDefinition typeDefinition(final DataSchemaNode node) { + private TypeDef typeDefinition(final DataSchemaNode node) { if (node instanceof LeafListSchemaNode) { - return typeDefinition((LeafListSchemaNode) node); + return typeDefinition(((LeafListSchemaNode)node).getType(), node.getQName()); } else if (node instanceof LeafSchemaNode) { - return _typeDefinition((LeafSchemaNode) node); + return typeDefinition(((LeafSchemaNode)node).getType(), node.getQName()); } else if (node instanceof AnyXmlSchemaNode) { return null; } else { diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java index bdd74e8f96..c11c7dbbe7 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java @@ -24,7 +24,7 @@ public class JsonLeafrefToCnSnTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialize() { - dataLoad("/json-to-cnsn/leafref"); + dataLoad("/json-to-cnsn/leafref",2,"leafref-module","cont"); } /** diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/augment-leafref-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/augment-leafref-module new file mode 100644 index 0000000000..766cc8153e --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/augment-leafref-module @@ -0,0 +1,21 @@ +module augment-leafref-module { + namespace "augment:leafref:module"; + + prefix "auglfrfmo"; + revision 2014-12-16 { + } + + + typedef leafreftype { + type leafref { + path "/cont/lf3"; + + } + } + + container cont { + leaf lf3 { + type string; + } + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json index 235666eed4..a9d5d29b44 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json @@ -1,6 +1,7 @@ { "cont":{ "lf1":121, - "lf2":121 + "lf2":121, + "lf4":"pcc://39.39.39.39" } } \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module index 8ca9f09096..9b124a0fe6 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module @@ -2,6 +2,8 @@ module leafref-module { namespace "leafref:module"; prefix "lfrfmo"; + + import augment-leafref-module {prefix augleafref; revision-date 2014-12-16;} revision 2013-11-18 { } @@ -14,6 +16,9 @@ module leafref-module { path "/cont/lf1"; } } + leaf lf4 { + type augleafref:leafreftype; + } } } \ No newline at end of file