Fix findbugs violations in sal-rest-docgen-maven
[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.ApiDocGenerator;
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 ApiDocGenerator implements BasicCodeGenerator, MavenProjectAware {
41     private static final Logger LOG = LoggerFactory.getLogger(StaticDocGenerator.class);
42
43     private static final String DEFAULT_OUTPUT_BASE_DIR_PATH = "target" + File.separator + "generated-resources"
44         + File.separator + "swagger-api-documentation";
45
46     @Override
47     @SuppressFBWarnings("DM_DEFAULT_ENCODING")
48     public Collection<File> generateSources(final SchemaContext context, final File outputBaseDir,
49             final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
50                     throws IOException {
51         List<File> result = new ArrayList<>();
52
53         // Create Base Directory
54         final File outputDir;
55         if (outputBaseDir == null) {
56             outputDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH);
57         } else {
58             outputDir = outputBaseDir;
59         }
60
61         if (!outputDir.mkdirs()) {
62             throw new IOException("Could not create directory " + outputDir);
63         }
64
65         // Create Resources directory
66         File resourcesDir = new File(outputDir, "resources");
67         if (!resourcesDir.mkdirs()) {
68             throw new IOException("Could not create directory " + resourcesDir);
69         }
70
71         // Create JS file
72         File resourcesJsFile = new File(outputDir, "resources.js");
73         if (!resourcesJsFile.createNewFile()) {
74             LOG.info("File " + resourcesJsFile + " already exists.");
75         }
76
77         try (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(
97                         "\"models\":{}", "\"models\":" + apiDeclaration.getModels().toString().replace("\\\"", "\""));
98                 // Escape single quotes and new lines
99                 json = json.replace("\'", "\\\'").replace("\\n", "\\\\n");
100                 bufferedWriter.write("\t\tcase \"" + name + "(" + revision + ")\": return \'" + json + "\';\n");
101
102                 File resourceFile = new File(resourcesDir, name + "(" + revision + ").json");
103
104                 try (BufferedWriter resourceFileWriter = new BufferedWriter(new FileWriter(resourceFile))) {
105                     resourceFileWriter.write(json);
106                 }
107
108                 result.add(resourceFile);
109             }
110             bufferedWriter.write("\t}\n\treturn \"\";\n}");
111         }
112
113         result.add(resourcesJsFile);
114         return result;
115     }
116
117     @Override
118     protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
119         if (uriInfo == null) {
120             return name + "(" + revision + ")";
121         }
122         return super.generatePath(uriInfo, name, revision);
123     }
124
125     @Override
126     protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
127         if (uriInfo == null) {
128             return RESTCONF_CONTEXT_ROOT;
129         }
130         return super.createBasePathFromUriInfo(uriInfo);
131     }
132
133     @Override
134     public void setAdditionalConfig(final Map<String, String> additionalConfig) {
135     }
136
137     @Override
138     public void setResourceBaseDir(final File resourceBaseDir) {
139     }
140
141     @Override
142     public void setMavenProject(final MavenProject mavenProject) {
143     }
144 }