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 java.io.BufferedWriter;
14 import java.io.FileWriter;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
20 import java.util.Optional;
22 import java.util.function.Function;
23 import javax.ws.rs.core.UriInfo;
24 import org.apache.maven.project.MavenProject;
25 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGenerator;
26 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
27 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
28 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
32 import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
35 * This class gathers all yang defined {@link Module}s and generates Swagger compliant documentation.
37 public class StaticDocGenerator extends ApiDocGenerator implements BasicCodeGenerator, MavenProjectAware {
38 private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
39 + File.separator + "swagger-api-documentation";
41 private MavenProject mavenProject;
42 private File projectBaseDir;
43 private Map<String, String> additionalConfig;
44 private File resourceBaseDir;
47 public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir,
48 final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
50 List<File> result = new ArrayList<>();
52 // Create Base Directory
54 if (outputBaseDir == null) {
55 outputDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
57 outputDir = outputBaseDir;
61 // Create Resources directory
62 File resourcesDir = new File(outputDir, "resources");
63 resourcesDir.mkdirs();
66 File resourcesJsFile = new File(outputDir, "resources.js");
67 resourcesJsFile.createNewFile();
68 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(resourcesJsFile));
69 ObjectMapper mapper = new ObjectMapper();
70 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
72 // Write resource listing to JS file
73 ResourceList resourceList = super.getResourceListing(null, context, "");
74 String resourceListJson = mapper.writeValueAsString(resourceList);
75 resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
76 bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
78 // Write resources/APIs to JS file and to disk
79 bufferedWriter.write("function jsonFor(resource) {\n\tswitch(resource) {\n");
80 for (Resource resource : resourceList.getApis()) {
81 int revisionIndex = resource.getPath().indexOf('(');
82 String name = resource.getPath().substring(0, revisionIndex);
83 String revision = resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
84 ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "");
85 String json = mapper.writeValueAsString(apiDeclaration);
86 // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
88 "\"models\":{}", "\"models\":" + apiDeclaration.getModels().toString().replace("\\\"", "\""));
89 // Escape single quotes and new lines
90 json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
91 bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
93 File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
94 BufferedWriter resourceFileWriter = new BufferedWriter(new FileWriter(resourceFile));
95 resourceFileWriter.write(json);
96 resourceFileWriter.close();
97 result.add(resourceFile);
99 bufferedWriter.write("\t}\n\treturn \"\";\n}");
100 bufferedWriter.close();
102 result.add(resourcesJsFile);
107 protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
108 if (uriInfo == null) {
109 return name + "(" + revision + ")";
111 return super.generatePath(uriInfo, name, revision);
115 protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
116 if (uriInfo == null) {
117 return RESTCONF_CONTEXT_ROOT;
119 return super.createBasePathFromUriInfo(uriInfo);
123 public void setAdditionalConfig(final Map<String, String> additionalConfig) {
124 this.additionalConfig = additionalConfig;
128 public void setResourceBaseDir(final File resourceBaseDir) {
129 this.resourceBaseDir = resourceBaseDir;
133 public void setMavenProject(final MavenProject mavenProject) {
134 this.mavenProject = mavenProject;
135 this.projectBaseDir = mavenProject.getBasedir();