Bug 8351: enforce check-style rules for restconf
[netconf.git] / restconf / sal-rest-docgen-maven / src / main / java / org / opendaylight / netconf / sal / rest / doc / maven / StaticDocGenerator.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.rest.doc.maven;
9
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import java.io.BufferedWriter;
13 import java.io.File;
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;
19 import java.util.Map;
20 import java.util.Set;
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;
31
32 /**
33  * This class gathers all yang defined {@link Module}s and generates Swagger compliant documentation.
34  */
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";
38
39     private MavenProject mavenProject;
40     private File projectBaseDir;
41     private Map<String, String> additionalConfig;
42     private File resourceBaseDir;
43
44     /**
45      * Generate sources.
46      *
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
52      */
53     @Override
54     public Collection<File> generateSources(final SchemaContext context, final File outputDir,
55             final Set<Module> yangModules) throws IOException {
56         List<File> result = new ArrayList<>();
57
58         // Create Base Directory
59         final File outputBaseDir;
60         if (outputDir == null) {
61             outputBaseDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
62         } else {
63             outputBaseDir = outputDir;
64         }
65         outputBaseDir.mkdirs();
66
67         // Create Resources directory
68         File resourcesDir = new File(outputBaseDir, "resources");
69         resourcesDir.mkdirs();
70
71         // Create JS file
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);
77
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");
83
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
93             json = json.replace(
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");
98
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);
104         }
105         bufferedWriter.write("\t}\n\treturn \"\";\n}");
106         bufferedWriter.close();
107
108         result.add(resourcesJsFile);
109         return result;
110     }
111
112     @Override
113     protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
114         if (uriInfo == null) {
115             return name + "(" + revision + ")";
116         }
117         return super.generatePath(uriInfo, name, revision);
118     }
119
120     @Override
121     protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
122         if (uriInfo == null) {
123             return RESTCONF_CONTEXT_ROOT;
124         }
125         return super.createBasePathFromUriInfo(uriInfo);
126     }
127
128     @Override
129     public void setAdditionalConfig(final Map<String, String> additionalConfig) {
130         this.additionalConfig = additionalConfig;
131     }
132
133     @Override
134     public void setResourceBaseDir(final File resourceBaseDir) {
135         this.resourceBaseDir = resourceBaseDir;
136     }
137
138     @Override
139     public void setMavenProject(final MavenProject mavenProject) {
140         this.mavenProject = mavenProject;
141         this.projectBaseDir = mavenProject.getBasedir();
142     }
143 }