From b307c4f621486b193bdb33ad4b9cca52cd47b812 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 30 Sep 2014 22:16:08 -0700 Subject: [PATCH] Fixes bug 2114 Adds handling of "leaf" node at the module level. Change-Id: I55efb47b57d2f33b136b0dae8c82effe04ad706b Signed-off-by: Abhishek Kumar --- .../sal/rest/doc/impl/ModelGenerator.java | 9 ++- .../sal/rest/doc/impl/ModelGeneratorTest.java | 45 +++++++++++++ .../src/test/resources/yang/opflex.yang | 50 +++++++++++++++ .../src/test/resources/yang/toaster.yang | 64 +++++++++++-------- 4 files changed, 138 insertions(+), 30 deletions(-) create mode 100644 opendaylight/md-sal/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGeneratorTest.java create mode 100644 opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/opflex.yang diff --git a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java b/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java index f4274870c9..3b503ebba3 100644 --- a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java +++ b/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGenerator.java @@ -146,8 +146,10 @@ public class ModelGenerator { for (DataSchemaNode childNode : module.getChildNodes()) { // For every container and list in the module - processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, true, schemaContext); - processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, false, schemaContext); + if (childNode instanceof ContainerSchemaNode || childNode instanceof ListSchemaNode) { + processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, true, schemaContext); + processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, false, schemaContext); + } } } @@ -306,6 +308,9 @@ public class ModelGenerator { property.put(TYPE_KEY, childNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE); property.put(ITEMS_KEY, items); properties.put(childNode.getQName().getLocalName(), property); + } else if (childNode instanceof LeafSchemaNode){ + JSONObject property = processLeafNode((LeafSchemaNode)childNode); + properties.put(childNode.getQName().getLocalName(), property); } } return properties; diff --git a/opendaylight/md-sal/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGeneratorTest.java b/opendaylight/md-sal/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGeneratorTest.java new file mode 100644 index 0000000000..5918a0e839 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/ModelGeneratorTest.java @@ -0,0 +1,45 @@ +package org.opendaylight.controller.sal.rest.doc.impl; + +import com.google.common.base.Preconditions; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; + +import java.io.File; +import java.util.HashSet; +import java.util.Map; + + +public class ModelGeneratorTest { + + private DocGenTestHelper helper; + private SchemaContext schemaContext; + + @Before + public void setUp() throws Exception { + helper = new DocGenTestHelper(); + helper.setUp(); + schemaContext = new YangParserImpl().resolveSchemaContext(new HashSet(helper.getModules().values())); + } + + @Test + public void testConvertToJsonSchema() throws Exception { + + Preconditions.checkArgument(helper.getModules() != null, "No modules found"); + + ModelGenerator generator = new ModelGenerator(); + + for (Map.Entry m : helper.getModules().entrySet()) { + if (m.getKey().getAbsolutePath().endsWith("opflex.yang")) { + + JSONObject jsonObject = generator.convertToJsonSchema(m.getValue(), schemaContext); + Assert.assertNotNull(jsonObject); + } + } + + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/opflex.yang b/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/opflex.yang new file mode 100644 index 0000000000..8e598ddaab --- /dev/null +++ b/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/opflex.yang @@ -0,0 +1,50 @@ +module opflex { + yang-version 1; + + namespace "urn:opendaylight:groupbasedpolicy:opflex"; + prefix "opflex"; + + + + + + description + "This module defines the group-based policy OpFlex renderer model."; + + revision "2014-05-28" { + description + "Initial revision."; + } + + typedef serialization { + description + "The serialization to use for OpFlex messages."; + + type enumeration { + enum json { + description + "JSON 1.0 serialization."; + } + enum xml { + description + "XML serialization."; + } + enum binary { + description + "OpFlex binary serialization."; + } + } + } + + // ****************** + // Configuration Data + // ****************** + leaf domain { + description + "The OpFlex administrative domain."; + + config true; + + type string; + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/toaster.yang b/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/toaster.yang index 20bbd78622..ffddc8c3da 100644 --- a/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/toaster.yang +++ b/opendaylight/md-sal/sal-rest-docgen/src/test/resources/yang/toaster.yang @@ -20,11 +20,19 @@ module toaster { "Toaster module in progress."; } + leaf domain { + description + "Toaster domain."; + + config true; + + type string; + } identity toast-type { description "Base for all bread types supported by the toaster. - New bread types not listed here nay be added in the + New bread types not listed here nay be added in the future."; } @@ -72,7 +80,7 @@ module toaster { "Indicates the toaster service is available"; description "Top-level container for all toaster database objects."; - + leaf testToasterBits { type bits { bit testbit1 { @@ -84,21 +92,21 @@ module toaster { } default "testbit2"; } - + leaf testUnion { type union { type int32; type string; } - - } - + + } + leaf-list allow-user { type string; description "A list of user name patterns to allow"; - + } - + choice how { default interval; case interval { @@ -123,14 +131,14 @@ module toaster { type string; } } - } - + } + leaf toasterManufacturer { type DisplayString; config false; mandatory true; description - "The name of the toaster's manufacturer. For instance, + "The name of the toaster's manufacturer. For instance, Microsoft Toaster."; } @@ -161,7 +169,7 @@ module toaster { config false; mandatory true; description - "This variable indicates the current state of + "This variable indicates the current state of the toaster."; } } @@ -169,11 +177,11 @@ module toaster { rpc make-toast { description "Make some toast. - The toastDone notification will be sent when + The toastDone notification will be sent when the toast is finished. An 'in-use' error will be returned if toast is already being made. - A 'resource-denied' error will be returned + A 'resource-denied' error will be returned if the toaster service is disabled."; input { leaf toasterDoneness { @@ -182,10 +190,10 @@ module toaster { } default '5'; description - "This variable controls how well-done is the + "This variable controls how well-done is the ensuing toast. It should be on a scale of 1 to 10. - Toast made at 10 generally is considered unfit - for human consumption; toast made at 1 is warmed + Toast made at 10 generally is considered unfit + for human consumption; toast made at 1 is warmed lightly."; } @@ -195,23 +203,23 @@ module toaster { } default 'wheat-bread'; description - "This variable informs the toaster of the type of - material that is being toasted. The toaster - uses this information, combined with - toasterDoneness, to compute for how - long the material must be toasted to achieve + "This variable informs the toaster of the type of + material that is being toasted. The toaster + uses this information, combined with + toasterDoneness, to compute for how + long the material must be toasted to achieve the required doneness."; } } - } + } rpc cancel-toast { description "Stop making toast, if any is being made. - A 'resource-denied' error will be returned + A 'resource-denied' error will be returned if the toaster service is disabled."; - } - + } + notification toastDone { description "Indicates that the toast in progress has completed."; @@ -236,5 +244,5 @@ module toaster { description "Indicates the final toast status"; } - } - } + } + } -- 2.36.6