2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.sal.rest.doc.maven;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.BufferedWriter;
15 import java.io.FileWriter;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
21 import java.util.Optional;
23 import java.util.function.Function;
24 import javax.ws.rs.core.UriInfo;
25 import org.apache.maven.project.MavenProject;
26 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGenerator;
27 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
28 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
29 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
33 import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This class gathers all yang defined {@link Module}s and generates Swagger compliant documentation.
40 public class StaticDocGenerator extends ApiDocGenerator implements BasicCodeGenerator, MavenProjectAware {
41 private static final Logger LOG = LoggerFactory.getLogger(StaticDocGenerator.class);
43 private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
44 + File.separator + "swagger-api-documentation";
47 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
48 public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir,
49 final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
51 List<File> result = new ArrayList<>();
53 // Create Base Directory
55 if (outputBaseDir == null) {
56 outputDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
58 outputDir = outputBaseDir;
61 if (!outputDir.mkdirs()) {
62 throw new IOException("Could not create directory " + outputDir);
65 // Create Resources directory
66 File resourcesDir = new File(outputDir, "resources");
67 if (!resourcesDir.mkdirs()) {
68 throw new IOException("Could not create directory " + resourcesDir);
72 File resourcesJsFile = new File(outputDir, "resources.js");
73 if (!resourcesJsFile.createNewFile()) {
74 LOG.info("File " + resourcesJsFile + " already exists.");
77 try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(resourcesJsFile))) {
78 ObjectMapper mapper = new ObjectMapper();
79 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
81 // Write resource listing to JS file
82 ResourceList resourceList = super.getResourceListing(null, context, "");
83 String resourceListJson = mapper.writeValueAsString(resourceList);
84 resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
85 bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
87 // Write resources/APIs to JS file and to disk
88 bufferedWriter.write("function jsonFor(resource) {\n\tswitch(resource) {\n");
89 for (Resource resource : resourceList.getApis()) {
90 int revisionIndex = resource.getPath().indexOf('(');
91 String name = resource.getPath().substring(0, revisionIndex);
92 String revision = resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
93 ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "");
94 String json = mapper.writeValueAsString(apiDeclaration);
95 // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
97 "\"models\":{}", "\"models\":" + apiDeclaration.getModels().toString().replace("\\\"", "\""));
98 // Escape single quotes and new lines
99 json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
100 bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
102 File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
104 try (BufferedWriter resourceFileWriter = new BufferedWriter(new FileWriter(resourceFile))) {
105 resourceFileWriter.write(json);
108 result.add(resourceFile);
110 bufferedWriter.write("\t}\n\treturn \"\";\n}");
113 result.add(resourcesJsFile);
118 protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
119 if (uriInfo == null) {
120 return name + "(" + revision + ")";
122 return super.generatePath(uriInfo, name, revision);
126 protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
127 if (uriInfo == null) {
128 return RESTCONF_CONTEXT_ROOT;
130 return super.createBasePathFromUriInfo(uriInfo);
134 public void setAdditionalConfig(final Map<String, String> additionalConfig) {
138 public void setResourceBaseDir(final File resourceBaseDir) {
142 public void setMavenProject(final MavenProject mavenProject) {