Bug 6679 - api explorer creates false examples
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / ModelGenerator.java
index 1a94b21141cdf65bf013a6f99ccec6fc8a8c1c82..24768e836864ff76261c9dff5a102153304cdd86 100644 (file)
@@ -20,7 +20,6 @@ import org.json.JSONException;
 import org.json.JSONObject;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.Post;
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
@@ -123,7 +122,8 @@ public class ModelGenerator {
     }
 
     private void processModules(final Module module, final JSONObject models) throws JSONException {
-        createConcreteModelForPost(models, module.getName() + BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX, createPropertiesForPost(module));
+        createConcreteModelForPost(models, module.getName() + BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX,
+                createPropertiesForPost(module, module.getName()));
     }
 
     private void processContainersAndLists(final Module module, final JSONObject models, final SchemaContext schemaContext)
@@ -138,7 +138,6 @@ public class ModelGenerator {
                 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, false, schemaContext);
             }
         }
-
     }
 
     /**
@@ -151,30 +150,58 @@ public class ModelGenerator {
      */
     private void processRPCs(final Module module, final JSONObject models, final SchemaContext schemaContext) throws JSONException,
             IOException {
-
         final Set<RpcDefinition> rpcs = module.getRpcs();
         final String moduleName = module.getName();
         for (final RpcDefinition rpc : rpcs) {
-
             final ContainerSchemaNode input = rpc.getInput();
             if (input != null) {
-                final JSONObject inputJSON = processDataNodeContainer(input, moduleName, models, schemaContext);
+                final JSONObject properties = processChildren(input.getChildNodes(), moduleName, models, true, schemaContext);
+
                 final String filename = "(" + rpc.getQName().getLocalName() + ")input";
-                inputJSON.put("id", filename);
-                // writeToFile(filename, inputJSON.toString(2), moduleName);
-                models.put(filename, inputJSON);
+                final JSONObject childSchema = getSchemaTemplate();
+                childSchema.put(TYPE_KEY, OBJECT_TYPE);
+                childSchema.put(PROPERTIES_KEY, properties);
+                childSchema.put("id", filename);
+                models.put(filename, childSchema);
+
+                processTopData(filename, models, input);
             }
 
             final ContainerSchemaNode output = rpc.getOutput();
             if (output != null) {
-                final JSONObject outputJSON = processDataNodeContainer(output, moduleName, models, schemaContext);
+                final JSONObject properties = processChildren(output.getChildNodes(), moduleName, models, true, schemaContext);
                 final String filename = "(" + rpc.getQName().getLocalName() + ")output";
-                outputJSON.put("id", filename);
-                models.put(filename, outputJSON);
+                final JSONObject childSchema = getSchemaTemplate();
+                childSchema.put(TYPE_KEY, OBJECT_TYPE);
+                childSchema.put(PROPERTIES_KEY, properties);
+                childSchema.put("id", filename);
+                models.put(filename, childSchema);
+
+                processTopData(filename, models, output);
             }
         }
     }
 
