Skip to content

Commit

Permalink
Updating Action Rest Endpoints on Swagger UI
Browse files Browse the repository at this point in the history
NETCONF-647

Signed-off-by: ajay_dp001 <[email protected]>
Change-Id: I790d81a833bdcb611459b3418fbf66be2388e050
  • Loading branch information
ajay_dp001 authored and rovarga committed Nov 4, 2019
1 parent cf8760f commit aaf133b
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -84,7 +85,7 @@ public Collection<File> generateSources(final EffectiveModelContext context, fin
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");
Expand All @@ -95,7 +96,8 @@ public Collection<File> generateSources(final EffectiveModelContext context, fin
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,10 +66,10 @@ public ApiDocServiceImpl(MountPointSwaggerGeneratorDraft02 mountPointSwaggerGene
@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();
Expand All @@ -79,10 +81,10 @@ public synchronized Response getRootDoc(final UriInfo uriInfo) {
@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();
Expand All @@ -99,7 +101,7 @@ public synchronized Response getApiExplorer(final UriInfo uriInfo) {
@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;
Expand All @@ -115,10 +117,12 @@ public synchronized Response getMountRootDoc(final String instanceNum, final Uri
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
Expand All @@ -127,12 +131,12 @@ public synchronized Response getMountRootDoc(final String instanceNum, final Uri

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();
}
Expand All @@ -141,15 +145,20 @@ public synchronized Response getMountRootDoc(final String instanceNum, final Uri
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;
}
}
Loading

0 comments on commit aaf133b

Please sign in to comment.