Updating Action Rest Endpoints on Swagger UI 01/85501/2
authorajay_dp001 <ajay.deep.singh@est.tech>
Sat, 2 Nov 2019 02:25:45 +0000 (02:25 +0000)
committerRobert Varga <nite@hq.sk>
Mon, 4 Nov 2019 09:29:01 +0000 (09:29 +0000)
NETCONF-647

Signed-off-by: ajay_dp001 <ajay.deep.singh@est.tech>
Change-Id: I790d81a833bdcb611459b3418fbf66be2388e050

restconf/sal-rest-docgen-maven/src/main/java/org/opendaylight/netconf/sal/rest/doc/maven/StaticDocGenerator.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocServiceImpl.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/BaseYangSwaggerGenerator.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/ModelGenerator.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/mountpoints/MountPointSwagger.java
restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/ApiDocGeneratorTest.java
restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/MountPointSwaggerTest.java

index 86463ca402b2fdd417df22e4b4b668c25a2718b1..4fdf9cdef6fb6e9167fd19fe6bc78da9348c4181 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Set;
 import java.util.function.Function;
 import javax.ws.rs.core.UriInfo;
 import org.apache.maven.project.MavenProject;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
 import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangSwaggerGeneratorDraft02;
 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
@@ -84,7 +85,7 @@ public class StaticDocGenerator extends BaseYangSwaggerGeneratorDraft02
             mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
 
             // Write resource listing to JS file
-            ResourceList resourceList = super.getResourceListing(null, context, "");
+            ResourceList resourceList = super.getResourceListing(null, context, "", URIType.DRAFT02);
             String resourceListJson = mapper.writeValueAsString(resourceList);
             resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
             bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
@@ -95,7 +96,8 @@ public class StaticDocGenerator extends BaseYangSwaggerGeneratorDraft02
                 int revisionIndex = resource.getPath().indexOf('(');
                 String name = resource.getPath().substring(0, revisionIndex);
                 String revision = resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
