Bug 1500 - Null pointer exception when using mounted resources tab to
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / main / java / org / opendaylight / controller / sal / rest / doc / impl / BaseYangSwaggerGenerator.java
index 5d0f3612e4f3c931c39199a0f180461b8430e0f8..c86b89c004b645aa42e31b86320077278ff23551 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.controller.sal.rest.doc.impl;
 
 import static org.opendaylight.controller.sal.rest.doc.util.RestDocgenUtil.resolvePathArgumentsName;
-
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
@@ -20,10 +19,12 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Set;
 import java.util.SortedSet;
@@ -52,7 +53,7 @@ import org.slf4j.LoggerFactory;
 
 public class BaseYangSwaggerGenerator {
 
-    private static Logger _logger = LoggerFactory.getLogger(BaseYangSwaggerGenerator.class);
+    private static final Logger LOG = LoggerFactory.getLogger(BaseYangSwaggerGenerator.class);
 
     protected static final String API_VERSION = "1.0.0";
     protected static final String SWAGGER_VERSION = "1.2";
@@ -84,19 +85,19 @@ public class BaseYangSwaggerGenerator {
 
         List<Resource> resources = new ArrayList<>(modules.size());
 
-        _logger.info("Modules found [{}]", modules.size());
+        LOG.info("Modules found [{}]", modules.size());
 
         for (Module module : modules) {
             String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision());
             Resource resource = new Resource();
-            _logger.debug("Working on [{},{}]...", module.getName(), revisionString);
+            LOG.debug("Working on [{},{}]...", module.getName(), revisionString);
             ApiDeclaration doc = getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context);
 
             if (doc != null) {
                 resource.setPath(generatePath(uriInfo, module.getName(), revisionString));
                 resources.add(resource);
             } else {
-                _logger.debug("Could not generate doc for {},{}", module.getName(), revisionString);
+                LOG.debug("Could not generate doc for {},{}", module.getName(), revisionString);
             }
         }
 
@@ -119,11 +120,25 @@ public class BaseYangSwaggerGenerator {
 
     public ApiDeclaration getApiDeclaration(String module, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) {
         Date rev = null;
+
         try {
-            rev = SIMPLE_DATE_FORMAT.parse(revision);
+            if(revision != null && !revision.equals("0000-00-00")) {
+                rev = SIMPLE_DATE_FORMAT.parse(revision);
+            }
         } catch (ParseException e) {
             throw new IllegalArgumentException(e);
         }
+
+        if(rev != null) {
+            Calendar cal = new GregorianCalendar();
+
+            cal.setTime(rev);
+
+            if(cal.get(Calendar.YEAR) < 1970) {
+                rev = null;
+            }
+        }
+
         Module m = schemaContext.findModuleByName(module, rev);
         Preconditions.checkArgument(m != null, "Could not find module by name,revision: " + module + "," + revision);
 
@@ -158,11 +173,11 @@ public class BaseYangSwaggerGenerator {
         List<Api> apis = new ArrayList<Api>();
 
         Collection<DataSchemaNode> dataSchemaNodes = m.getChildNodes();
-        _logger.debug("child nodes size [{}]", dataSchemaNodes.size());
+        LOG.debug("child nodes size [{}]", dataSchemaNodes.size());
         for (DataSchemaNode node : dataSchemaNodes) {
             if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) {
 
-                _logger.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName());
+                LOG.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName());
 
                 List<Parameter> pathParams = new ArrayList<Parameter>();
                 String resourcePath = getDataStorePath("/config/", context);
@@ -173,15 +188,15 @@ public class BaseYangSwaggerGenerator {
                 resourcePath = getDataStorePath("/operational/", context);
                 addApis(node, apis, resourcePath, pathParams, schemaContext, false);
             }
+        }
 
-            Set<RpcDefinition> rpcs = m.getRpcs();
-            for (RpcDefinition rpcDefinition : rpcs) {
-                String resourcePath = getDataStorePath("/operations/", context);
-                addRpcs(rpcDefinition, apis, resourcePath, schemaContext);
-            }
+        Set<RpcDefinition> rpcs = m.getRpcs();
+        for (RpcDefinition rpcDefinition : rpcs) {
+            String resourcePath = getDataStorePath("/operations/", context);
+            addRpcs(rpcDefinition, apis, resourcePath, schemaContext);
         }
 
-        _logger.debug("Number of APIs found [{}]", apis.size());
+        LOG.debug("Number of APIs found [{}]", apis.size());
 
         if (!apis.isEmpty()) {
             doc.setApis(apis);
@@ -190,8 +205,8 @@ public class BaseYangSwaggerGenerator {
             try {
                 models = jsonConverter.convertToJsonSchema(m, schemaContext);
                 doc.setModels(models);
-                if (_logger.isDebugEnabled()) {
-                    _logger.debug(mapper.writeValueAsString(doc));
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(mapper.writeValueAsString(doc));
                 }
             } catch (IOException | JSONException e) {
                 e.printStackTrace();
@@ -240,7 +255,7 @@ public class BaseYangSwaggerGenerator {
         List<Parameter> pathParams = new ArrayList<Parameter>(parentPathParams);
 
         String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/";
-        _logger.debug("Adding path: [{}]", resourcePath);
+        LOG.debug("Adding path: [{}]", resourcePath);
         api.setPath(resourcePath);
 
         Iterable<DataSchemaNode> childSchemaNodes = Collections.<DataSchemaNode> emptySet();
@@ -373,13 +388,15 @@ public class BaseYangSwaggerGenerator {
 
         SortedSet<Module> sortedModules = new TreeSet<>(new Comparator<Module>() {
             @Override
-            public int compare(Module o1, Module o2) {
-                int result = o1.getName().compareTo(o2.getName());
+            public int compare(Module module1, Module module2) {
+                int result = module1.getName().compareTo(module2.getName());
                 if (result == 0) {
-                    result = o1.getRevision().compareTo(o2.getRevision());
+                    Date module1Revision = module1.getRevision() != null ? module1.getRevision() : new Date(0);
+                    Date module2Revision = module2.getRevision() != null ? module2.getRevision() : new Date(0);
+                    result = module1Revision.compareTo(module2Revision);
                 }
                 if (result == 0) {
-                    result = o1.getNamespace().compareTo(o2.getNamespace());
+                    result = module1.getNamespace().compareTo(module2.getNamespace());
                 }
                 return result;
             }