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