-                ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "");
+                ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "",
+                    URIType.DRAFT02);
                 String json = mapper.writeValueAsString(apiDeclaration);
                 // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
                 json = json.replace(
index bce08552d8678ddd4921b8297fe381fb2bb9fb6b..bfac921a818d633fcf80b88448614e17df810845 100644 (file)
@@ -40,6 +40,8 @@ public class ApiDocServiceImpl implements ApiDocService {
     private static final String TOTAL_PAGES = "totalPages";
     private static final String PAGE_NUM = "pageNum";
 
+    public enum URIType { RFC8040, DRAFT02 }
+
     private final MountPointSwagger mountPointSwaggerDraft02;
     private final MountPointSwagger mountPointSwaggerRFC8040;
     private final ApiDocGeneratorDraftO2 apiDocGeneratorDraft02;
@@ -64,10 +66,10 @@ public class ApiDocServiceImpl implements ApiDocService {
     @Override
     public synchronized Response getRootDoc(final UriInfo uriInfo) {
         final ResourceList rootDoc;
-        if (isNew(uriInfo)) {
-            rootDoc = apiDocGeneratorRFC8040.getResourceListing(uriInfo);
+        if (isNew(uriInfo).equals(URIType.RFC8040)) {
+            rootDoc = apiDocGeneratorRFC8040.getResourceListing(uriInfo, URIType.RFC8040);
         } else {
-            rootDoc = apiDocGeneratorDraft02.getResourceListing(uriInfo);
+            rootDoc = apiDocGeneratorDraft02.getResourceListing(uriInfo, URIType.DRAFT02);
         }
 
         return Response.ok(rootDoc).build();
@@ -79,10 +81,10 @@ public class ApiDocServiceImpl implements ApiDocService {
     @Override
     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
         final ApiDeclaration doc;
-        if (isNew(uriInfo)) {
-            doc = apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo);
+        if (isNew(uriInfo).equals(URIType.RFC8040)) {
+            doc = apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo, URIType.RFC8040);
         } else {
-            doc = apiDocGeneratorDraft02.getApiDeclaration(module, revision, uriInfo);
+            doc = apiDocGeneratorDraft02.getApiDeclaration(module, revision, uriInfo, URIType.DRAFT02);
         }
 
         return Response.ok(doc).build();
@@ -99,7 +101,7 @@ public class ApiDocServiceImpl implements ApiDocService {
     @Override
     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
         final MountPointSwagger mountPointSwagger;
-        if (isNew(uriInfo)) {
+        if (isNew(uriInfo).equals(URIType.RFC8040)) {
             mountPointSwagger = mountPointSwaggerRFC8040;
         } else {
             mountPointSwagger = mountPointSwaggerDraft02;
@@ -115,10 +117,12 @@ public class ApiDocServiceImpl implements ApiDocService {
         final ResourceList resourceList;
 
         if (uriInfo.getQueryParameters().getFirst(TOTAL_PAGES) != null) {
-            if (isNew(uriInfo)) {
-                resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum));
+            if (isNew(uriInfo).equals(URIType.RFC8040)) {
+                resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum),
+                    URIType.RFC8040);
             } else {
-                resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum));
+                resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum),
+                    URIType.DRAFT02);
             }
             int size = resourceList.getApis().size();
             return Response.ok(size % DEFAULT_PAGESIZE == 0 ? size / DEFAULT_PAGESIZE
@@ -127,12 +131,12 @@ public class ApiDocServiceImpl implements ApiDocService {
 
         final int pageNum = Integer.parseInt(uriInfo.getQueryParameters().getFirst(PAGE_NUM));
 
-        if (isNew(uriInfo)) {
+        if (isNew(uriInfo).equals(URIType.RFC8040)) {
             resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
-                    false);
+                false, URIType.RFC8040);
         } else {
             resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
-                    false);
+                false, URIType.DRAFT02);
         }
         return Response.ok(resourceList).build();
     }
@@ -141,15 +145,20 @@ public class ApiDocServiceImpl implements ApiDocService {
     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
             final String revision, final UriInfo uriInfo) {
         final ApiDeclaration api;
-        if (isNew(uriInfo)) {
-            api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
+        if (isNew(uriInfo).equals(URIType.RFC8040)) {
+            api = mountPointSwaggerRFC8040
+                .getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision, URIType.RFC8040);
         } else {
-            api = mountPointSwaggerDraft02.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
+            api = mountPointSwaggerDraft02
+                .getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision, URIType.DRAFT02);
         }
         return Response.ok(api).build();
     }
 
-    private static boolean isNew(final UriInfo uriInfo) {
-        return uriInfo.getBaseUri().toString().contains("/18/");
+    private static URIType isNew(final UriInfo uriInfo) {
+        if (uriInfo.getBaseUri().toString().contains("/18/")) {
+            return URIType.RFC8040;
+        }
+        return URIType.DRAFT02;
     }
 }
index e1f6b50cfe253f8881646e061fc4c8d127de6888..9ee11ccd7b1469650d4436950d15c03f0fe61f43 100644 (file)
@@ -29,6 +29,7 @@ import java.util.SortedSet;
 import java.util.TreeSet;
 import javax.ws.rs.core.UriInfo;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.Delete;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.Get;
@@ -45,11 +46,13 @@ import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
@@ -78,22 +81,22 @@ public abstract class BaseYangSwaggerGenerator {
         return schemaService;
     }
 
