BUG-2288: DOMNotification API
[controller.git] / opendaylight / md-sal / sal-rest-docgen-maven / src / main / java / org / opendaylight / controller / 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.controller.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.plugin.logging.Log;
23 import org.apache.maven.project.MavenProject;
24 import org.opendaylight.controller.sal.rest.doc.impl.ApiDocGenerator;
25 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
26 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
27 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
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 CodeGenerator {
38
39     private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
40         + File.separator + "swagger-api-documentation";
41
42     private static final Logger _logger = LoggerFactory.getLogger(ApiDocGenerator.class);
43
44     private MavenProject mavenProject;
45     private File projectBaseDir;
46     private Map<String, String> additionalConfig;
47     private File resourceBaseDir;
48
49     /**
50      *
51      * @param context
52      * @param outputDir
53      * @param yangModules
54      * @return
55      * @throws IOException
56      */
57     @Override
58     public Collection<File> generateSources(final SchemaContext context, final File outputDir, final Set<Module> yangModules) throws IOException {
59         List<File> result = new ArrayList<>();
60
61         // Create Base Directory
62         final File outputBaseDir;
63         if (outputDir == null) {
64             outputBaseDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
65         } else {
66             outputBaseDir = outputDir;
67         }
68         outputBaseDir.mkdirs();
69
70         // Create Resources directory
71         File resourcesDir = new File(outputBaseDir, "resources");
72         resourcesDir.mkdirs();
73
74         // Create JS file
75         File resourcesJsFile = new File(outputBaseDir, "resources.js");
76         resourcesJsFile.createNewFile();
77         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(resourcesJsFile));
78         ObjectMapper mapper = new ObjectMapper();
79         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
80
81         // Write resource listing to JS file
82         ResourceList resourceList = super.getResourceListing(null, context, "");
83         String resourceListJson = mapper.writeValueAsString(resourceList);
84         resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
85         bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
86
87         // Write resources/APIs to JS file and to disk
88         bufferedWriter.write("function jsonFor(resource) {\n\tswitch(resource) {\n");
89         for (Resource resource : resourceList.getApis()) {
90             int revisionIndex = resource.getPath().indexOf('(');
91             String name = resource.getPath().substring(0, revisionIndex);
92             String revision = resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
93             ApiDeclaration apiDeclaration = super.getApiDeclaration(name, revision, null, context, "");
94             String json = mapper.writeValueAsString(apiDeclaration);
95             // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
96             json = json.replace("\"models\":{}", "\"models\":" + apiDeclaration.getModels().toString().replace("\\\"", "\""));
97             // Escape single quotes and new lines
98             json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
99             bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
100
101             File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
102             BufferedWriter resourceFileWriter = new BufferedWriter(new FileWriter(resourceFile));
103             resourceFileWriter.write(json);
104             resourceFileWriter.close();
105             result.add(resourceFile);
106         }
107         bufferedWriter.write("\t}\n\treturn \"\";\n}");
108         bufferedWriter.close();
109
110         result.add(resourcesJsFile);
111         return result;
112     }
113
114     @Override
115     protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
116         if (uriInfo == null) {
117             return name + "(" + revision + ")";
118         }
119         return super.generatePath(uriInfo, name, revision);
120     }
121
122     @Override
123     protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
124         if (uriInfo == null) {
125             return RESTCONF_CONTEXT_ROOT;
126         }
127         return super.createBasePathFromUriInfo(uriInfo);
128     }
129
130     @Override
131     public void setLog(final Log log) {
132     }
133
134     @Override
135     public void setAdditionalConfig(final Map<String, String> additionalConfig) {
136         this.additionalConfig = additionalConfig;
137     }
138
139     @Override
140     public void setResourceBaseDir(final File resourceBaseDir) {
141         this.resourceBaseDir = resourceBaseDir;
142     }
143
144     @Override
145     public void setMavenProject(final MavenProject mavenProject) {
146         this.mavenProject = mavenProject;
147         this.projectBaseDir = mavenProject.getBasedir();
148     }
149 }