From: Martin Vitez Date: Fri, 11 Oct 2013 10:54:11 +0000 (+0200) Subject: Added possibility to augment rpc nodes. Updated test. X-Git-Tag: third-party-0.1.2~7 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=72bec87841bfed9c0bd5108204690652df8bcef7;p=yangtools.git Added possibility to augment rpc nodes. Updated test. Change-Id: Id60d7638b74c595a429e0c36900d40dd3015833d Signed-off-by: Martin Vitez --- diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.xtend b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.xtend index da4cb1e57a..bb731426ca 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.xtend +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.xtend @@ -42,6 +42,7 @@ import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder.NotificationDefinitionImpl; import org.opendaylight.yangtools.yang.model.api.AugmentationTarget +import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder public final class ParserUtils { @@ -308,7 +309,11 @@ public final class ParserUtils { var SchemaNodeBuilder nodeFound = dataNodeContainerParent.getDataChildByName(currentName); // if not found, search in notifications if (nodeFound == null && currentParent instanceof ModuleBuilder) { - nodeFound = searchNotifications(currentParent as ModuleBuilder, currentName); + nodeFound = searchNotifications(currentParent as ModuleBuilder, currentName); + } + // if not found, search in rpcs + if (nodeFound == null && currentParent instanceof ModuleBuilder) { + nodeFound = searchRpcs(currentParent as ModuleBuilder, currentName); } // if not found, search in uses if (nodeFound == null) { @@ -324,6 +329,13 @@ public final class ParserUtils { } else if (currentParent instanceof ChoiceBuilder) { val choiceParent = currentParent as ChoiceBuilder; currentParent = choiceParent.getCaseNodeByName(currentName); + } else if (currentParent instanceof RpcDefinitionBuilder) { + val rpc = currentParent as RpcDefinitionBuilder; + if ("input".equals(currentName)) { + currentParent = rpc.input; + } else if ("output".equals(currentName)) { + currentParent = rpc.output; + } } else { throw new YangParseException(moduleName, line, "Error in augment parsing: failed to find node " + currentName); @@ -347,6 +359,15 @@ public final class ParserUtils { return null; } + private static def searchRpcs(ModuleBuilder parent, String name) { + for(rpc : parent.rpcs) { + if(rpc.getQName().localName.equals(name)) { + return rpc; + } + } + return null; + } + private static def searchUses(DataNodeContainerBuilder dataNodeContainerParent, String name) { var currentName = name; for (unb : dataNodeContainerParent.usesNodes) { diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java index b367705e4e..97454af9d2 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java @@ -30,6 +30,7 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.util.BaseTypes; @@ -80,7 +81,7 @@ public class AugmentTest { Set augmentChildren = augment.getChildNodes(); assertEquals(5, augmentChildren.size()); - for(DataSchemaNode dsn : augmentChildren) { + for (DataSchemaNode dsn : augmentChildren) { TestUtils.checkIsAugmenting(dsn, false); } @@ -88,7 +89,7 @@ public class AugmentTest { LeafSchemaNode interfaceId = (LeafSchemaNode) augment.getDataChildByName("interface-id"); LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type"); ContainerSchemaNode schemas = (ContainerSchemaNode) augment.getDataChildByName("schemas"); - ChoiceNode odl = (ChoiceNode)augment.getDataChildByName("odl"); + ChoiceNode odl = (ChoiceNode) augment.getDataChildByName("odl"); assertNotNull(ds0ChannelNumber); assertNotNull(interfaceId); @@ -131,7 +132,6 @@ public class AugmentTest { expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); assertEquals(expectedPath, odl.getPath()); - // testfile3 Module module3 = TestUtils.findModule(modules, "custom"); augmentations = module3.getAugmentations(); @@ -139,9 +139,9 @@ public class AugmentTest { AugmentationSchema augment1 = null; AugmentationSchema augment2 = null; for (AugmentationSchema as : augmentations) { - if("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { + if ("if:ifType='ds0'".equals(as.getWhenCondition().toString())) { augment1 = as; - } else if("if:ifType='ds2'".equals(as.getWhenCondition().toString())) { + } else if ("if:ifType='ds2'".equals(as.getWhenCondition().toString())) { augment2 = as; } } @@ -174,7 +174,7 @@ public class AugmentTest { LeafSchemaNode interfaceId = (LeafSchemaNode) augmentedContainer.getDataChildByName("interface-id"); LeafSchemaNode myType = (LeafSchemaNode) augmentedContainer.getDataChildByName("my-type"); ContainerSchemaNode schemas = (ContainerSchemaNode) augmentedContainer.getDataChildByName("schemas"); - ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl"); + ChoiceNode odl = (ChoiceNode) augmentedContainer.getDataChildByName("odl"); assertNotNull(ds0ChannelNumber); assertNotNull(interfaceId); @@ -226,7 +226,7 @@ public class AugmentTest { // testfile1.yang // augment "/data:interfaces/data:ifEntry/t3:augment-holder" - ChoiceNode odl = (ChoiceNode)augmentedContainer.getDataChildByName("odl"); + ChoiceNode odl = (ChoiceNode) augmentedContainer.getDataChildByName("odl"); assertNotNull(odl); Set cases = odl.getCases(); assertEquals(4, cases.size()); @@ -236,14 +236,14 @@ public class AugmentTest { ChoiceCaseNode node2 = null; ChoiceCaseNode node3 = null; - for(ChoiceCaseNode ccn : cases) { - if("id".equals(ccn.getQName().getLocalName())) { + for (ChoiceCaseNode ccn : cases) { + if ("id".equals(ccn.getQName().getLocalName())) { id = ccn; - } else if("node1".equals(ccn.getQName().getLocalName())) { + } else if ("node1".equals(ccn.getQName().getLocalName())) { node1 = ccn; - } else if("node2".equals(ccn.getQName().getLocalName())) { + } else if ("node2".equals(ccn.getQName().getLocalName())) { node2 = ccn; - } else if("node3".equals(ccn.getQName().getLocalName())) { + } else if ("node3".equals(ccn.getQName().getLocalName())) { node3 = ccn; } } @@ -297,7 +297,7 @@ public class AugmentTest { // case id child qnames[4] = new QName(types1NS, types1Rev, t1, "id"); qnames[5] = new QName(types1NS, types1Rev, t1, "id"); - LeafSchemaNode caseIdChild = (LeafSchemaNode)idChildren.iterator().next(); + LeafSchemaNode caseIdChild = (LeafSchemaNode) idChildren.iterator().next(); assertNotNull(caseIdChild); expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); assertEquals(expectedPath, caseIdChild.getPath()); @@ -305,10 +305,94 @@ public class AugmentTest { // case node3 child qnames[4] = new QName(types1NS, types1Rev, t1, "node3"); qnames[5] = new QName(types1NS, types1Rev, t1, "node3"); - ContainerSchemaNode caseNode3Child = (ContainerSchemaNode)node3Children.iterator().next(); + ContainerSchemaNode caseNode3Child = (ContainerSchemaNode) node3Children.iterator().next(); assertNotNull(caseNode3Child); expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); assertEquals(expectedPath, caseNode3Child.getPath()); } + @Test + public void testAugmentRpc() throws Exception { + modules = TestUtils.loadModules(getClass().getResource("/augment-test/rpc").getPath()); + final URI NS_BAR = URI.create("urn:opendaylight:bar"); + final URI NS_FOO = URI.create("urn:opendaylight:foo"); + final Date revision = simpleDateFormat.parse("2013-10-11"); + + Module bar = TestUtils.findModule(modules, "bar"); + Set rpcs = bar.getRpcs(); + assertEquals(2, rpcs.size()); + + RpcDefinition submit = null; + for (RpcDefinition rpc : rpcs) { + if ("submit".equals(rpc.getQName().getLocalName())) { + submit = rpc; + break; + } + } + assertNotNull(submit); + + QName submitQName = new QName(NS_BAR, revision, "b", "submit"); + assertEquals(submitQName, submit.getQName()); + ContainerSchemaNode input = submit.getInput(); + QName inputQName = new QName(NS_BAR, revision, "b", "input"); + assertEquals(inputQName, input.getQName()); + ChoiceNode arguments = (ChoiceNode) input.getDataChildByName("arguments"); + QName argumentsQName = new QName(NS_BAR, revision, "b", "arguments"); + assertEquals(argumentsQName, arguments.getQName()); + assertFalse(arguments.isAugmenting()); + Set cases = arguments.getCases(); + assertEquals(3, cases.size()); + + ChoiceCaseNode attach = null; + ChoiceCaseNode create = null; + ChoiceCaseNode destroy = null; + for (ChoiceCaseNode child : cases) { + if ("attach".equals(child.getQName().getLocalName())) { + attach = child; + } else if ("create".equals(child.getQName().getLocalName())) { + create = child; + } else if ("destroy".equals(child.getQName().getLocalName())) { + destroy = child; + } + } + assertNotNull(attach); + assertNotNull(create); + assertNotNull(destroy); + + assertTrue(attach.isAugmenting()); + assertTrue(create.isAugmenting()); + assertTrue(destroy.isAugmenting()); + + SchemaPath expectedPath = null; + QName[] qnames = new QName[4]; + qnames[0] = submitQName; + qnames[1] = inputQName; + qnames[2] = argumentsQName; + + // case attach + qnames[3] = new QName(NS_FOO, revision, "f", "attach"); + assertEquals(qnames[3], attach.getQName()); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, attach.getPath()); + Set attachChildren = attach.getChildNodes(); + assertEquals(1, attachChildren.size()); + + // case create + qnames[3] = new QName(NS_FOO, revision, "f", "create"); + assertEquals(qnames[3], create.getQName()); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, create.getPath()); + Set createChildren = create.getChildNodes(); + assertEquals(1, createChildren.size()); + + // case attach + qnames[3] = new QName(NS_FOO, revision, "f", "destroy"); + assertEquals(qnames[3], destroy.getQName()); + expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); + assertEquals(expectedPath, destroy.getPath()); + Set destroyChildren = destroy.getChildNodes(); + assertEquals(1, destroyChildren.size()); + + } + } diff --git a/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang new file mode 100644 index 0000000000..99357bbfe9 --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/bar.yang @@ -0,0 +1,46 @@ +module bar { + yang-version 1; + namespace "urn:opendaylight:bar"; + prefix "b"; + + revision "2013-10-11" { + } + + rpc cancel { + input { + leaf id { + type string; + } + } + output { + leaf failure { + type string; + } + } + } + + + rpc submit { + input { + leaf id { + type string; + } + leaf type { + type string; + } + choice arguments { + } + } + + output { + choice result { + case failure { + leaf failure { + type string; + } + } + } + } + } + +} diff --git a/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang new file mode 100644 index 0000000000..cecce24ced --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/augment-test/rpc/foo.yang @@ -0,0 +1,50 @@ +module foo { + yang-version 1; + namespace "urn:opendaylight:foo"; + prefix "f"; + + import bar { prefix b; revision-date 2013-10-11; } + + revision "2013-10-11" { + } + + augment "/b:submit/b:input/b:arguments" { + when "../type = attach-path-instruction"; + + case attach { + container attach { + leaf link-id { + type string; + } + leaf path-id { + type string; + } + } + } + } + + augment "/b:submit/b:input/b:arguments" { + when "../type = create-path-instruction"; + + case create { + container create { + leaf id { + type string; + } + } + } + } + + augment "/b:submit/b:input/b:arguments" { + when "../type = destroy-path-instruction"; + + case destroy { + container destroy { + leaf id { + type string; + } + } + } + } + +}