-    public ResourceList getResourceListing(final UriInfo uriInfo) {
+    public ResourceList getResourceListing(final UriInfo uriInfo, final URIType uriType) {
         final SchemaContext schemaContext = schemaService.getGlobalContext();
         Preconditions.checkState(schemaContext != null);
-        return getResourceListing(uriInfo, schemaContext, "", 0, true);
+        return getResourceListing(uriInfo, schemaContext, "", 0, true, uriType);
     }
 
     public ResourceList getResourceListing(final UriInfo uriInfo, final SchemaContext schemaContext,
-                                           final String context) {
-        return getResourceListing(uriInfo, schemaContext, context, 0, true);
+        final String context, final URIType uriType) {
+        return getResourceListing(uriInfo, schemaContext, context, 0, true, uriType);
     }
 
     /**
      * Return list of modules converted to swagger compliant resource list.
      */
     public ResourceList getResourceListing(final UriInfo uriInfo, final SchemaContext schemaContext,
-            final String context, final int pageNum, boolean all) {
+        final String context, final int pageNum, boolean all, final URIType uriType) {
 
         final ResourceList resourceList = createResourceList();
 
@@ -110,7 +113,7 @@ public abstract class BaseYangSwaggerGenerator {
 
             LOG.debug("Working on [{},{}]...", module.getName(), revisionString);
             final ApiDeclaration doc =
-                    getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context);
+                getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context, uriType);
             if (doc != null) {
                 count++;
                 if (count >= start && count < end || all) {
@@ -144,14 +147,15 @@ public abstract class BaseYangSwaggerGenerator {
         return uri.toASCIIString();
     }
 
-    public ApiDeclaration getApiDeclaration(final String module, final String revision, final UriInfo uriInfo) {
+    public ApiDeclaration getApiDeclaration(final String module, final String revision, final UriInfo uriInfo,
+        final URIType uriType) {
         final SchemaContext schemaContext = schemaService.getGlobalContext();
         Preconditions.checkState(schemaContext != null);
-        return getApiDeclaration(module, revision, uriInfo, schemaContext, "");
+        return getApiDeclaration(module, revision, uriInfo, schemaContext, "", uriType);
     }
 
     public ApiDeclaration getApiDeclaration(final String moduleName, final String revision, final UriInfo uriInfo,
-            final SchemaContext schemaContext, final String context) {
+        final SchemaContext schemaContext, final String context, final URIType uriType) {
         final Optional<Revision> rev;
 
         try {
@@ -162,16 +166,16 @@ public abstract class BaseYangSwaggerGenerator {
 
         final Module module = schemaContext.findModule(moduleName, rev).orElse(null);
         Preconditions.checkArgument(module != null,
-                "Could not find module by name,revision: " + moduleName + "," + revision);
+            "Could not find module by name,revision: " + moduleName + "," + revision);
 
-        return getApiDeclaration(module, uriInfo, context, schemaContext);
+        return getApiDeclaration(module, uriInfo, context, schemaContext, uriType);
     }
 
     public ApiDeclaration getApiDeclaration(final Module module, final UriInfo uriInfo,
-            final String context, final SchemaContext schemaContext) {
+        final String context, final SchemaContext schemaContext, final URIType uriType) {
         final String basePath = createBasePathFromUriInfo(uriInfo);
 
-        final ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext);
+        final ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext, uriType);
         if (doc != null) {
             return doc;
         }
@@ -185,13 +189,13 @@ public abstract class BaseYangSwaggerGenerator {
             portPart = ":" + port;
         }
         final String basePath =
-                new StringBuilder(uriInfo.getBaseUri().getScheme()).append("://").append(uriInfo.getBaseUri().getHost())
-                        .append(portPart).toString();
+            new StringBuilder(uriInfo.getBaseUri().getScheme()).append("://").append(uriInfo.getBaseUri().getHost())
+                .append(portPart).toString();
         return basePath;
     }
 
     public ApiDeclaration getSwaggerDocSpec(final Module module, final String basePath, final String context,
-                                            final SchemaContext schemaContext) {
+        final SchemaContext schemaContext, final URIType uriType) {
         final ApiDeclaration doc = createApiDeclaration(basePath);
 
         final List<Api> apis = new ArrayList<>();
@@ -226,12 +230,14 @@ public abstract class BaseYangSwaggerGenerator {
                         hasAddRootPostLink = true;
                     }
 
-                    addApis(node, apis, resourcePath, pathParams, schemaContext, true, module.getName(), "config");
+                    addApis(node, apis, resourcePath, pathParams, schemaContext, true, module.getName(), "config",
+                        uriType);
                 }
                 pathParams = new ArrayList<>();
                 resourcePath = getDataStorePath("operational", context);
 
-                addApis(node, apis, resourcePath, pathParams, schemaContext, false, module.getName(), "operational");
+                addApis(node, apis, resourcePath, pathParams, schemaContext, false, module.getName(), "operational",
+                    uriType);
             }
         }
 
@@ -239,8 +245,7 @@ public abstract class BaseYangSwaggerGenerator {
         for (final RpcDefinition rpcDefinition : rpcs) {
             final String resourcePath;
             resourcePath = getDataStorePath("operations", context);
-
-            addRpcs(rpcDefinition, apis, resourcePath, schemaContext);
+            addOperations(rpcDefinition, apis, resourcePath, schemaContext);
         }
 
         LOG.debug("Number of APIs found [{}]", apis.size());
@@ -265,12 +270,12 @@ public abstract class BaseYangSwaggerGenerator {
     }
 
     private void addRootPostLink(final Module module, final DataNodeContainer node,
-            final List<Parameter> pathParams, final String resourcePath, final String dataStore, final List<Api> apis) {
+        final List<Parameter> pathParams, final String resourcePath, final String dataStore, final List<Api> apis) {
         if (containsListOrContainer(module.getChildNodes())) {
             final Api apiForRootPostUri = new Api();
             apiForRootPostUri.setPath(resourcePath.concat(getContent(dataStore)));
             apiForRootPostUri.setOperations(operationPost(module.getName() + MODULE_NAME_SUFFIX,
-                    module.getDescription().orElse(null), module, pathParams, true, ""));
+                module.getDescription().orElse(null), module, pathParams, true, ""));
             apis.add(apiForRootPostUri);
         }
     }
