Merge "Remove import-package declaration"
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / BaseYangSwaggerGenerator.java
index 55c62b24a1b38fa22b1a806b61f325340f91d94b..91612c1eac0354fa9bc42d1de953e1eeb243e82b 100644 (file)
@@ -11,13 +11,11 @@ import static org.opendaylight.netconf.sal.rest.doc.util.RestDocgenUtil.resolveP
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
-import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.net.URI;
-import java.text.DateFormat;
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
@@ -30,8 +28,6 @@ import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import javax.ws.rs.core.UriInfo;
-import org.json.JSONException;
-import org.json.JSONObject;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.Delete;
 import org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.Get;
@@ -44,6 +40,7 @@ import org.opendaylight.netconf.sal.rest.doc.swagger.Parameter;
 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -52,6 +49,7 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,7 +63,6 @@ public class BaseYangSwaggerGenerator {
     private static final String RESTCONF_DRAFT = "18";
 
     static final String MODULE_NAME_SUFFIX = "_module";
-    protected static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
     private final ModelGenerator jsonConverter = new ModelGenerator();
 
     // private Map<String, ApiDeclaration> MODULE_DOC_CACHE = new HashMap<>()
@@ -73,7 +70,6 @@ public class BaseYangSwaggerGenerator {
     private static boolean newDraft;
 
     protected BaseYangSwaggerGenerator() {
-        this.mapper.registerModule(new JsonOrgModule());
         this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
     }
 
@@ -92,7 +88,7 @@ public class BaseYangSwaggerGenerator {
         LOG.info("Modules found [{}]", modules.size());
 
         for (final Module module : modules) {
-            final String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision());
+            final String revisionString = module.getQNameModule().getFormattedRevision();
             final Resource resource = new Resource();
             LOG.debug("Working on [{},{}]...", module.getName(), revisionString);
             final ApiDeclaration doc =
@@ -128,8 +124,8 @@ public class BaseYangSwaggerGenerator {
         Date rev = null;
 
         try {
-            if ((revision != null) && !revision.equals("0000-00-00")) {
-                rev = SIMPLE_DATE_FORMAT.parse(revision);
+            if (revision != null && !SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(revision)) {
+                rev = SimpleDateFormatUtil.getRevisionFormat().parse(revision);
             }
         } catch (final ParseException e) {
             throw new IllegalArgumentException(e);
@@ -175,14 +171,14 @@ public class BaseYangSwaggerGenerator {
         return basePath;
     }
 
-    public ApiDeclaration getSwaggerDocSpec(final Module m, final String basePath, final String context,
-            final SchemaContext schemaContext) {
+    public ApiDeclaration getSwaggerDocSpec(final Module module, final String basePath, final String context,
+                                            final SchemaContext schemaContext) {
         final ApiDeclaration doc = createApiDeclaration(basePath);
 
         final List<Api> apis = new ArrayList<>();
         boolean hasAddRootPostLink = false;
 
-        final Collection<DataSchemaNode> dataSchemaNodes = m.getChildNodes();
+        final Collection<DataSchemaNode> dataSchemaNodes = module.getChildNodes();
         LOG.debug("child nodes size [{}]", dataSchemaNodes.size());
         for (final DataSchemaNode node : dataSchemaNodes) {
             if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) {
@@ -205,22 +201,22 @@ public class BaseYangSwaggerGenerator {
                      * only one root post link is added for this module.
                      */
                     if (!hasAddRootPostLink) {
-                        LOG.debug("Has added root post link for module {}", m.getName());
-                        addRootPostLink(m, (DataNodeContainer) node, pathParams, resourcePath, "config", apis);
+                        LOG.debug("Has added root post link for module {}", module.getName());
+                        addRootPostLink(module, (DataNodeContainer) node, pathParams, resourcePath, "config", apis);
 
                         hasAddRootPostLink = true;
                     }
 
-                    addApis(node, apis, resourcePath, pathParams, schemaContext, true, m.getName(), "config");
+                    addApis(node, apis, resourcePath, pathParams, schemaContext, true, module.getName(), "config");
                 }
                 pathParams = new ArrayList<>();
                 resourcePath = getDataStorePath("operational", context);
 
-                addApis(node, apis, resourcePath, pathParams, schemaContext, false, m.getName(), "operational");
+                addApis(node, apis, resourcePath, pathParams, schemaContext, false, module.getName(), "operational");
             }
         }
 
-        final Set<RpcDefinition> rpcs = m.getRpcs();
+        final Set<RpcDefinition> rpcs = module.getRpcs();
         for (final RpcDefinition rpcDefinition : rpcs) {
             final String resourcePath;
             resourcePath = getDataStorePath("operations", context);
@@ -232,15 +228,15 @@ public class BaseYangSwaggerGenerator {
 
         if (!apis.isEmpty()) {
             doc.setApis(apis);
-            JSONObject models = null;
+            ObjectNode models = null;
 
             try {
-                models = this.jsonConverter.convertToJsonSchema(m, schemaContext);
+                models = this.jsonConverter.convertToJsonSchema(module, schemaContext);
                 doc.setModels(models);
                 if (LOG.isDebugEnabled()) {
                     LOG.debug(this.mapper.writeValueAsString(doc));
                 }
-            } catch (IOException | JSONException e) {
+            } catch (IOException e) {
                 LOG.error("Exception occured in ModelGenerator", e);
             }
 
@@ -249,8 +245,8 @@ public class BaseYangSwaggerGenerator {
         return null;
     }
 
-    private void addRootPostLink(final Module module, final DataNodeContainer node, final List<Parameter> pathParams,
-            final String resourcePath, final String dataStore, final List<Api> apis) {
+    private static void addRootPostLink(final Module module, final DataNodeContainer node,
+            final List<Parameter> pathParams, final String resourcePath, final String dataStore, final List<Api> apis) {
         if (containsListOrContainer(module.getChildNodes())) {
             final Api apiForRootPostUri = new Api();
             apiForRootPostUri.setPath(resourcePath.concat(getContent(dataStore)));
@@ -273,15 +269,14 @@ public class BaseYangSwaggerGenerator {
         if (newDraft) {
             if ("config".contains(dataStore) || "operational".contains(dataStore)) {
                 return "/" + RESTCONF_DRAFT + "/data" + context;
-            } else {
-                return "/" + RESTCONF_DRAFT + "/operations" + context;
             }
-        } else {
-            return "/" + dataStore + context;
+            return "/" + RESTCONF_DRAFT + "/operations" + context;
         }
+
+        return "/" + dataStore + context;
     }
 
-    private String generateCacheKey(final String module, final String revision) {
+    private static String generateCacheKey(final String module, final String revision) {
         return module + "(" + revision + ")";
     }
 
@@ -295,7 +290,7 @@ public class BaseYangSwaggerGenerator {
         LOG.debug("Adding path: [{}]", resourcePath);
         api.setPath(resourcePath.concat(getContent(dataStore)));
 
-        Iterable<DataSchemaNode> childSchemaNodes = Collections.<DataSchemaNode> emptySet();
+        Iterable<DataSchemaNode> childSchemaNodes = Collections.<DataSchemaNode>emptySet();
         if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) {
             final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
             childSchemaNodes = dataNodeContainer.getChildNodes();
@@ -329,16 +324,16 @@ public class BaseYangSwaggerGenerator {
         }
     }
 
-    private boolean containsListOrContainer(final Iterable<DataSchemaNode> nodes) {
+    private static boolean containsListOrContainer(final Iterable<DataSchemaNode> nodes) {
         for (final DataSchemaNode child : nodes) {
-            if ((child instanceof ListSchemaNode) || (child instanceof ContainerSchemaNode)) {
+            if (child instanceof ListSchemaNode || child instanceof ContainerSchemaNode) {
                 return true;
             }
         }
         return false;
     }
 
-    private List<Operation> operation(final DataSchemaNode node, final List<Parameter> pathParams,
+    private static List<Operation> operation(final DataSchemaNode node, final List<Parameter> pathParams,
             final boolean isConfig, final Iterable<DataSchemaNode> childSchemaNodes, final String parentName) {
         final List<Operation> operations = new ArrayList<>();
 
@@ -360,7 +355,7 @@ public class BaseYangSwaggerGenerator {
         return operations;
     }
 
-    private List<Operation> operationPost(final String name, final String description,
+    private static List<Operation> operationPost(final String name, final String description,
             final DataNodeContainer dataNodeContainer, final List<Parameter> pathParams, final boolean isConfig,
             final String parentName) {
         final List<Operation> operations = new ArrayList<>();
@@ -371,7 +366,7 @@ public class BaseYangSwaggerGenerator {
         return operations;
     }
 
-    private String createPath(final DataSchemaNode schemaNode, final List<Parameter> pathParams,
+    private static String createPath(final DataSchemaNode schemaNode, final List<Parameter> pathParams,
             final SchemaContext schemaContext) {
         final ArrayList<LeafSchemaNode> pathListParams = new ArrayList<>();
         final StringBuilder path = new StringBuilder();
@@ -421,10 +416,10 @@ public class BaseYangSwaggerGenerator {
         operationSpec.setMethod("POST");
         operationSpec.setNotes(rpcDefn.getDescription());
         operationSpec.setNickname(rpcDefn.getQName().getLocalName());
-        if (rpcDefn.getOutput() != null) {
+        if (!rpcDefn.getOutput().getChildNodes().isEmpty()) {
             operationSpec.setType("(" + rpcDefn.getQName().getLocalName() + ")output" + OperationBuilder.TOP);
         }
-        if (rpcDefn.getInput() != null) {
+        if (!rpcDefn.getInput().getChildNodes().isEmpty()) {
             final Parameter payload = new Parameter();
             payload.setParamType("body");
             payload.setType("(" + rpcDefn.getQName().getLocalName() + ")input" + OperationBuilder.TOP);
@@ -465,6 +460,6 @@ public class BaseYangSwaggerGenerator {
     }
 
     public void setDraft(final boolean draft) {
-        this.newDraft = draft;
+        BaseYangSwaggerGenerator.newDraft = draft;
     }
 }