X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-docgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fdoc%2Fimpl%2FBaseYangSwaggerGenerator.java;fp=opendaylight%2Fmd-sal%2Fsal-rest-docgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fdoc%2Fimpl%2FBaseYangSwaggerGenerator.java;h=0000000000000000000000000000000000000000;hp=c40eca00513dd005b5bed6d24335015ce36000cf;hb=89b8b59cd26fd4810293ff14386eb29a71da9fac;hpb=9ba2b4eca79bcc0e78099b133296801c8d45a6c4 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 deleted file mode 100644 index c40eca0051..0000000000 --- a/opendaylight/md-sal/sal-rest-docgen/src/main/java/org/opendaylight/controller/sal/rest/doc/impl/BaseYangSwaggerGenerator.java +++ /dev/null @@ -1,413 +0,0 @@ -/* - * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -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; -import com.google.common.base.Preconditions; -import java.io.IOException; -import java.net.URI; -import java.text.DateFormat; -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; -import java.util.TreeSet; -import javax.ws.rs.core.UriInfo; -import org.json.JSONException; -import org.json.JSONObject; -import org.opendaylight.controller.sal.rest.doc.model.builder.OperationBuilder; -import org.opendaylight.controller.sal.rest.doc.swagger.Api; -import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration; -import org.opendaylight.controller.sal.rest.doc.swagger.Operation; -import org.opendaylight.controller.sal.rest.doc.swagger.Parameter; -import org.opendaylight.controller.sal.rest.doc.swagger.Resource; -import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList; -import org.opendaylight.yangtools.yang.common.QName; -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.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.RpcDefinition; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class BaseYangSwaggerGenerator { - - 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(); - - // private Map MODULE_DOC_CACHE = new HashMap<>() - private final ObjectMapper mapper = new ObjectMapper(); - - protected BaseYangSwaggerGenerator() { - mapper.registerModule(new JsonOrgModule()); - mapper.configure(SerializationFeature.INDENT_OUTPUT, true); - } - - /** - * - * @param uriInfo - * @param operType - * @return list of modules converted to swagger compliant resource list. - */ - public ResourceList getResourceListing(UriInfo uriInfo, SchemaContext schemaContext, String context) { - - ResourceList resourceList = createResourceList(); - - Set modules = getSortedModules(schemaContext); - - 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(); - 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 { - LOG.debug("Could not generate doc for {},{}", module.getName(), revisionString); - } - } - - resourceList.setApis(resources); - - return resourceList; - } - - protected ResourceList createResourceList() { - 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(); - return uri.toASCIIString(); - } - - public ApiDeclaration getApiDeclaration(String module, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) { - Date rev = null; - - try { - 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); - - return getApiDeclaration(m, rev, uriInfo, context, schemaContext); - } - - public ApiDeclaration getApiDeclaration(Module module, Date revision, UriInfo uriInfo, String context, SchemaContext schemaContext) { - String basePath = createBasePathFromUriInfo(uriInfo); - - ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext); - if (doc != null) { - return doc; - } - return null; - } - - protected String createBasePathFromUriInfo(UriInfo uriInfo) { - String portPart = ""; - 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(); - return basePath; - } - - public ApiDeclaration getSwaggerDocSpec(Module m, String basePath, String context, SchemaContext schemaContext) { - ApiDeclaration doc = createApiDeclaration(basePath); - - List apis = new ArrayList(); - - Collection dataSchemaNodes = m.getChildNodes(); - LOG.debug("child nodes size [{}]", dataSchemaNodes.size()); - for (DataSchemaNode node : dataSchemaNodes) { - if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) { - - 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); - } - - LOG.debug("Number of APIs found [{}]", apis.size()); - - if (!apis.isEmpty()) { - doc.setApis(apis); - JSONObject models = null; - - try { - models = jsonConverter.convertToJsonSchema(m, schemaContext); - doc.setModels(models); - if (LOG.isDebugEnabled()) { - LOG.debug(mapper.writeValueAsString(doc)); - } - } catch (IOException | JSONException e) { - e.printStackTrace(); - } - - return doc; - } - 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); - doc.setSwaggerVersion(SWAGGER_VERSION); - doc.setBasePath(basePath); - doc.setProduces(Arrays.asList("application/json", "application/xml")); - return doc; - } - - protected String getDataStorePath(String dataStore, String context) { - return dataStore + context; - } - - private String generateCacheKey(Module m) { - return generateCacheKey(m.getName(), SIMPLE_DATE_FORMAT.format(m.getRevision())); - } - - private String generateCacheKey(String module, String revision) { - return module + "(" + revision + ")"; - } - - private void addApis(DataSchemaNode node, List apis, String parentPath, List parentPathParams, SchemaContext schemaContext, - boolean addConfigApi) { - - Api api = new Api(); - List pathParams = new ArrayList(parentPathParams); - - String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/"; - LOG.debug("Adding path: [{}]", resourcePath); - api.setPath(resourcePath); - - Iterable childSchemaNodes = Collections. emptySet(); - if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) { - 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 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.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; - } - - private String createPath(final DataSchemaNode schemaNode, List pathParams, SchemaContext schemaContext) { - ArrayList pathListParams = new ArrayList(); - StringBuilder path = new StringBuilder(); - String localName = resolvePathArgumentsName(schemaNode, schemaContext); - path.append(localName); - - if ((schemaNode instanceof ListSchemaNode)) { - final List listKeys = ((ListSchemaNode) schemaNode).getKeyDefinition(); - for (final QName listKey : listKeys) { - DataSchemaNode _dataChildByName = ((DataNodeContainer) schemaNode).getDataChildByName(listKey); - pathListParams.add(((LeafSchemaNode) _dataChildByName)); - - String pathParamIdentifier = new StringBuilder("/{").append(listKey.getLocalName()).append("}") - .toString(); - path.append(pathParamIdentifier); - - Parameter pathParam = new Parameter(); - pathParam.setName(listKey.getLocalName()); - pathParam.setDescription(_dataChildByName.getDescription()); - pathParam.setType("string"); - pathParam.setParamType("path"); - - pathParams.add(pathParam); - } - } - return path.toString(); - } - - protected void addRpcs(RpcDefinition rpcDefn, List apis, String parentPath, SchemaContext schemaContext) { - Api rpc = new Api(); - String resourcePath = parentPath + resolvePathArgumentsName(rpcDefn, schemaContext); - rpc.setPath(resourcePath); - - 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"); - } - if (rpcDefn.getInput() != null) { - Parameter payload = new Parameter(); - payload.setParamType("body"); - payload.setType("(" + rpcDefn.getQName().getLocalName() + ")input"); - operationSpec.setParameters(Collections.singletonList(payload)); - operationSpec.setConsumes(OperationBuilder.CONSUMES_PUT_POST); - } - - rpc.setOperations(Arrays.asList(operationSpec)); - - apis.add(rpc); - } - - protected SortedSet getSortedModules(SchemaContext schemaContext) { - if (schemaContext == null) { - return new TreeSet<>(); - } - - Set modules = schemaContext.getModules(); - - SortedSet sortedModules = new TreeSet<>(new Comparator() { - @Override - public int compare(Module module1, Module 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); - result = module1Revision.compareTo(module2Revision); - } - if (result == 0) { - result = module1.getNamespace().compareTo(module2.getNamespace()); - } - return result; - } - }); - for (Module m : modules) { - if (m != null) { - sortedModules.add(m); - } - } - return sortedModules; - } - -}