+    private static JSONObject processTopData(final String filename, final JSONObject models, final SchemaNode schemaNode) {
+        final JSONObject items = new JSONObject();
+
+        items.put(REF_KEY, filename);
+        final JSONObject dataNodeProperties = new JSONObject();
+        dataNodeProperties.put(TYPE_KEY, schemaNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
+        dataNodeProperties.put(ITEMS_KEY, items);
+
+        dataNodeProperties.putOpt(DESCRIPTION_KEY, schemaNode.getDescription());
+        final JSONObject properties = new JSONObject();
+        properties.put(schemaNode.getQName().getLocalName(), dataNodeProperties);
+        final JSONObject finalChildSchema = getSchemaTemplate();
+        finalChildSchema.put(TYPE_KEY, OBJECT_TYPE);
+        finalChildSchema.put(PROPERTIES_KEY, properties);
+        finalChildSchema.put(ID_KEY, filename + OperationBuilder.TOP);
+        models.put(filename + OperationBuilder.TOP, finalChildSchema);
+
+        return dataNodeProperties;
+    }
+
     /**
      * Processes the 'identity' statement in a yang model and maps it to a 'model' in the Swagger JSON spec.
      *
@@ -227,43 +254,29 @@ public class ModelGenerator {
         }
     }
 
-    /**
-     * Processes the container and list nodes and populates the moduleJSON.
-     */
-    private JSONObject processDataNodeContainer(final DataNodeContainer dataNode, final String moduleName, final JSONObject models,
-            final SchemaContext schemaContext) throws JSONException, IOException {
-        return processDataNodeContainer(dataNode, moduleName, models, true, schemaContext);
-    }
-
-    private JSONObject processDataNodeContainer(final DataNodeContainer dataNode, final String moduleName, final JSONObject models,
-            final boolean isConfig, final SchemaContext schemaContext) throws JSONException, IOException {
+    private JSONObject processDataNodeContainer(final DataNodeContainer dataNode, final String parentName, final JSONObject models,
+                                                final boolean isConfig, final SchemaContext schemaContext) throws JSONException, IOException {
         if (dataNode instanceof ListSchemaNode || dataNode instanceof ContainerSchemaNode) {
             Preconditions.checkArgument(dataNode instanceof SchemaNode, "Data node should be also schema node");
             final Iterable<DataSchemaNode> containerChildren = dataNode.getChildNodes();
-            final JSONObject properties = processChildren(containerChildren, ((SchemaNode) dataNode).getQName(), moduleName,
-                    models, isConfig, schemaContext);
-
-            final String nodeName = (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
-                    + ((SchemaNode) dataNode).getQName().getLocalName();
+            final String localName = ((SchemaNode) dataNode).getQName().getLocalName();
+            final JSONObject properties = processChildren(containerChildren, parentName + "/" + localName, models, isConfig, schemaContext);
+            final String nodeName = parentName + (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
+                    + localName;
 
             final JSONObject childSchema = getSchemaTemplate();
             childSchema.put(TYPE_KEY, OBJECT_TYPE);
             childSchema.put(PROPERTIES_KEY, properties);
+
             childSchema.put("id", nodeName);
             models.put(nodeName, childSchema);
 
             if (isConfig) {
-                createConcreteModelForPost(models, ((SchemaNode) dataNode).getQName().getLocalName(),
-                        createPropertiesForPost(dataNode));
+                createConcreteModelForPost(models, localName,
+                        createPropertiesForPost(dataNode, parentName + "/" + localName));
             }
 
-            final JSONObject items = new JSONObject();
-            items.put(REF_KEY, nodeName);
-            final JSONObject dataNodeProperties = new JSONObject();
-            dataNodeProperties.put(TYPE_KEY, dataNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
-            dataNodeProperties.put(ITEMS_KEY, items);
-
-            return dataNodeProperties;
+            return processTopData(nodeName, models, (SchemaNode) dataNode);
         }
         return null;
     }
@@ -278,12 +291,13 @@ public class ModelGenerator {
         models.put(nodePostName, postSchema);
     }
 
-    private JSONObject createPropertiesForPost(final DataNodeContainer dataNodeContainer) throws JSONException {
+    private JSONObject createPropertiesForPost(final DataNodeContainer dataNodeContainer, final String parentName)
+            throws JSONException {
         final JSONObject properties = new JSONObject();
         for (final DataSchemaNode childNode : dataNodeContainer.getChildNodes()) {
             if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) {
                 final JSONObject items = new JSONObject();
-                items.put(REF_KEY, "(config)" + childNode.getQName().getLocalName());
+                items.put(REF_KEY, parentName + "(config)" + childNode.getQName().getLocalName());
                 final JSONObject property = new JSONObject();
                 property.put(TYPE_KEY, childNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
                 property.put(ITEMS_KEY, items);
@@ -296,48 +310,45 @@ public class ModelGenerator {
         return properties;
     }
 
-    private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final QName parentQName, final String moduleName,
-            final JSONObject models, final SchemaContext schemaContext) throws JSONException, IOException {
-        return processChildren(nodes, parentQName, moduleName, models, true, schemaContext);
+    private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final String moduleName,
+                                       final JSONObject models, final SchemaContext schemaContext) throws JSONException, IOException {
+        return processChildren(nodes, moduleName, models, true, schemaContext);
     }
 
     /**
      * Processes the nodes.
      */
-    private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final QName parentQName,
-            final String moduleName, final JSONObject models, final boolean isConfig, final SchemaContext schemaContext)
+    private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final String parentName, final JSONObject models,
+                                       final boolean isConfig, final SchemaContext schemaContext)
             throws JSONException, IOException {
-
         final JSONObject properties = new JSONObject();
-
         for (final DataSchemaNode node : nodes) {
             if (node.isConfiguration() == isConfig) {
-
                 final String name = resolveNodesName(node, topLevelModule, schemaContext);
-                JSONObject property = null;
+                final JSONObject property;
                 if (node instanceof LeafSchemaNode) {
                     property = processLeafNode((LeafSchemaNode) node);
+
                 } else if (node instanceof ListSchemaNode) {
-                    property = processDataNodeContainer((ListSchemaNode) node, moduleName, models, isConfig,
+                    property = processDataNodeContainer((ListSchemaNode) node, parentName, models, isConfig,
                             schemaContext);
 
                 } else if (node instanceof LeafListSchemaNode) {
                     property = processLeafListNode((LeafListSchemaNode) node);
 
                 } else if (node instanceof ChoiceSchemaNode) {
-                    property = processChoiceNode((ChoiceSchemaNode) node, moduleName, models, schemaContext);
+                    property = processChoiceNode((ChoiceSchemaNode) node, parentName, models, schemaContext);
 
                 } else if (node instanceof AnyXmlSchemaNode) {
                     property = processAnyXMLNode((AnyXmlSchemaNode) node);
 
                 } else if (node instanceof ContainerSchemaNode) {
-                    property = processDataNodeContainer((ContainerSchemaNode) node, moduleName, models, isConfig,
+                    property = processDataNodeContainer((ContainerSchemaNode) node, parentName, models, isConfig,
                             schemaContext);
 
                 } else {
                     throw new IllegalArgumentException("Unknown DataSchemaNode type: " + node.getClass());
                 }
-
                 property.putOpt(DESCRIPTION_KEY, node.getDescription());
                 properties.put(name, property);
             }
@@ -359,16 +370,15 @@ public class ModelGenerator {
         return props;
     }
 
-    private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String moduleName, final JSONObject models,
-            final SchemaContext schemaContext) throws JSONException, IOException {
+    private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String parentName, final JSONObject models,
+                                         final SchemaContext schemaContext) throws JSONException, IOException {
 
         final Set<ChoiceCaseNode> cases = choiceNode.getCases();
 
         final JSONArray choiceProps = new JSONArray();
         for (final ChoiceCaseNode choiceCase : cases) {
             final String choiceName = choiceCase.getQName().getLocalName();
-            final JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), choiceCase.getQName(), moduleName,
-                    models, schemaContext);
+            final JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), parentName, models, schemaContext);
             final JSONObject choiceObj = new JSONObject();
             choiceObj.put(choiceName, choiceProp);
             choiceObj.put(TYPE_KEY, OBJECT_TYPE);