a1f49d31b5b19cb33f19ecce33b57181ef3e7aa8
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.BufferedWriter;
14 import java.io.File;
15 import java.io.FileWriter;
16 import java.io.IOException;
17 import java.nio.charset.StandardCharsets;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.Set;
24 import javax.ws.rs.core.UriInfo;
25 import org.apache.maven.project.MavenProject;
26 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
27 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
28 import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangSwaggerGeneratorDraft02;
29 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
30 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
31 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
35 import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
36 import org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * This class gathers all yang defined {@link Module}s and generates Swagger compliant documentation.
42  */
43 public class StaticDocGenerator extends BaseYangSwaggerGeneratorDraft02
44         implements BasicCodeGenerator, MavenProjectAware {
45     private static final Logger LOG = LoggerFactory.getLogger(StaticDocGenerator.class);
46
47     private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
48         + File.separator + "swagger-api-documentation";
49
50     public StaticDocGenerator() {
51         super(Optional.empty());
52     }
53
54     @Override
55     @SuppressFBWarnings("DM_DEFAULT_ENCODING")
56     public Collection<File> generateSources(final EffectiveModelContext context, final File outputBaseDir,
57             final Set<Module> currentModules, final ModuleResourceResolver moduleResourcePathResolver)
58                     throws IOException {
59         final List<File> result = new ArrayList<>();
60
61         // Create Base Directory
62         final File outputDir;
63         if (outputBaseDir == null) {
64             outputDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
65         } else {
66             outputDir = outputBaseDir;
67         }
68
69         if (!outputDir.mkdirs()) {
70             throw new IOException("Could not create directory " + outputDir);
71         }
72
73         // Create Resources directory
74         final File resourcesDir = new File(outputDir, "resources");
75         if (!resourcesDir.mkdirs()) {
76             throw new IOException("Could not create directory " + resourcesDir);
77         }
78
79         // Create JS file
80         final File resourcesJsFile = new File(outputDir, "resources.js");
81         if (!resourcesJsFile.createNewFile()) {
82             LOG.info("File {} already exists.", resourcesJsFile);
83         }
84
85         try (BufferedWriter bufferedWriter =
86                 new BufferedWriter(new FileWriter(resourcesJsFile, StandardCharsets.UTF_8))) {
87             final ObjectMapper mapper = new ObjectMapper();
88             mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
89
90             // Write resource listing to JS file
91             final ResourceList resourceList = super.getResourceListing(null, context, "", URIType.DRAFT02,
92                     ApiDocServiceImpl.OAversion.V2_0);
93             String resourceListJson = mapper.writeValueAsString(resourceList);
94             resourceListJson = resourceListJson.replace("\'", "\\\'").replace("\\n", "\\\\n");
95             bufferedWriter.write("function getSpec() {\n\treturn \'" + resourceListJson + "\';\n}\n\n");
96
97             // Write resources/APIs to JS file and to disk
98             bufferedWriter.write("function jsonFor(resource) {\n\tswitch(resource) {\n");
99             for (final Resource resource : resourceList.getApis()) {
100                 final int revisionIndex = resource.getPath().indexOf('(');
101                 final String name = resource.getPath().substring(0, revisionIndex);
102                 final String revision =
103                         resource.getPath().substring(revisionIndex + 1, resource.getPath().length() - 1);
104                 final SwaggerObject swaggerObject = super.getApiDeclaration(name, revision, null, context, "",
105                     URIType.DRAFT02, ApiDocServiceImpl.OAversion.V2_0);
106                 String json = mapper.writeValueAsString(swaggerObject);
107                 // Manually insert models because org.json.JSONObject cannot be serialized by ObjectMapper
108                 json = json.replace(
109                         "\"models\":{}", "\"models\":"
110                                 + swaggerObject.getDefinitions().toString().replace("\\\"", "\""));
111                 // Escape single quotes and new lines
112                 json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
113                 bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
114
115                 final File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
116
117                 try (BufferedWriter resourceFileWriter =
118                         new BufferedWriter(new FileWriter(resourceFile, StandardCharsets.UTF_8))) {
119                     resourceFileWriter.write(json);
120                 }
121
122                 result.add(resourceFile);
123             }
124             bufferedWriter.write("\t}\n\treturn \"\";\n}");
125         }
126
127         result.add(resourcesJsFile);
128         return result;
129     }
130
131     @Override
132     public String generatePath(final UriInfo uriInfo, final String name, final String revision) {
133         if (uriInfo == null) {
134             return name + "(" + revision + ")";
135         }
136         return super.generatePath(uriInfo, name, revision);
137     }
138
139     @Override
140     public void setAdditionalConfig(final Map<String, String> additionalConfig) {
141     }
142
143     @Override
144     public void setResourceBaseDir(final File resourceBaseDir) {
145     }
146
147     @Override
148     public void setMavenProject(final MavenProject mavenProject) {
149     }
150 }