Merge "Address TODOs: use NetconfRpcFutureCallback for RPC callback"
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / ModelGenerator.java
index fdbd7b4997120c5572fca34b79f6a4befd201ce4..5813f1e4def9ccb5eb7cee0452cc3071ac3f962b 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;
@@ -113,7 +112,7 @@ public class ModelGenerator {
     }
 
     public JSONObject convertToJsonSchema(final Module module, final SchemaContext schemaContext) throws IOException, JSONException {
-        JSONObject models = new JSONObject();
+        final JSONObject models = new JSONObject();
         topLevelModule = module;
         processModules(module, models);
         processContainersAndLists(module, models, schemaContext);
@@ -129,16 +128,15 @@ public class ModelGenerator {
     private void processContainersAndLists(final Module module, final JSONObject models, final SchemaContext schemaContext)
             throws IOException, JSONException {
 
-        String moduleName = module.getName();
+        final String moduleName = module.getName();
 
-        for (DataSchemaNode childNode : module.getChildNodes()) {
+        for (final DataSchemaNode childNode : module.getChildNodes()) {
             // For every container and list in the module
             if (childNode instanceof ContainerSchemaNode || childNode instanceof ListSchemaNode) {
                 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, true, schemaContext);
                 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, false, schemaContext);
             }
         }
-
     }
 
     /**
@@ -151,30 +149,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 properties = processChildren(input.getChildNodes(), moduleName, models, true, schemaContext);
 
-        Set<RpcDefinition> rpcs = module.getRpcs();
-        String moduleName = module.getName();
-        for (RpcDefinition rpc : rpcs) {
+                final String filename = "(" + rpc.getQName().getLocalName() + ")input";
+                final JSONObject childSchema = getSchemaTemplate();
+                childSchema.put(TYPE_KEY, OBJECT_TYPE);
+                childSchema.put(PROPERTIES_KEY, properties);
+                childSchema.put("id", filename);
+                models.put(filename, childSchema);
 
-            ContainerSchemaNode input = rpc.getInput();
-            if (input != null) {
-                JSONObject inputJSON = processDataNodeContainer(input, moduleName, models, schemaContext);
-                String filename = "(" + rpc.getQName().getLocalName() + ")input";
-                inputJSON.put("id", filename);
-                // writeToFile(filename, inputJSON.toString(2), moduleName);
-                models.put(filename, inputJSON);
+                processTopData(filename, models, input);
             }
 
-            ContainerSchemaNode output = rpc.getOutput();
+            final ContainerSchemaNode output = rpc.getOutput();
             if (output != null) {
-                JSONObject outputJSON = processDataNodeContainer(output, moduleName, models, schemaContext);
-                String filename = "(" + rpc.getQName().getLocalName() + ")output";
-                outputJSON.put("id", filename);
-                models.put(filename, outputJSON);
+                final JSONObject properties = processChildren(output.getChildNodes(), moduleName, models, true, schemaContext);
+                final String filename = "(" + rpc.getQName().getLocalName() + ")output";
+                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.
      *
@@ -185,31 +211,31 @@ public class ModelGenerator {
      */
     private static void processIdentities(final Module module, final JSONObject models) throws JSONException {
 
-        String moduleName = module.getName();
-        Set<IdentitySchemaNode> idNodes = module.getIdentities();
+        final String moduleName = module.getName();
+        final Set<IdentitySchemaNode> idNodes = module.getIdentities();
         LOG.debug("Processing Identities for module {} . Found {} identity statements", moduleName, idNodes.size());
 
-        for (IdentitySchemaNode idNode : idNodes) {
-            JSONObject identityObj = new JSONObject();
-            String identityName = idNode.getQName().getLocalName();
+        for (final IdentitySchemaNode idNode : idNodes) {
+            final JSONObject identityObj = new JSONObject();
+            final String identityName = idNode.getQName().getLocalName();
             LOG.debug("Processing Identity: {}", identityName);
 
             identityObj.put(ID_KEY, identityName);
             identityObj.put(DESCRIPTION_KEY, idNode.getDescription());
 
-            JSONObject props = new JSONObject();
-            IdentitySchemaNode baseId = idNode.getBaseIdentity();
+            final JSONObject props = new JSONObject();
+            final IdentitySchemaNode baseId = idNode.getBaseIdentity();
 
             if (baseId == null) {
                 /**
                  * This is a base identity. So lets see if it has sub types. If it does, then add them to the model
                  * definition.
                  */
-                Set<IdentitySchemaNode> derivedIds = idNode.getDerivedIdentities();
+                final Set<IdentitySchemaNode> derivedIds = idNode.getDerivedIdentities();
 
                 if (derivedIds != null) {
-                    JSONArray subTypes = new JSONArray();
-                    for (IdentitySchemaNode derivedId : derivedIds) {
+                    final JSONArray subTypes = new JSONArray();
+                    for (final IdentitySchemaNode derivedId : derivedIds) {
                         subTypes.put(derivedId.getQName().getLocalName());
                     }
                     identityObj.put(SUB_TYPES_KEY, subTypes);
@@ -227,28 +253,20 @@ 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 {
+                                                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");
-            Iterable<DataSchemaNode> containerChildren = dataNode.getChildNodes();
-            JSONObject properties = processChildren(containerChildren, ((SchemaNode) dataNode).getQName(), moduleName,
-                    models, isConfig, schemaContext);
+            final Iterable<DataSchemaNode> containerChildren = dataNode.getChildNodes();
+            final JSONObject properties = processChildren(containerChildren, moduleName, models, isConfig, schemaContext);
 
-            String nodeName = (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
+            final String nodeName = (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
                     + ((SchemaNode) dataNode).getQName().getLocalName();
 
-            JSONObject childSchema = getSchemaTemplate();
+            final JSONObject childSchema = getSchemaTemplate();
             childSchema.put(TYPE_KEY, OBJECT_TYPE);
             childSchema.put(PROPERTIES_KEY, properties);
+
             childSchema.put("id", nodeName);
             models.put(nodeName, childSchema);
 
@@ -257,21 +275,15 @@ public class ModelGenerator {
                         createPropertiesForPost(dataNode));
             }
 
-            JSONObject items = new JSONObject();
-            items.put(REF_KEY, nodeName);
-            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;
     }
 
     private static void createConcreteModelForPost(final JSONObject models, final String localName,
             final JSONObject properties) throws JSONException {
-        String nodePostName = OperationBuilder.CONFIG + localName + Post.METHOD_NAME;
-        JSONObject postSchema = getSchemaTemplate();
+        final String nodePostName = OperationBuilder.CONFIG + localName + Post.METHOD_NAME;
+        final JSONObject postSchema = getSchemaTemplate();
         postSchema.put(TYPE_KEY, OBJECT_TYPE);
         postSchema.put("id", nodePostName);
         postSchema.put(PROPERTIES_KEY, properties);
@@ -279,44 +291,42 @@ public class ModelGenerator {
     }
 
     private JSONObject createPropertiesForPost(final DataNodeContainer dataNodeContainer) throws JSONException {
-        JSONObject properties = new JSONObject();
-        for (DataSchemaNode childNode : dataNodeContainer.getChildNodes()) {
+        final JSONObject properties = new JSONObject();
+        for (final DataSchemaNode childNode : dataNodeContainer.getChildNodes()) {
             if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) {
-                JSONObject items = new JSONObject();
+                final JSONObject items = new JSONObject();
                 items.put(REF_KEY, "(config)" + childNode.getQName().getLocalName());
-                JSONObject property = new JSONObject();
+                final JSONObject property = new JSONObject();
                 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);
+                final JSONObject property = processLeafNode((LeafSchemaNode) childNode);
                 properties.put(childNode.getQName().getLocalName(), property);
             }
         }
         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 moduleName, final JSONObject models,
+                                       final boolean isConfig, final SchemaContext schemaContext)
             throws JSONException, IOException {
-
-        JSONObject properties = new JSONObject();
-
-        for (DataSchemaNode node : nodes) {
+        final JSONObject properties = new JSONObject();
+        for (final DataSchemaNode node : nodes) {
             if (node.isConfiguration() == isConfig) {
-
-                String name = resolveNodesName(node, topLevelModule, schemaContext);
-                JSONObject property = null;
+                final String name = resolveNodesName(node, topLevelModule, schemaContext);
+                final JSONObject property;
                 if (node instanceof LeafSchemaNode) {
                     property = processLeafNode((LeafSchemaNode) node);
+
                 } else if (node instanceof ListSchemaNode) {
                     property = processDataNodeContainer((ListSchemaNode) node, moduleName, models, isConfig,
                             schemaContext);
@@ -337,7 +347,6 @@ public class ModelGenerator {
                 } else {
                     throw new IllegalArgumentException("Unknown DataSchemaNode type: " + node.getClass());
                 }
-
                 property.putOpt(DESCRIPTION_KEY, node.getDescription());
                 properties.put(name, property);
             }
@@ -346,14 +355,14 @@ public class ModelGenerator {
     }
 
     private JSONObject processLeafListNode(final LeafListSchemaNode listNode) throws JSONException {
-        JSONObject props = new JSONObject();
+        final JSONObject props = new JSONObject();
         props.put(TYPE_KEY, ARRAY_TYPE);
 
-        JSONObject itemsVal = new JSONObject();
+        final JSONObject itemsVal = new JSONObject();
         processTypeDef(listNode.getType(), itemsVal);
         props.put(ITEMS_KEY, itemsVal);
 
-        ConstraintDefinition constraints = listNode.getConstraints();
+        final ConstraintDefinition constraints = listNode.getConstraints();
         processConstraints(constraints, props);
 
         return props;
@@ -362,20 +371,19 @@ public class ModelGenerator {
     private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String moduleName, final JSONObject models,
             final SchemaContext schemaContext) throws JSONException, IOException {
 
-        Set<ChoiceCaseNode> cases = choiceNode.getCases();
+        final Set<ChoiceCaseNode> cases = choiceNode.getCases();
 
-        JSONArray choiceProps = new JSONArray();
-        for (ChoiceCaseNode choiceCase : cases) {
-            String choiceName = choiceCase.getQName().getLocalName();
-            JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), choiceCase.getQName(), moduleName,
-                    models, schemaContext);
-            JSONObject choiceObj = new JSONObject();
+        final JSONArray choiceProps = new JSONArray();
+        for (final ChoiceCaseNode choiceCase : cases) {
+            final String choiceName = choiceCase.getQName().getLocalName();
+            final JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), moduleName, models, schemaContext);
+            final JSONObject choiceObj = new JSONObject();
             choiceObj.put(choiceName, choiceProp);
             choiceObj.put(TYPE_KEY, OBJECT_TYPE);
             choiceProps.put(choiceObj);
         }
 
-        JSONObject oneOfProps = new JSONObject();
+        final JSONObject oneOfProps = new JSONObject();
         oneOfProps.put(ONE_OF_KEY, choiceProps);
         oneOfProps.put(TYPE_KEY, OBJECT_TYPE);
 
@@ -383,11 +391,11 @@ public class ModelGenerator {
     }
 
     private static void processConstraints(final ConstraintDefinition constraints, final JSONObject props) throws JSONException {
-        boolean isMandatory = constraints.isMandatory();
+        final boolean isMandatory = constraints.isMandatory();
         props.put(REQUIRED_KEY, isMandatory);
 
-        Integer minElements = constraints.getMinElements();
-        Integer maxElements = constraints.getMaxElements();
+        final Integer minElements = constraints.getMinElements();
+        final Integer maxElements = constraints.getMaxElements();
         if (minElements != null) {
             props.put(MIN_ITEMS, minElements);
         }
@@ -397,9 +405,9 @@ public class ModelGenerator {
     }
 
     private JSONObject processLeafNode(final LeafSchemaNode leafNode) throws JSONException {
-        JSONObject property = new JSONObject();
+        final JSONObject property = new JSONObject();
 
-        String leafDescription = leafNode.getDescription();
+        final String leafDescription = leafNode.getDescription();
         property.put(DESCRIPTION_KEY, leafDescription);
 
         processConstraints(leafNode.getConstraints(), property);
@@ -409,9 +417,9 @@ public class ModelGenerator {
     }
 
     private static JSONObject processAnyXMLNode(final AnyXmlSchemaNode leafNode) throws JSONException {
-        JSONObject property = new JSONObject();
+        final JSONObject property = new JSONObject();
 
-        String leafDescription = leafNode.getDescription();
+        final String leafDescription = leafNode.getDescription();
         property.put(DESCRIPTION_KEY, leafDescription);
 
         processConstraints(leafNode.getConstraints(), property);
@@ -444,15 +452,15 @@ public class ModelGenerator {
 
     private static void processBinaryType(final BinaryTypeDefinition binaryType, final JSONObject property) throws JSONException {
         property.put(TYPE_KEY, STRING);
-        JSONObject media = new JSONObject();
+        final JSONObject media = new JSONObject();
         media.put(BINARY_ENCODING_KEY, BASE_64);
         property.put(MEDIA_KEY, media);
     }
 
     private static void processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException {
-        List<EnumPair> enumPairs = enumLeafType.getValues();
-        List<String> enumNames = new ArrayList<>();
-        for (EnumPair enumPair : enumPairs) {
+        final List<EnumPair> enumPairs = enumLeafType.getValues();
+        final List<String> enumNames = new ArrayList<>();
+        for (final EnumPair enumPair : enumPairs) {
             enumNames.add(enumPair.getName());
         }
         property.putOpt(ENUM, new JSONArray(enumNames));
@@ -462,13 +470,13 @@ public class ModelGenerator {
         property.put(TYPE_KEY, ARRAY_TYPE);
         property.put(MIN_ITEMS, 0);
         property.put(UNIQUE_ITEMS_KEY, true);
-        JSONArray enumValues = new JSONArray();
+        final JSONArray enumValues = new JSONArray();
 
-        List<Bit> bits = bitsType.getBits();
-        for (Bit bit : bits) {
+        final List<Bit> bits = bitsType.getBits();
+        for (final Bit bit : bits) {
             enumValues.put(bit.getName());
         }
-        JSONObject itemsValue = new JSONObject();
+        final JSONObject itemsValue = new JSONObject();
         itemsValue.put(ENUM, enumValues);
         property.put(ITEMS_KEY, itemsValue);
     }
@@ -483,9 +491,9 @@ public class ModelGenerator {
 
         // FIXME: json-schema is not expressive enough to capture min/max laternatives. We should find the true minimum
         //        and true maximum implied by the constraints and use that.
-        for (LengthConstraint lengthConstraint : lengthConstraints) {
-            Number min = lengthConstraint.getMin();
-            Number max = lengthConstraint.getMax();
+        for (final LengthConstraint lengthConstraint : lengthConstraints) {
+            final Number min = lengthConstraint.getMin();
+            final Number max = lengthConstraint.getMax();
             property.putOpt(MIN_LENGTH_KEY, min);
             property.putOpt(MAX_LENGTH_KEY, max);
         }
@@ -494,8 +502,8 @@ public class ModelGenerator {
     }
 
     private static void processUnionType(final UnionTypeDefinition unionType, final JSONObject property) throws JSONException {
-        StringBuilder type = new StringBuilder();
-        for (TypeDefinition<?> typeDef : unionType.getTypes()) {
+        final StringBuilder type = new StringBuilder();
+        for (final TypeDefinition<?> typeDef : unionType.getTypes()) {
             if (type.length() > 0) {
                 type.append(" or ");
             }
@@ -509,7 +517,7 @@ public class ModelGenerator {
      * Helper method to generate a pre-filled JSON schema object.
      */
     private static JSONObject getSchemaTemplate() throws JSONException {
-        JSONObject schemaJSON = new JSONObject();
+        final JSONObject schemaJSON = new JSONObject();
         schemaJSON.put(SCHEMA_KEY, SCHEMA_URL);
 
         return schemaJSON;