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;
21 import javax.ws.rs.core.UriInfo;
22 import org.apache.maven.project.MavenProject;
23 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGenerator;
24 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
25 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
26 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
30 import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
33 * This class gathers all yang defined {@link Module}s and generates Swagger compliant documentation.
35 public class StaticDocGenerator extends ApiDocGenerator implements BasicCodeGenerator, MavenProjectAware {
36 private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
37 + File.separator + "swagger-api-documentation";
39 private MavenProject mavenProject;
40 private File projectBaseDir;
41 private Map<String, String> additionalConfig;
42 private File resourceBaseDir;
47 * @param context schema context
48 * @param outputDir output directory
49 * @param yangModules modules
50 * @return {@link Collection} of resource files
51 * @throws IOException when file operation fails
54 public Collection<File> generateSources(final SchemaContext context, final File outputDir,
55 final Set<Module> yangModules) throws IOException {
56 List<File> result = new ArrayList<>();
58 // Create Base Directory
59 final File outputBaseDir;
60 if (outputDir == null) {
61 outputBaseDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
63 outputBaseDir = outputDir;
65 outputBaseDir.mkdirs();
67 // Create Resources directory
68 File resourcesDir = new File(outputBaseDir, "resources");
69 resourcesDir.mkdirs();
72 File resourcesJsFile = new File(outputBaseDir, "resources.js");
73 resourcesJsFile.createNewFile();
74 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(resourcesJsFile));
75 ObjectMapper mapper = new ObjectMapper();
76 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
78 // Write resource listing to JS file
79 ResourceList resourceList = super.getResourceListing(null, context, "");
80 String resourceListJson = mapper.writeValueAsString(resourceList);
81 resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
82 bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
84 // Write resources/APIs to JS file and to disk
85 bufferedWriter.write("function jsonFor(resource) {\n\tswitch(resource) {\n");
86 for (Resource resource : resourceList.getApis()) {
87 int revisionIndex = resource.getPath().indexOf('(');
88 String name = resource.getPath().substring(0, revisionIndex);
89 String revision = resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
90 ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "");
91 String json = mapper.writeValueAsString(apiDeclaration);
92 // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
94 "\"models\":{}", "\"models\":" + apiDeclaration.getModels().toString().replace("\\\"", "\""));
95 // Escape single quotes and new lines
96 json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
97 bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
99 File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
100 BufferedWriter resourceFileWriter = new BufferedWriter(new FileWriter(resourceFile));
101 resourceFileWriter.write(json);
102 resourceFileWriter.close();
103 result.add(resourceFile);
105 bufferedWriter.write("\t}\n\treturn \"\";\n}");
106 bufferedWriter.close();
108 result.add(resourcesJsFile);
113 protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
114 if (uriInfo == null) {
115 return name + "(" + revision + ")";
117 return super.generatePath(uriInfo, name, revision);
121 protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
122 if (uriInfo == null) {
123 return RESTCONF_CONTEXT_ROOT;
125 return super.createBasePathFromUriInfo(uriInfo);
129 public void setAdditionalConfig(final Map<String, String> additionalConfig) {
130 this.additionalConfig = additionalConfig;
134 public void setResourceBaseDir(final File resourceBaseDir) {
135 this.resourceBaseDir = resourceBaseDir;
139 public void setMavenProject(final MavenProject mavenProject) {
140 this.mavenProject = mavenProject;
141 this.projectBaseDir = mavenProject.getBasedir();