dbb1c1e1ee7a4c44b3352aecf4ecb533a9068bf7
[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.Optional;
21 import java.util.Set;
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;
33
34 /**
35  * This class gathers all yang defined {@link Module}s and generates Swagger compliant documentation.
36  */
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";
40
41     private MavenProject mavenProject;
42     private File projectBaseDir;
43     private Map<String, String> additionalConfig;
44     private File resourceBaseDir;
45
46     @Override
47     public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir,
48             final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
49                     throws IOException {
50         List<File> result = new ArrayList<>();
51
52         // Create Base Directory
53         final File outputDir;
54         if (outputBaseDir == null) {
55             outputDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
56         } else {
57             outputDir = outputBaseDir;
58         }
59         outputDir.mkdirs();
60
61         // Create Resources directory
62         File resourcesDir = new File(outputDir, "resources");
63         resourcesDir.mkdirs();
64
65         // Create JS file
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);
71
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");
77
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
87             json = json.replace(
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");
92
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);
98         }
99         bufferedWriter.write("\t}\n\treturn \"\";\n}");
100         bufferedWriter.close();
101
102         result.add(resourcesJsFile);
103         return result;
104     }
105
106     @Override
107     protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
108         if (uriInfo == null) {
109             return name + "(" + revision + ")";
110         }
111         return super.generatePath(uriInfo, name, revision);
112     }
113
114     @Override
115     protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
116         if (uriInfo == null) {
117             return RESTCONF_CONTEXT_ROOT;
118         }
119         return super.createBasePathFromUriInfo(uriInfo);
120     }
121
122     @Override
123     public void setAdditionalConfig(final Map<String, String> additionalConfig) {
124         this.additionalConfig = additionalConfig;
125     }
126
127     @Override
128     public void setResourceBaseDir(final File resourceBaseDir) {
129         this.resourceBaseDir = resourceBaseDir;
130     }
131
132     @Override
133     public void setMavenProject(final MavenProject mavenProject) {
134         this.mavenProject = mavenProject;
135         this.projectBaseDir = mavenProject.getBasedir();
136     }
137 }