@@ -291,8 +296,8 @@ public abstract class BaseYangSwaggerGenerator {
     }
 
     private void addApis(final DataSchemaNode node, final List<Api> apis, final String parentPath,
-            final List<Parameter> parentPathParams, final SchemaContext schemaContext, final boolean addConfigApi,
-            final String parentName, final String dataStore) {
+        final List<Parameter> parentPathParams, final SchemaContext schemaContext, final boolean addConfigApi,
+        final String parentName, final String dataStore, final URIType uriType) {
         final Api api = new Api();
         final List<Parameter> pathParams = new ArrayList<>(parentPathParams);
 
@@ -308,13 +313,19 @@ public abstract class BaseYangSwaggerGenerator {
         api.setOperations(operation(node, pathParams, addConfigApi, childSchemaNodes, parentName));
         apis.add(api);
 
+        if (uriType.equals(URIType.RFC8040)) {
+            ((ActionNodeContainer) node).getActions().forEach((actionDef -> {
+                addOperations(actionDef, apis, resourcePath, schemaContext);
+            }));
+        }
+
         for (final DataSchemaNode childNode : childSchemaNodes) {
             if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) {
                 // keep config and operation attributes separate.
                 if (childNode.isConfiguration() == addConfigApi) {
                     final String newParent = parentName + "/" + node.getQName().getLocalName();
                     addApis(childNode, apis, resourcePath, pathParams, schemaContext, addConfigApi, newParent,
-                            dataStore);
+                        dataStore, uriType);
                 }
             }
         }
@@ -396,30 +407,28 @@ public abstract class BaseYangSwaggerGenerator {
         return path.toString();
     }
 
