From 6f958d7b86718863684de5103dabce6a50e55bf9 Mon Sep 17 00:00:00 2001 From: Jan Hajnar Date: Mon, 8 Jun 2015 17:28:12 +0200 Subject: [PATCH] Bug 3595 - RESTCONF: GET operation on a node in a list, identified by leaf-ref fails. * added referenced type resolution for leafrefs in ControllerContext Change-Id: Ic38b77038feb786a46a8bdd069448f006418d7bf Signed-off-by: Jan Hajnar (cherry picked from commit 5af9027a8ce234f3dcc44ee0d1791036b7f0ed62) --- .../sal/restconf/impl/ControllerContext.java | 16 ++++--- .../sal/restconf/impl/test/Bug3595Test.java | 45 +++++++++++++++++++ .../leafref/yang/leafref-module.yang | 24 +++++++--- .../leafref/yang/referenced-module.yang | 16 +++++-- 4 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java index ad2409bfcf..b33d79e1d0 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java @@ -63,6 +63,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; 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.SchemaContextUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -754,17 +756,19 @@ public class ControllerContext implements SchemaContextListener { private void addKeyValue(final HashMap map, final DataSchemaNode node, final String uriValue, final DOMMountPoint mountPoint) { - Preconditions. checkNotNull(uriValue); + Preconditions.checkNotNull(uriValue); Preconditions.checkArgument((node instanceof LeafSchemaNode)); final String urlDecoded = urlPathArgDecode(uriValue); - final TypeDefinition typedef = ((LeafSchemaNode) node).getType(); - final Codec codec = RestCodec.from(typedef, mountPoint); - - Object decoded = codec == null ? null : codec.deserialize(urlDecoded); + TypeDefinition typedef = ((LeafSchemaNode) node).getType(); + final TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(typedef); + if (baseType instanceof LeafrefTypeDefinition) { + typedef = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) baseType, globalSchema, node); + } + Codec codec = RestCodec.from(typedef, mountPoint); + Object decoded = codec.deserialize(urlDecoded); String additionalInfo = ""; if (decoded == null) { - final TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(typedef); if ((baseType instanceof IdentityrefTypeDefinition)) { decoded = toQName(urlDecoded); additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name."; diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java new file mode 100644 index 0000000000..cb15aad604 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java @@ -0,0 +1,45 @@ +package org.opendaylight.controller.sal.restconf.impl.test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.TestCase.assertNotNull; +import java.io.FileNotFoundException; +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; + +public class Bug3595Test { + + private static final QName CONT_QNAME = QName.create("leafref:module", "2014-04-17", "cont"); + private static final QName LST_WITH_LFREF_KEY_QNAME = QName.create(CONT_QNAME, "lst-with-lfref-key"); + private static final QName LFREF_KEY_QNAME = QName.create(CONT_QNAME, "lfref-key"); + + + private static ControllerContext controllerContext = ControllerContext.getInstance(); + + @BeforeClass + public static void initialize() throws FileNotFoundException { + SchemaContext schemaContext = TestUtils.loadSchemaContext("/leafref/yang"); + Module module = TestUtils.findModule(schemaContext.getModules(), "leafref-module"); + assertNotNull(module); + module = TestUtils.findModule(schemaContext.getModules(), "referenced-module"); + assertNotNull(module); + + controllerContext.setGlobalSchema(schemaContext); + } + + @Test + public void testLeafrefListKeyDeserializtion() { + final YangInstanceIdentifier node1IIexpected = YangInstanceIdentifier.of(CONT_QNAME) + .node(LST_WITH_LFREF_KEY_QNAME).node(new YangInstanceIdentifier.NodeIdentifierWithPredicates( + LST_WITH_LFREF_KEY_QNAME, LFREF_KEY_QNAME, "node1")); + final InstanceIdentifierContext iiContext = + controllerContext.toInstanceIdentifier("leafref-module:cont/lst-with-lfref-key/node1"); + iiContext.getInstanceIdentifier(); + assertEquals(node1IIexpected, iiContext.getInstanceIdentifier()); + } +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/leafref-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/leafref-module.yang index 00df290691..9675b79ac9 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/leafref-module.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/leafref-module.yang @@ -1,15 +1,15 @@ module leafref-module { - namespace "leafref:module"; + namespace "leafref:module"; prefix "lfrfmodule"; - import referenced-module { prefix refmod; revision-date 2014-04-17;} + import referenced-module { prefix refmod; revision-date 2014-04-17;} - - revision 2014-04-17 { + + revision 2014-04-17 { } - + container cont { leaf lf1 { @@ -39,6 +39,16 @@ module leafref-module { path "../lf1"; } } - + + list lst-with-lfref-key { + key "lfref-key"; + + leaf lfref-key { + type leafref { + path "/refmod:cont/refmod:id-ref"; + } + } + } + } -} \ No newline at end of file +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/referenced-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/referenced-module.yang index b6de719e4d..05dd123eeb 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/referenced-module.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/leafref/yang/referenced-module.yang @@ -1,13 +1,23 @@ module referenced-module { - namespace "referenced:module"; + namespace "referenced:module"; prefix "refmodule"; - revision 2014-04-17 { + revision 2014-04-17 { } container cont { leaf lf1 { type instance-identifier; } + + leaf id-ref { + type leafref { + path "../../id"; + } + } + } + + leaf id { + type string; } -} \ No newline at end of file +} -- 2.36.6