Controller Blueprints Microservice
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / apps / controllerblueprints / service / DataBaseInitService.java
index 3a5c4fd..92fe8de 100644 (file)
@@ -22,6 +22,7 @@ import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.IOUtils;\r
 import org.apache.commons.lang3.StringUtils;\r
 import org.apache.commons.lang3.text.StrBuilder;\r
+import org.jetbrains.annotations.NotNull;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactType;\r
@@ -33,16 +34,16 @@ import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;
 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;\r
 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary;\r
 import org.onap.ccsdk.apps.controllerblueprints.service.utils.ConfigModelUtils;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
 import org.springframework.beans.factory.annotation.Value;\r
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;\r
+import org.springframework.boot.context.event.ApplicationReadyEvent;\r
+import org.springframework.context.event.EventListener;\r
 import org.springframework.core.io.Resource;\r
 import org.springframework.core.io.support.ResourcePatternResolver;\r
 import org.springframework.stereotype.Component;\r
-\r
-import javax.annotation.PostConstruct;\r
 import java.io.IOException;\r
 import java.nio.charset.Charset;\r
 import java.util.List;\r
@@ -55,20 +56,23 @@ import java.util.List;
  */\r
 \r
 @Component\r
-@ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true", matchIfMissing = false)\r
+@ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")\r
 public class DataBaseInitService {\r
 \r
-    private static Logger log = LoggerFactory.getLogger(DataBaseInitService.class);\r
-    @Value("${blueprints.load.path}")\r
-    private String modelLoadPath;\r
+    private static EELFLogger log = EELFManager.getInstance().getLogger(DataBaseInitService.class);\r
     private ModelTypeService modelTypeService;\r
     private ResourceDictionaryService resourceDictionaryService;\r
     private ConfigModelService configModelService;\r
 \r
+    @Value("${load.dataTypePath}")\r
     private String dataTypePath;\r
+    @Value("${load.nodeTypePath}")\r
     private String nodeTypePath;\r
+    @Value("${load.artifactTypePath}")\r
     private String artifactTypePath;\r
+    @Value("${load.resourceDictionaryPath}")\r
     private String resourceDictionaryPath;\r
+    @Value("${load.blueprintsPath}")\r
     private String bluePrintsPath;\r
 \r
     @Autowired\r
@@ -77,9 +81,9 @@ public class DataBaseInitService {
     /**\r
      * This is a DataBaseInitService, used to load the initial data\r
      *\r
-     * @param modelTypeService\r
-     * @param resourceDictionaryService\r
-     * @param configModelService\r
+     * @param modelTypeService modelTypeService\r
+     * @param resourceDictionaryService resourceDictionaryService\r
+     * @param configModelService configModelService\r
      */\r
     public DataBaseInitService(ModelTypeService modelTypeService, ResourceDictionaryService resourceDictionaryService,\r
                                ConfigModelService configModelService) {\r
@@ -90,15 +94,9 @@ public class DataBaseInitService {
 \r
     }\r
 \r
-    @PostConstruct\r
+    @SuppressWarnings("unused")\r
+    @EventListener(ApplicationReadyEvent.class)\r
     private void initDatabase() {\r
-        log.info("loading Blueprints from DIR : {}", modelLoadPath);\r
-        dataTypePath = modelLoadPath + "/model_type/data_type";\r
-        nodeTypePath = modelLoadPath + "/model_type/node_type";\r
-        artifactTypePath = modelLoadPath + "/model_type/artifact_type";\r
-        resourceDictionaryPath = modelLoadPath + "/resource_dictionary";\r
-        bluePrintsPath = modelLoadPath + "/blueprints";\r
-\r
         log.info("loading dataTypePath from DIR : {}", dataTypePath);\r
         log.info("loading nodeTypePath from DIR : {}", nodeTypePath);\r
         log.info("loading artifactTypePath from DIR : {}", artifactTypePath);\r
@@ -115,31 +113,28 @@ public class DataBaseInitService {
         try {\r
             Resource[] dataTypefiles = getPathResources(dataTypePath, ".json");\r
             StrBuilder errorBuilder = new StrBuilder();\r
-            if (dataTypefiles != null) {\r
                 for (Resource file : dataTypefiles) {\r
                     if (file != null) {\r
                         loadDataType(file, errorBuilder);\r
                     }\r
                 }\r
-            }\r
 \r
             Resource[] nodeTypefiles = getPathResources(nodeTypePath, ".json");\r
-            if (nodeTypefiles != null) {\r
-                for (Resource file : nodeTypefiles) {\r
+                       for (Resource file : nodeTypefiles) {\r
                     if (file != null) {\r
                         loadNodeType(file, errorBuilder);\r
                     }\r
                 }\r
-            }\r
+\r
 \r
             Resource[] artifactTypefiles = getPathResources(artifactTypePath, ".json");\r
-            if (artifactTypefiles != null) {\r
+\r
                 for (Resource file : artifactTypefiles) {\r
                     if (file != null) {\r
                         loadArtifactType(file, errorBuilder);\r
                     }\r
                 }\r
-            }\r
+\r
 \r
             if (!errorBuilder.isEmpty()) {\r
                 log.error(errorBuilder.toString());\r
@@ -154,38 +149,36 @@ public class DataBaseInitService {
                 " *************************** loadResourceDictionary **********************");\r
         try {\r
             Resource[] dataTypefiles = getPathResources(resourceDictionaryPath, ".json");\r
-            if (dataTypefiles != null) {\r
+\r
                 StrBuilder errorBuilder = new StrBuilder();\r
-                String fileName = null;\r
+                String fileName;\r
                 for (Resource file : dataTypefiles) {\r
                     try {\r
                         fileName = file.getFilename();\r
                         log.trace("Loading : {}", fileName);\r
                         String definitionContent = getResourceContent(file);\r
-                        ResourceDefinition dictionaryDefinition =\r
+                        ResourceDefinition resourceDefinition =\r
                                 JacksonUtils.readValue(definitionContent, ResourceDefinition.class);\r
-                        if (dictionaryDefinition != null) {\r
-                            Preconditions.checkNotNull(dictionaryDefinition.getProperty(), "Failed to get Property Definition");\r
+                        if (resourceDefinition != null) {\r
+                            Preconditions.checkNotNull(resourceDefinition.getProperty(), "Failed to get Property Definition");\r
                             ResourceDictionary resourceDictionary = new ResourceDictionary();\r
-                            resourceDictionary.setResourcePath(dictionaryDefinition.getResourcePath());\r
-                            resourceDictionary.setName(dictionaryDefinition.getName());\r
-                            resourceDictionary.setDefinition(definitionContent);\r
-\r
-                            resourceDictionary.setResourceType(dictionaryDefinition.getResourceType());\r
-                            resourceDictionary.setDescription(dictionaryDefinition.getProperty().getDescription());\r
-                            resourceDictionary.setDataType(dictionaryDefinition.getProperty().getType());\r
-                            if(dictionaryDefinition.getProperty().getEntrySchema() != null){\r
-                                resourceDictionary.setEntrySchema(dictionaryDefinition.getProperty().getEntrySchema().getType());\r
+                            resourceDictionary.setName(resourceDefinition.getName());\r
+                            resourceDictionary.setDefinition(resourceDefinition);\r
+\r
+                            Preconditions.checkNotNull(resourceDefinition.getProperty(), "Property field is missing");\r
+                            resourceDictionary.setDescription(resourceDefinition.getProperty().getDescription());\r
+                            resourceDictionary.setDataType(resourceDefinition.getProperty().getType());\r
+                            if(resourceDefinition.getProperty().getEntrySchema() != null){\r
+                                resourceDictionary.setEntrySchema(resourceDefinition.getProperty().getEntrySchema().getType());\r
                             }\r
-                            resourceDictionary.setUpdatedBy(dictionaryDefinition.getUpdatedBy());\r
-                            if (StringUtils.isBlank(dictionaryDefinition.getTags())) {\r
+                            resourceDictionary.setUpdatedBy(resourceDefinition.getUpdatedBy());\r
+                            if (StringUtils.isBlank(resourceDefinition.getTags())) {\r
                                 resourceDictionary.setTags(\r
-                                        dictionaryDefinition.getName() + ", " + dictionaryDefinition.getUpdatedBy()\r
-                                                + ", " + dictionaryDefinition.getResourceType() + ", "\r
-                                                + dictionaryDefinition.getUpdatedBy());\r
+                                        resourceDefinition.getName() + ", " + resourceDefinition.getUpdatedBy()\r
+                                                + ", " + resourceDefinition.getUpdatedBy());\r
 \r
                             } else {\r
-                                resourceDictionary.setTags(dictionaryDefinition.getTags());\r
+                                resourceDictionary.setTags(resourceDefinition.getTags());\r
                             }\r
                             resourceDictionaryService.saveResourceDictionary(resourceDictionary);\r
 \r
@@ -201,7 +194,7 @@ public class DataBaseInitService {
                     log.error(errorBuilder.toString());\r
                 }\r
 \r
-            }\r
+\r
         } catch (Exception e) {\r
             log.error(\r
                     "Failed in Resource dictionary loading", e);\r
@@ -248,11 +241,12 @@ public class DataBaseInitService {
             String nodeKey = file.getFilename().replace(".json", "");\r
             String definitionContent = getResourceContent(file);\r
             NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);\r
+            Preconditions.checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));\r
             ModelType modelType = new ModelType();\r
             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);\r
             modelType.setDerivedFrom(nodeType.getDerivedFrom());\r
             modelType.setDescription(nodeType.getDescription());\r
-            modelType.setDefinition(definitionContent);\r
+            modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
             modelType.setModelName(nodeKey);\r
             modelType.setVersion(nodeType.getVersion());\r
             modelType.setUpdatedBy("System");\r
@@ -265,17 +259,18 @@ public class DataBaseInitService {
         }\r
     }\r
 \r
-    private void loadDataType(Resource file, StrBuilder errorBuilder) {\r
+    private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {\r
         try {\r
             log.trace("Loading Data Type: {}", file.getFilename());\r
             String dataKey = file.getFilename().replace(".json", "");\r
             String definitionContent = getResourceContent(file);\r
             DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);\r
+            Preconditions.checkNotNull(dataType, String.format("failed to get data type from file : %s", file.getFilename()));\r
             ModelType modelType = new ModelType();\r
             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
             modelType.setDerivedFrom(dataType.getDerivedFrom());\r
             modelType.setDescription(dataType.getDescription());\r
-            modelType.setDefinition(definitionContent);\r
+            modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
             modelType.setModelName(dataKey);\r
             modelType.setVersion(dataType.getVersion());\r
             modelType.setUpdatedBy("System");\r
@@ -294,11 +289,12 @@ public class DataBaseInitService {
             String dataKey = file.getFilename().replace(".json", "");\r
             String definitionContent = getResourceContent(file);\r
             ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);\r
+            Preconditions.checkNotNull(artifactType, String.format("failed to get artifact type from file : %s", file.getFilename()));\r
             ModelType modelType = new ModelType();\r
             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
             modelType.setDerivedFrom(artifactType.getDerivedFrom());\r
             modelType.setDescription(artifactType.getDescription());\r
-            modelType.setDefinition(definitionContent);\r
+            modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
             modelType.setModelName(dataKey);\r
             modelType.setVersion(artifactType.getVersion());\r
             modelType.setUpdatedBy("System");\r