-    protected void addRpcs(final RpcDefinition rpcDefn, final List<Api> apis, final String parentPath,
-            final SchemaContext schemaContext) {
-        final Api rpc = new Api();
-        final String resourcePath = parentPath + "/" + resolvePathArgumentsName(rpcDefn, schemaContext);
-        rpc.setPath(resourcePath);
+    protected void addOperations(final OperationDefinition operDef, final List<Api> apis, final String parentPath,
+        final SchemaContext schemaContext) {
+        final Api operationApi = new Api();
+        final String resourcePath = parentPath + "/" + resolvePathArgumentsName(operDef, schemaContext);
+        operationApi.setPath(resourcePath);
 
         final Operation operationSpec = new Operation();
         operationSpec.setMethod("POST");
-        operationSpec.setNotes(rpcDefn.getDescription().orElse(null));
-        operationSpec.setNickname(rpcDefn.getQName().getLocalName());
-        if (!rpcDefn.getOutput().getChildNodes().isEmpty()) {
-            operationSpec.setType("(" + rpcDefn.getQName().getLocalName() + ")output" + OperationBuilder.TOP);
+        operationSpec.setNotes(operDef.getDescription().orElse(null));
+        operationSpec.setNickname(operDef.getQName().getLocalName());
+        if (!operDef.getOutput().getChildNodes().isEmpty()) {
+            operationSpec.setType("(" + operDef.getQName().getLocalName() + ")output" + OperationBuilder.TOP);
         }
-        if (!rpcDefn.getInput().getChildNodes().isEmpty()) {
+        if (!operDef.getInput().getChildNodes().isEmpty()) {
             final Parameter payload = new Parameter();
             payload.setParamType("body");
-            payload.setType("(" + rpcDefn.getQName().getLocalName() + ")input" + OperationBuilder.TOP);
+            payload.setType("(" + operDef.getQName().getLocalName() + ")input" + OperationBuilder.TOP);
             operationSpec.setParameters(Collections.singletonList(payload));
             operationSpec.setConsumes(OperationBuilder.CONSUMES_PUT_POST);
         }
-
-        rpc.setOperations(Arrays.asList(operationSpec));
-
-        apis.add(rpc);
+        operationApi.setOperations(Arrays.asList(operationSpec));
+        apis.add(operationApi);
     }
 
     protected SortedSet<Module> getSortedModules(final SchemaContext schemaContext) {
index ddb9e14807cba5da0c82dfd4b8914279c7790aef..de7cea3a1abba702454ffd2ff9f4d678406f647e 100644 (file)
@@ -25,6 +25,8 @@ import java.util.regex.Pattern;
 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.ActionDefinition;
+import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
@@ -38,6 +40,7 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.MandatoryAware;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
 import org.opendaylight.yangtools.yang.model.api.PathExpression;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -136,51 +139,82 @@ public class ModelGenerator {
             if (childNode instanceof ContainerSchemaNode || childNode instanceof ListSchemaNode) {
                 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, true, schemaContext);
                 processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, false, schemaContext);
+                processActionNodeContainer(childNode, moduleName, models, schemaContext);
             }
         }
     }
 
     /**
-     * Process the RPCs for a Module Spits out a file each of the name
-     * {@code <rpcName>-input.json and <rpcName>-output.json}
-     * for each RPC that contains input & output elements.
+     * Process the Actions Swagger UI.
      *
-     * @param module module
+     * @param DataSchemaNode childNode
+     * @param String         moduleName
+     * @param ObjectNode     models
+     * @param SchemaContext  schemaContext
      * @throws IOException if I/O operation fails
      */
-    private void processRPCs(final Module module, final ObjectNode models,
-                             final SchemaContext schemaContext) throws IOException {
-        final Set<RpcDefinition> rpcs = module.getRpcs();
+    private void processActionNodeContainer(final DataSchemaNode childNode, final String moduleName,
+        final ObjectNode models, final SchemaContext schemaContext) throws IOException {
+        for (ActionDefinition actionDef : ((ActionNodeContainer) childNode).getActions()) {
+            processOperations(actionDef, moduleName, models, schemaContext);
+        }
+    }
+
+    /**
+     * Process the RPCs for Swagger UI.
+     *
+     * @param Module        module
+     * @param ObjectNode    models
+     * @param SchemaContext schemaContext
+     * @throws IOException if I/O operation fails
+     */
+    private void processRPCs(final Module module, final ObjectNode models, final SchemaContext schemaContext)
+        throws IOException {
         final String moduleName = module.getName();
-        for (final RpcDefinition rpc : rpcs) {
-            final ContainerSchemaNode input = rpc.getInput();
-            if (!input.getChildNodes().isEmpty()) {
-                final ObjectNode properties =
-                        processChildren(input.getChildNodes(), moduleName, models, true, schemaContext);
-
-                final String filename = "(" + rpc.getQName().getLocalName() + ")input";
-                final ObjectNode childSchema = getSchemaTemplate();
-                childSchema.put(TYPE_KEY, OBJECT_TYPE);
-                childSchema.set(PROPERTIES_KEY, properties);
-                childSchema.put(ID_KEY, filename);
-                models.set(filename, childSchema);
-
-                processTopData(filename, models, input);
-            }
+        for (final RpcDefinition rpcDefinition : module.getRpcs()) {
+            processOperations(rpcDefinition, moduleName, models, schemaContext);
+        }
+    }
 
-            final ContainerSchemaNode output = rpc.getOutput();
-            if (!output.getChildNodes().isEmpty()) {
-                final ObjectNode properties =
-                        processChildren(output.getChildNodes(), moduleName, models, true, schemaContext);
-                final String filename = "(" + rpc.getQName().getLocalName() + ")output";
-                final ObjectNode childSchema = getSchemaTemplate();
-                childSchema.put(TYPE_KEY, OBJECT_TYPE);
-                childSchema.set(PROPERTIES_KEY, properties);
-                childSchema.put(ID_KEY, filename);
-                models.set(filename, childSchema);
-
-                processTopData(filename, models, output);
-            }
+    /**
+     * Process the Operations for a Module Spits out a file each of the name {@code <operationName>-input.json and
+     * <oprationName>-output.json} for each Operation that contains input & output elements.
+     *
+     * @param OperationDefinition operationDef
+     * @param String              moduleName
+     * @param ObjectNode          models
+     * @param SchemaContext       schemaContext
+     * @throws IOException if I/O operation fails
+     */
+    private void processOperations(final OperationDefinition operationDef, final String moduleName,
+        final ObjectNode models, final SchemaContext schemaContext) throws IOException {
+        final ContainerSchemaNode input = operationDef.getInput();
+        if (!input.getChildNodes().isEmpty()) {
+            final ObjectNode properties =
+                processChildren(input.getChildNodes(), moduleName, models, true, schemaContext);
+
+            final String filename = "(" + operationDef.getQName().getLocalName() + ")input";
+            final ObjectNode childSchema = getSchemaTemplate();
+            childSchema.put(TYPE_KEY, OBJECT_TYPE);
+            childSchema.set(PROPERTIES_KEY, properties);
+            childSchema.put(ID_KEY, filename);
+            models.set(filename, childSchema);
+
+            processTopData(filename, models, input);
+        }
+
+        final ContainerSchemaNode output = operationDef.getOutput();
+        if (!output.getChildNodes().isEmpty()) {
+            final ObjectNode properties =
+                processChildren(output.getChildNodes(), moduleName, models, true, schemaContext);
+            final String filename = "(" + operationDef.getQName().getLocalName() + ")output";
+            final ObjectNode childSchema = getSchemaTemplate();
+            childSchema.put(TYPE_KEY, OBJECT_TYPE);
+            childSchema.set(PROPERTIES_KEY, properties);
+            childSchema.put(ID_KEY, filename);
+            models.set(filename, childSchema);
+
+            processTopData(filename, models, output);
         }
     }
 
index fe037cf47f323e6f752a7cc646b5eb0224099dae..358615962ab849279ee368c16ac56a95084383d7 100644 (file)
@@ -24,6 +24,7 @@ import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
 import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangSwaggerGenerator;
 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
@@ -100,11 +101,12 @@ public class MountPointSwagger implements DOMMountPointListener, AutoCloseable {
         return swaggerGenerator.generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount";
     }
 
-    public ResourceList getResourceList(final UriInfo uriInfo, final Long id) {
-        return getResourceList(uriInfo, id, 0, true);
+    public ResourceList getResourceList(final UriInfo uriInfo, final Long id, final URIType uriType) {
+        return getResourceList(uriInfo, id, 0, true, uriType);
     }
 
-    public ResourceList getResourceList(final UriInfo uriInfo, final Long id, final int pageNum, boolean all) {
+    public ResourceList getResourceList(final UriInfo uriInfo, final Long id, final int pageNum, boolean all,
+        final URIType uriType) {
         final YangInstanceIdentifier iid = getInstanceId(id);
         if (iid == null) {
             return null; // indicating not found.
@@ -119,7 +121,8 @@ public class MountPointSwagger implements DOMMountPointListener, AutoCloseable {
         dataStores.setPath(swaggerGenerator.generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
         resources.add(dataStores);
         final String urlPrefix = getYangMountUrl(iid);
-        final ResourceList list = swaggerGenerator.getResourceListing(uriInfo, context, urlPrefix, pageNum, all);
+        final ResourceList list = swaggerGenerator
+            .getResourceListing(uriInfo, context, urlPrefix, pageNum, all, uriType);
         resources.addAll(list.getApis());
         list.setApis(resources);
         return list;
@@ -152,7 +155,7 @@ public class MountPointSwagger implements DOMMountPointListener, AutoCloseable {
     }
 
     public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module,
-            final String revision) {
+        final String revision, final URIType uriType) {
         final YangInstanceIdentifier iid = getInstanceId(id);
         final SchemaContext context = getSchemaContext(iid);
         final String urlPrefix = getYangMountUrl(iid);
@@ -163,7 +166,7 @@ public class MountPointSwagger implements DOMMountPointListener, AutoCloseable {
         if (DATASTORES_LABEL.equals(module) && DATASTORES_REVISION.equals(revision)) {
             return generateDataStoreApiDoc(uriInfo, urlPrefix);
         }
-        return swaggerGenerator.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
+        return swaggerGenerator.getApiDeclaration(module, revision, uriInfo, context, urlPrefix, uriType);
     }
 
     private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
index 4946f633c160be274791ce9b41e943a281466923..ac91b379dc9e8195337e2f9bb014da7cb7190d08 100644 (file)
@@ -27,6 +27,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorDraftO2;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
 import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
@@ -73,7 +74,7 @@ public class ApiDocGeneratorTest {
             if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE)
                     && m.getQNameModule().getRevision().equals(DATE)) {
                 final ApiDeclaration doc = this.generator.getSwaggerDocSpec(m, "http://localhost:8080/restconf", "",
-                        this.schemaContext);
+                        this.schemaContext, URIType.DRAFT02);
                 validateToaster(doc);
                 validateTosterDocContainsModulePrefixes(doc);
                 validateSwaggerModules(doc);
@@ -235,7 +236,7 @@ public class ApiDocGeneratorTest {
             if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE_2)
                     && m.getQNameModule().getRevision().equals(REVISION_2)) {
                 final ApiDeclaration doc = this.generator.getSwaggerDocSpec(m, "http://localhost:8080/restconf", "",
-                        this.schemaContext);
+                        this.schemaContext, URIType.DRAFT02);
                 assertNotNull(doc);
 
                 // testing bugs.opendaylight.org bug 1290. UnionType model type.
@@ -254,7 +255,7 @@ public class ApiDocGeneratorTest {
             if (m.getQNameModule().getNamespace().toString().equals(NAMESPACE_2)
                     && m.getQNameModule().getRevision().equals(REVISION_2)) {
                 final ApiDeclaration doc = this.generator.getSwaggerDocSpec(m, "http://localhost:8080/restconf", "",
-                        this.schemaContext);
+                        this.schemaContext, URIType.DRAFT02);
                 assertNotNull(doc);
 
                 final ObjectNode models = doc.getModels();
@@ -325,7 +326,7 @@ public class ApiDocGeneratorTest {
     public void testGetResourceListing() throws Exception {
         final UriInfo info = this.helper.createMockUriInfo(HTTP_HOST);
 
-        final ResourceList resourceListing = this.generator.getResourceListing(info);
+        final ResourceList resourceListing = this.generator.getResourceListing(info, URIType.DRAFT02);
 
         Resource toaster = null;
         Resource toaster2 = null;
index 2514e679ee10516acca23e4d8bdb51c21c8fd87f..08cab58ddfb774a6ac6d262808f11f92fce8ee87 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
@@ -73,7 +74,7 @@ public class MountPointSwaggerTest {
     public void testGetResourceListBadIid() throws Exception {
         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
 
-        assertEquals(null, this.swagger.getResourceList(mockInfo, 1L));
+        assertEquals(null, this.swagger.getResourceList(mockInfo, 1L, URIType.DRAFT02));
     }
 
     @Test()
@@ -96,7 +97,7 @@ public class MountPointSwaggerTest {
         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
         this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
                                                  // mount points
-        final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L);
+        final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L, URIType.DRAFT02);
 
         Resource dataStoreResource = null;
         for (final Resource r : resourceList.getApis()) {
@@ -113,7 +114,8 @@ public class MountPointSwaggerTest {
         this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
                                                  // mount points
 
-        final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
+        final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-",
+            URIType.DRAFT02);
         assertNotNull("failed to find Datastore API", mountPointApi);
         final List<Api> apis = mountPointApi.getApis();
         assertEquals("Unexpected api list size", 3, apis.size());