X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-docgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fdoc%2Fimpl%2FBaseYangSwaggerGenerator.java;h=c86b89c004b645aa42e31b86320077278ff23551;hb=cb4800008fe5852753b2809da429ec373dd0092a;hp=1b2718251446dde021d3cc62b794b990f8852dc4;hpb=d7ce7c5acee7e6f7cd7895ceff5af63ac53789ad;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/BaseYangSwaggerGenerator.java b/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/BaseYangSwaggerGenerator.java index 1b27182514..c86b89c004 100644 --- a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/BaseYangSwaggerGenerator.java +++ b/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/BaseYangSwaggerGenerator.java @@ -8,7 +8,6 @@ package org.opendaylight.controller.sal.rest.doc.impl; import static org.opendaylight.controller.sal.rest.doc.util.RestDocgenUtil.resolvePathArgumentsName; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule; @@ -20,10 +19,12 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.GregorianCalendar; import java.util.List; import java.util.Set; import java.util.SortedSet; @@ -52,11 +53,13 @@ import org.slf4j.LoggerFactory; public class BaseYangSwaggerGenerator { - private static Logger _logger = LoggerFactory.getLogger(BaseYangSwaggerGenerator.class); + private static final Logger LOG = LoggerFactory.getLogger(BaseYangSwaggerGenerator.class); 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"; + + static final String MODULE_NAME_SUFFIX = "_module"; protected final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private final ModelGenerator jsonConverter = new ModelGenerator(); @@ -82,19 +85,19 @@ public class BaseYangSwaggerGenerator { List resources = new ArrayList<>(modules.size()); - _logger.info("Modules found [{}]", modules.size()); + LOG.info("Modules found [{}]", modules.size()); for (Module module : modules) { String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision()); Resource resource = new Resource(); - _logger.debug("Working on [{},{}]...", module.getName(), revisionString); + LOG.debug("Working on [{},{}]...", module.getName(), revisionString); ApiDeclaration doc = getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context); if (doc != null) { resource.setPath(generatePath(uriInfo, module.getName(), revisionString)); resources.add(resource); } else { - _logger.debug("Could not generate doc for {},{}", module.getName(), revisionString); + LOG.debug("Could not generate doc for {},{}", module.getName(), revisionString); } } @@ -117,11 +120,25 @@ public class BaseYangSwaggerGenerator { public ApiDeclaration getApiDeclaration(String module, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) { Date rev = null; + try { - rev = SIMPLE_DATE_FORMAT.parse(revision); + if(revision != null && !revision.equals("0000-00-00")) { + rev = SIMPLE_DATE_FORMAT.parse(revision); + } } catch (ParseException e) { throw new IllegalArgumentException(e); } + + if(rev != null) { + Calendar cal = new GregorianCalendar(); + + cal.setTime(rev); + + if(cal.get(Calendar.YEAR) < 1970) { + rev = null; + } + } + Module m = schemaContext.findModuleByName(module, rev); Preconditions.checkArgument(m != null, "Could not find module by name,revision: " + module + "," + revision); @@ -156,29 +173,30 @@ public class BaseYangSwaggerGenerator { List apis = new ArrayList(); Collection dataSchemaNodes = m.getChildNodes(); - _logger.debug("child nodes size [{}]", dataSchemaNodes.size()); + LOG.debug("child nodes size [{}]", dataSchemaNodes.size()); for (DataSchemaNode node : dataSchemaNodes) { if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) { - _logger.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName()); + LOG.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName()); List pathParams = new ArrayList(); String resourcePath = getDataStorePath("/config/", context); + addRootPostLink(m, (DataNodeContainer) node, pathParams, resourcePath, apis); addApis(node, apis, resourcePath, pathParams, schemaContext, true); pathParams = new ArrayList(); resourcePath = getDataStorePath("/operational/", context); addApis(node, apis, resourcePath, pathParams, schemaContext, false); } + } - Set rpcs = m.getRpcs(); - for (RpcDefinition rpcDefinition : rpcs) { - String resourcePath = getDataStorePath("/operations/", context); - addRpcs(rpcDefinition, apis, resourcePath, schemaContext); - } + Set rpcs = m.getRpcs(); + for (RpcDefinition rpcDefinition : rpcs) { + String resourcePath = getDataStorePath("/operations/", context); + addRpcs(rpcDefinition, apis, resourcePath, schemaContext); } - _logger.debug("Number of APIs found [{}]", apis.size()); + LOG.debug("Number of APIs found [{}]", apis.size()); if (!apis.isEmpty()) { doc.setApis(apis); @@ -187,8 +205,8 @@ public class BaseYangSwaggerGenerator { try { models = jsonConverter.convertToJsonSchema(m, schemaContext); doc.setModels(models); - if (_logger.isDebugEnabled()) { - _logger.debug(mapper.writeValueAsString(doc)); + if (LOG.isDebugEnabled()) { + LOG.debug(mapper.writeValueAsString(doc)); } } catch (IOException | JSONException e) { e.printStackTrace(); @@ -199,6 +217,16 @@ public class BaseYangSwaggerGenerator { return null; } + private void addRootPostLink(final Module m, final DataNodeContainer node, final List pathParams, + final String resourcePath, final List apis) { + if (containsListOrContainer(m.getChildNodes())) { + final Api apiForRootPostUri = new Api(); + apiForRootPostUri.setPath(resourcePath); + apiForRootPostUri.setOperations(operationPost(m.getName()+MODULE_NAME_SUFFIX, m.getDescription(), m, pathParams, true)); + apis.add(apiForRootPostUri); + } + } + protected ApiDeclaration createApiDeclaration(String basePath) { ApiDeclaration doc = new ApiDeclaration(); doc.setApiVersion(API_VERSION); @@ -227,47 +255,74 @@ public class BaseYangSwaggerGenerator { List pathParams = new ArrayList(parentPathParams); String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/"; - _logger.debug("Adding path: [{}]", resourcePath); + LOG.debug("Adding path: [{}]", resourcePath); api.setPath(resourcePath); - api.setOperations(operations(node, pathParams, addConfigApi)); - apis.add(api); + + Iterable childSchemaNodes = Collections. emptySet(); if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) { - DataNodeContainer schemaNode = (DataNodeContainer) node; - - for (DataSchemaNode childNode : schemaNode.getChildNodes()) { - // We don't support going to leaf nodes today. Only lists and - // containers. - if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) { - // keep config and operation attributes separate. - if (childNode.isConfiguration() == addConfigApi) { - addApis(childNode, apis, resourcePath, pathParams, schemaContext, addConfigApi); - } + DataNodeContainer dataNodeContainer = (DataNodeContainer) node; + childSchemaNodes = dataNodeContainer.getChildNodes(); + } + api.setOperations(operation(node, pathParams, addConfigApi, childSchemaNodes)); + apis.add(api); + + for (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); } } } } + private boolean containsListOrContainer(final Iterable nodes) { + for (DataSchemaNode child : nodes) { + if (child instanceof ListSchemaNode || child instanceof ContainerSchemaNode) { + return true; + } + } + return false; + } + /** * @param node * @param pathParams * @return */ - private List operations(DataSchemaNode node, List pathParams, boolean isConfig) { + private List operation(DataSchemaNode node, List pathParams, boolean isConfig, Iterable childSchemaNodes) { List operations = new ArrayList<>(); OperationBuilder.Get getBuilder = new OperationBuilder.Get(node, isConfig); operations.add(getBuilder.pathParams(pathParams).build()); if (isConfig) { - OperationBuilder.Post postBuilder = new OperationBuilder.Post(node); - operations.add(postBuilder.pathParams(pathParams).build()); - - OperationBuilder.Put putBuilder = new OperationBuilder.Put(node); + OperationBuilder.Put putBuilder = new OperationBuilder.Put(node.getQName().getLocalName(), + node.getDescription()); operations.add(putBuilder.pathParams(pathParams).build()); OperationBuilder.Delete deleteBuilder = new OperationBuilder.Delete(node); operations.add(deleteBuilder.pathParams(pathParams).build()); + + if (containsListOrContainer(childSchemaNodes)) { + operations.addAll(operationPost(node.getQName().getLocalName(), node.getDescription(), (DataNodeContainer) node, + pathParams, isConfig)); + } + } + return operations; + } + + /** + * @param node + * @param pathParams + * @return + */ + private List operationPost(final String name, final String description, final DataNodeContainer dataNodeContainer, List pathParams, boolean isConfig) { + List operations = new ArrayList<>(); + if (isConfig) { + OperationBuilder.Post postBuilder = new OperationBuilder.Post(name, description, dataNodeContainer); + operations.add(postBuilder.pathParams(pathParams).build()); } return operations; } @@ -333,13 +388,15 @@ public class BaseYangSwaggerGenerator { SortedSet sortedModules = new TreeSet<>(new Comparator() { @Override - public int compare(Module o1, Module o2) { - int result = o1.getName().compareTo(o2.getName()); + public int compare(Module module1, Module module2) { + int result = module1.getName().compareTo(module2.getName()); if (result == 0) { - result = o1.getRevision().compareTo(o2.getRevision()); + Date module1Revision = module1.getRevision() != null ? module1.getRevision() : new Date(0); + Date module2Revision = module2.getRevision() != null ? module2.getRevision() : new Date(0); + result = module1Revision.compareTo(module2Revision); } if (result == 0) { - result = o1.getNamespace().compareTo(o2.getNamespace()); + result = module1.getNamespace().compareTo(module2.getNamespace()); } return result; }