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