X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-docgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Frest%2Fdoc%2Fimpl%2FBaseYangSwaggerGenerator.java;h=55c62b24a1b38fa22b1a806b61f325340f91d94b;hb=4914063256c8776e3dba65f7573fa6950754501b;hp=7696ce6732baa6b4b2fc9756290fc5eeeeff75c0;hpb=fc67431555eaf291ab0a959813c25b3ba86bc5b7;p=netconf.git diff --git a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/BaseYangSwaggerGenerator.java b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/BaseYangSwaggerGenerator.java index 7696ce6732..55c62b24a1 100644 --- a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/BaseYangSwaggerGenerator.java +++ b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/BaseYangSwaggerGenerator.java @@ -62,6 +62,7 @@ public class BaseYangSwaggerGenerator { protected static final String API_VERSION = "1.0.0"; protected static final String SWAGGER_VERSION = "1.2"; protected static final String RESTCONF_CONTEXT_ROOT = "restconf"; + private static final String RESTCONF_DRAFT = "18"; static final String MODULE_NAME_SUFFIX = "_module"; protected static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); @@ -69,30 +70,33 @@ public class BaseYangSwaggerGenerator { // private Map MODULE_DOC_CACHE = new HashMap<>() private final ObjectMapper mapper = new ObjectMapper(); + private static boolean newDraft; protected BaseYangSwaggerGenerator() { - mapper.registerModule(new JsonOrgModule()); - mapper.configure(SerializationFeature.INDENT_OUTPUT, true); + this.mapper.registerModule(new JsonOrgModule()); + this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true); } /** * Return list of modules converted to swagger compliant resource list. */ - public ResourceList getResourceListing(UriInfo uriInfo, SchemaContext schemaContext, String context) { + public ResourceList getResourceListing(final UriInfo uriInfo, final SchemaContext schemaContext, + final String context) { - ResourceList resourceList = createResourceList(); + final ResourceList resourceList = createResourceList(); - Set modules = getSortedModules(schemaContext); + final Set modules = getSortedModules(schemaContext); - List resources = new ArrayList<>(modules.size()); + final List resources = new ArrayList<>(modules.size()); LOG.info("Modules found [{}]", modules.size()); - for (Module module : modules) { - String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision()); - Resource resource = new Resource(); + for (final Module module : modules) { + final String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision()); + final Resource resource = new Resource(); LOG.debug("Working on [{},{}]...", module.getName(), revisionString); - ApiDeclaration doc = getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context); + final ApiDeclaration doc = + getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context); if (doc != null) { resource.setPath(generatePath(uriInfo, module.getName(), revisionString)); @@ -108,30 +112,31 @@ public class BaseYangSwaggerGenerator { } protected ResourceList createResourceList() { - ResourceList resourceList = new ResourceList(); + final ResourceList resourceList = new ResourceList(); resourceList.setApiVersion(API_VERSION); resourceList.setSwaggerVersion(SWAGGER_VERSION); return resourceList; } - protected String generatePath(UriInfo uriInfo, String name, String revision) { - URI uri = uriInfo.getRequestUriBuilder().path(generateCacheKey(name, revision)).build(); + protected String generatePath(final UriInfo uriInfo, final String name, final String revision) { + final URI uri = uriInfo.getRequestUriBuilder().path(generateCacheKey(name, revision)).build(); return uri.toASCIIString(); } - public ApiDeclaration getApiDeclaration(String moduleName, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) { + public ApiDeclaration getApiDeclaration(final String moduleName, final String revision, final UriInfo uriInfo, + final SchemaContext schemaContext, final String context) { Date rev = null; try { - if (revision != null && !revision.equals("0000-00-00")) { + if ((revision != null) && !revision.equals("0000-00-00")) { rev = SIMPLE_DATE_FORMAT.parse(revision); } - } catch (ParseException e) { + } catch (final ParseException e) { throw new IllegalArgumentException(e); } if (rev != null) { - Calendar cal = new GregorianCalendar(); + final Calendar cal = new GregorianCalendar(); cal.setTime(rev); @@ -140,44 +145,46 @@ public class BaseYangSwaggerGenerator { } } - Module module = schemaContext.findModuleByName(moduleName, rev); + final Module module = schemaContext.findModuleByName(moduleName, rev); Preconditions.checkArgument(module != null, "Could not find module by name,revision: " + moduleName + "," + revision); return getApiDeclaration(module, rev, uriInfo, context, schemaContext); } - public ApiDeclaration getApiDeclaration(Module module, Date revision, UriInfo uriInfo, String context, SchemaContext schemaContext) { - String basePath = createBasePathFromUriInfo(uriInfo); + public ApiDeclaration getApiDeclaration(final Module module, final Date revision, final UriInfo uriInfo, + final String context, final SchemaContext schemaContext) { + final String basePath = createBasePathFromUriInfo(uriInfo); - ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext); + final ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext); if (doc != null) { return doc; } return null; } - protected String createBasePathFromUriInfo(UriInfo uriInfo) { + protected String createBasePathFromUriInfo(final UriInfo uriInfo) { String portPart = ""; - int port = uriInfo.getBaseUri().getPort(); + final int port = uriInfo.getBaseUri().getPort(); if (port != -1) { portPart = ":" + port; } - String basePath = new StringBuilder(uriInfo.getBaseUri().getScheme()).append("://") - .append(uriInfo.getBaseUri().getHost()).append(portPart).append("/").append(RESTCONF_CONTEXT_ROOT) - .toString(); + final String basePath = + new StringBuilder(uriInfo.getBaseUri().getScheme()).append("://").append(uriInfo.getBaseUri().getHost()) + .append(portPart).append("/").append(RESTCONF_CONTEXT_ROOT).toString(); return basePath; } - public ApiDeclaration getSwaggerDocSpec(Module m, String basePath, String context, SchemaContext schemaContext) { - ApiDeclaration doc = createApiDeclaration(basePath); + public ApiDeclaration getSwaggerDocSpec(final Module m, final String basePath, final String context, + final SchemaContext schemaContext) { + final ApiDeclaration doc = createApiDeclaration(basePath); - List apis = new ArrayList<>(); + final List apis = new ArrayList<>(); boolean hasAddRootPostLink = false; - Collection dataSchemaNodes = m.getChildNodes(); + final Collection dataSchemaNodes = m.getChildNodes(); LOG.debug("child nodes size [{}]", dataSchemaNodes.size()); - for (DataSchemaNode node : dataSchemaNodes) { + for (final DataSchemaNode node : dataSchemaNodes) { if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) { LOG.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName()); @@ -185,34 +192,39 @@ public class BaseYangSwaggerGenerator { String resourcePath; /* - * Only when the node's config statement is true, such apis as GET/PUT/POST/DELETE config - * are added for this node. + * Only when the node's config statement is true, such apis as + * GET/PUT/POST/DELETE config are added for this node. */ - if (node.isConfiguration()) { // This node's config statement is true. - resourcePath = getDataStorePath("/config/", context); + if (node.isConfiguration()) { // This node's config statement is + // true. + resourcePath = getDataStorePath("config", context); /* - * When there are two or more top container or list nodes whose config statement is true in module, - * make sure that only one root post link is added for this module. + * When there are two or more top container or list nodes + * whose config statement is true in module, make sure that + * only one root post link is added for this module. */ if (!hasAddRootPostLink) { LOG.debug("Has added root post link for module {}", m.getName()); - addRootPostLink(m, (DataNodeContainer) node, pathParams, resourcePath, apis); + addRootPostLink(m, (DataNodeContainer) node, pathParams, resourcePath, "config", apis); + hasAddRootPostLink = true; } - addApis(node, apis, resourcePath, pathParams, schemaContext, true); + addApis(node, apis, resourcePath, pathParams, schemaContext, true, m.getName(), "config"); } - pathParams = new ArrayList<>(); - resourcePath = getDataStorePath("/operational/", context); - addApis(node, apis, resourcePath, pathParams, schemaContext, false); + resourcePath = getDataStorePath("operational", context); + + addApis(node, apis, resourcePath, pathParams, schemaContext, false, m.getName(), "operational"); } } - Set rpcs = m.getRpcs(); - for (RpcDefinition rpcDefinition : rpcs) { - String resourcePath = getDataStorePath("/operations/", context); + final Set rpcs = m.getRpcs(); + for (final RpcDefinition rpcDefinition : rpcs) { + final String resourcePath; + resourcePath = getDataStorePath("operations", context); + addRpcs(rpcDefinition, apis, resourcePath, schemaContext); } @@ -223,10 +235,10 @@ public class BaseYangSwaggerGenerator { JSONObject models = null; try { - models = jsonConverter.convertToJsonSchema(m, schemaContext); + models = this.jsonConverter.convertToJsonSchema(m, schemaContext); doc.setModels(models); if (LOG.isDebugEnabled()) { - LOG.debug(mapper.writeValueAsString(doc)); + LOG.debug(this.mapper.writeValueAsString(doc)); } } catch (IOException | JSONException e) { LOG.error("Exception occured in ModelGenerator", e); @@ -238,18 +250,18 @@ public class BaseYangSwaggerGenerator { } private void addRootPostLink(final Module module, final DataNodeContainer node, final List pathParams, - final String resourcePath, final List apis) { + final String resourcePath, final String dataStore, final List apis) { if (containsListOrContainer(module.getChildNodes())) { final Api apiForRootPostUri = new Api(); - apiForRootPostUri.setPath(resourcePath); + apiForRootPostUri.setPath(resourcePath.concat(getContent(dataStore))); apiForRootPostUri.setOperations(operationPost(module.getName() + MODULE_NAME_SUFFIX, - module.getDescription(), module, pathParams, true)); + module.getDescription(), module, pathParams, true, "")); apis.add(apiForRootPostUri); } } - protected ApiDeclaration createApiDeclaration(String basePath) { - ApiDeclaration doc = new ApiDeclaration(); + protected ApiDeclaration createApiDeclaration(final String basePath) { + final ApiDeclaration doc = new ApiDeclaration(); doc.setApiVersion(API_VERSION); doc.setSwaggerVersion(SWAGGER_VERSION); doc.setBasePath(basePath); @@ -257,127 +269,165 @@ public class BaseYangSwaggerGenerator { return doc; } - protected String getDataStorePath(String dataStore, String context) { - return dataStore + context; + protected String getDataStorePath(final String dataStore, final String context) { + if (newDraft) { + if ("config".contains(dataStore) || "operational".contains(dataStore)) { + return "/" + RESTCONF_DRAFT + "/data" + context; + } else { + return "/" + RESTCONF_DRAFT + "/operations" + context; + } + } else { + return "/" + dataStore + context; + } } - private String generateCacheKey(String module, String revision) { + private String generateCacheKey(final String module, final String revision) { return module + "(" + revision + ")"; } - private void addApis(DataSchemaNode node, List apis, String parentPath, List parentPathParams, SchemaContext schemaContext, - boolean addConfigApi) { + private void addApis(final DataSchemaNode node, final List apis, final String parentPath, + final List parentPathParams, final SchemaContext schemaContext, final boolean addConfigApi, + final String parentName, final String dataStore) { + final Api api = new Api(); + final List pathParams = new ArrayList<>(parentPathParams); - Api api = new Api(); - List pathParams = new ArrayList<>(parentPathParams); - - String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/"; + final String resourcePath = parentPath + "/" + createPath(node, pathParams, schemaContext); LOG.debug("Adding path: [{}]", resourcePath); - api.setPath(resourcePath); + api.setPath(resourcePath.concat(getContent(dataStore))); - Iterable childSchemaNodes = Collections.emptySet(); + Iterable childSchemaNodes = Collections. emptySet(); if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) { - DataNodeContainer dataNodeContainer = (DataNodeContainer) node; + final DataNodeContainer dataNodeContainer = (DataNodeContainer) node; childSchemaNodes = dataNodeContainer.getChildNodes(); } - api.setOperations(operation(node, pathParams, addConfigApi, childSchemaNodes)); + api.setOperations(operation(node, pathParams, addConfigApi, childSchemaNodes, parentName)); apis.add(api); - for (DataSchemaNode childNode : childSchemaNodes) { - if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) { + for (final DataSchemaNode childNode : childSchemaNodes) { + if ((childNode instanceof ListSchemaNode) || (childNode instanceof ContainerSchemaNode)) { // keep config and operation attributes separate. if (childNode.isConfiguration() == addConfigApi) { - addApis(childNode, apis, resourcePath, pathParams, schemaContext, addConfigApi); + final String newParent = parentName + "/" + node.getQName().getLocalName(); + addApis(childNode, apis, resourcePath, pathParams, schemaContext, addConfigApi, newParent, + dataStore); } } } + } + protected static String getContent(final String dataStore) { + if (newDraft) { + if ("operational".contains(dataStore)) { + return "?content=nonconfig"; + } else if ("config".contains(dataStore)) { + return "?content=config"; + } else { + return ""; + } + } else { + return ""; + } } private boolean containsListOrContainer(final Iterable nodes) { - for (DataSchemaNode child : nodes) { - if (child instanceof ListSchemaNode || child instanceof ContainerSchemaNode) { + for (final DataSchemaNode child : nodes) { + if ((child instanceof ListSchemaNode) || (child instanceof ContainerSchemaNode)) { return true; } } return false; } - private List operation(DataSchemaNode node, List pathParams, boolean isConfig, Iterable childSchemaNodes) { - List operations = new ArrayList<>(); + private List operation(final DataSchemaNode node, final List pathParams, + final boolean isConfig, final Iterable childSchemaNodes, final String parentName) { + final List operations = new ArrayList<>(); - Get getBuilder = new Get(node, isConfig); + final Get getBuilder = new Get(node, isConfig); operations.add(getBuilder.pathParams(pathParams).build()); if (isConfig) { - Put putBuilder = new Put(node.getQName().getLocalName(), - node.getDescription()); + final Put putBuilder = new Put(node.getQName().getLocalName(), node.getDescription(), parentName); operations.add(putBuilder.pathParams(pathParams).build()); - Delete deleteBuilder = new Delete(node); + final Delete deleteBuilder = new Delete(node); operations.add(deleteBuilder.pathParams(pathParams).build()); if (containsListOrContainer(childSchemaNodes)) { operations.addAll(operationPost(node.getQName().getLocalName(), node.getDescription(), - (DataNodeContainer) node, pathParams, isConfig)); + (DataNodeContainer) node, pathParams, isConfig, parentName + "/")); } } return operations; } - private List operationPost(final String name, final String description, final DataNodeContainer dataNodeContainer, List pathParams, boolean isConfig) { - List operations = new ArrayList<>(); + private List operationPost(final String name, final String description, + final DataNodeContainer dataNodeContainer, final List pathParams, final boolean isConfig, + final String parentName) { + final List operations = new ArrayList<>(); if (isConfig) { - Post postBuilder = new Post(name, description, dataNodeContainer); + final Post postBuilder = new Post(name, parentName + name, description, dataNodeContainer); operations.add(postBuilder.pathParams(pathParams).build()); } return operations; } - private String createPath(final DataSchemaNode schemaNode, List pathParams, SchemaContext schemaContext) { - ArrayList pathListParams = new ArrayList<>(); - StringBuilder path = new StringBuilder(); - String localName = resolvePathArgumentsName(schemaNode, schemaContext); + private String createPath(final DataSchemaNode schemaNode, final List pathParams, + final SchemaContext schemaContext) { + final ArrayList pathListParams = new ArrayList<>(); + final StringBuilder path = new StringBuilder(); + final String localName = resolvePathArgumentsName(schemaNode, schemaContext); path.append(localName); if ((schemaNode instanceof ListSchemaNode)) { final List listKeys = ((ListSchemaNode) schemaNode).getKeyDefinition(); + StringBuilder keyBuilder = null; + if (newDraft) { + keyBuilder = new StringBuilder("="); + } + for (final QName listKey : listKeys) { - DataSchemaNode dataChildByName = ((DataNodeContainer) schemaNode).getDataChildByName(listKey); + final DataSchemaNode dataChildByName = ((DataNodeContainer) schemaNode).getDataChildByName(listKey); pathListParams.add(((LeafSchemaNode) dataChildByName)); - - String pathParamIdentifier = new StringBuilder("/{").append(listKey.getLocalName()).append("}") - .toString(); + final String pathParamIdentifier; + if (newDraft) { + pathParamIdentifier = keyBuilder.append("{").append(listKey.getLocalName()).append("}").toString(); + } else { + pathParamIdentifier = "/{" + listKey.getLocalName() + "}"; + } path.append(pathParamIdentifier); - Parameter pathParam = new Parameter(); + final Parameter pathParam = new Parameter(); pathParam.setName(listKey.getLocalName()); pathParam.setDescription(dataChildByName.getDescription()); pathParam.setType("string"); pathParam.setParamType("path"); pathParams.add(pathParam); + if (newDraft) { + keyBuilder = new StringBuilder(","); + } } } return path.toString(); } - protected void addRpcs(RpcDefinition rpcDefn, List apis, String parentPath, SchemaContext schemaContext) { - Api rpc = new Api(); - String resourcePath = parentPath + resolvePathArgumentsName(rpcDefn, schemaContext); + protected void addRpcs(final RpcDefinition rpcDefn, final List apis, final String parentPath, + final SchemaContext schemaContext) { + final Api rpc = new Api(); + final String resourcePath = parentPath + "/" + resolvePathArgumentsName(rpcDefn, schemaContext); rpc.setPath(resourcePath); - Operation operationSpec = new Operation(); + final Operation operationSpec = new Operation(); operationSpec.setMethod("POST"); operationSpec.setNotes(rpcDefn.getDescription()); operationSpec.setNickname(rpcDefn.getQName().getLocalName()); if (rpcDefn.getOutput() != null) { - operationSpec.setType("(" + rpcDefn.getQName().getLocalName() + ")output"); + operationSpec.setType("(" + rpcDefn.getQName().getLocalName() + ")output" + OperationBuilder.TOP); } if (rpcDefn.getInput() != null) { - Parameter payload = new Parameter(); + final Parameter payload = new Parameter(); payload.setParamType("body"); - payload.setType("(" + rpcDefn.getQName().getLocalName() + ")input"); + payload.setType("(" + rpcDefn.getQName().getLocalName() + ")input" + OperationBuilder.TOP); operationSpec.setParameters(Collections.singletonList(payload)); operationSpec.setConsumes(OperationBuilder.CONSUMES_PUT_POST); } @@ -387,18 +437,18 @@ public class BaseYangSwaggerGenerator { apis.add(rpc); } - protected SortedSet getSortedModules(SchemaContext schemaContext) { + protected SortedSet getSortedModules(final SchemaContext schemaContext) { if (schemaContext == null) { return new TreeSet<>(); } - Set modules = schemaContext.getModules(); + final Set modules = schemaContext.getModules(); - SortedSet sortedModules = new TreeSet<>((module1, module2) -> { + final SortedSet sortedModules = new TreeSet<>((module1, module2) -> { int result = module1.getName().compareTo(module2.getName()); if (result == 0) { - Date module1Revision = module1.getRevision() != null ? module1.getRevision() : new Date(0); - Date module2Revision = module2.getRevision() != null ? module2.getRevision() : new Date(0); + final Date module1Revision = module1.getRevision() != null ? module1.getRevision() : new Date(0); + final Date module2Revision = module2.getRevision() != null ? module2.getRevision() : new Date(0); result = module1Revision.compareTo(module2Revision); } if (result == 0) { @@ -406,7 +456,7 @@ public class BaseYangSwaggerGenerator { } return result; }); - for (Module m : modules) { + for (final Module m : modules) { if (m != null) { sortedModules.add(m); } @@ -414,4 +464,7 @@ public class BaseYangSwaggerGenerator { return sortedModules; } + public void setDraft(final boolean draft) { + this.newDraft = draft; + } }