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.BaseYangSwaggerGeneratorDraft02;
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 BaseYangSwaggerGeneratorDraft02
41 implements BasicCodeGenerator, MavenProjectAware {
42 private static final Logger LOG = LoggerFactory.getLogger(StaticDocGenerator.class);
44 private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
45 + File.separator + "swagger-api-documentation";
47 public StaticDocGenerator() {
48 super(Optional.empty());
52 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
53 public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir,
54 final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
56 List<File> result = new ArrayList<>();
58 // Create Base Directory
60 if (outputBaseDir == null) {
61 outputDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
63 outputDir = outputBaseDir;
66 if (!outputDir.mkdirs()) {
67 throw new IOException("Could not create directory " + outputDir);
70 // Create Resources directory
71 File resourcesDir = new File(outputDir, "resources");
72 if (!resourcesDir.mkdirs()) {
73 throw new IOException("Could not create directory " + resourcesDir);
77 File resourcesJsFile = new File(outputDir, "resources.js");
78 if (!resourcesJsFile.createNewFile()) {
79 LOG.info("File " + resourcesJsFile + " already exists.");
82 try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(resourcesJsFile))) {
83 ObjectMapper mapper = new ObjectMapper();
84 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
86 // Write resource listing to JS file
87 ResourceList resourceList = super.getResourceListing(null, context, "");
88 String resourceListJson = mapper.writeValueAsString(resourceList);
89 resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
90 bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
92 // Write resources/APIs to JS file and to disk
93 bufferedWriter.write("function jsonFor(resource) {\n\tswitch(resource) {\n");
94 for (Resource resource : resourceList.getApis()) {
95 int revisionIndex = resource.getPath().indexOf('(');
96 String name = resource.getPath().substring(0, revisionIndex);
97 String revision = resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
98 ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "");
99 String json = mapper.writeValueAsString(apiDeclaration);
100 // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
102 "\"models\":{}", "\"models\":" + apiDeclaration.getModels().toString().replace("\\\"", "\""));
103 // Escape single quotes and new lines
104 json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
105 bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
107 File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
109 try (BufferedWriter resourceFileWriter = new BufferedWriter(new FileWriter(resourceFile))) {
110 resourceFileWriter.write(json);
113 result.add(resourceFile);
115 bufferedWriter.write("\t}\n\treturn \"\";\n}");
118 result.add(resourcesJsFile);
123 public String generatePath(final UriInfo uriInfo, final String name, final String revision) {
124 if (uriInfo == null) {
125 return name + "(" + revision + ")";
127 return super.generatePath(uriInfo, name, revision);
131 public String createBasePathFromUriInfo(final UriInfo uriInfo) {
132 if (uriInfo == null) {
133 return RESTCONF_CONTEXT_ROOT;
135 return super.createBasePathFromUriInfo(uriInfo);
139 public void setAdditionalConfig(final Map<String, String> additionalConfig) {
143 public void setResourceBaseDir(final File resourceBaseDir) {
147 public void setMavenProject(final MavenProject mavenProject) {