Controller Blueprints Microservice
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / apps / controllerblueprints / service / DataBaseInitService.java
index 4b732cc..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
@@ -159,30 +157,28 @@ public class DataBaseInitService {
                         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
@@ -250,7 +246,7 @@ public class DataBaseInitService {
             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
@@ -263,7 +259,7 @@ 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
@@ -274,7 +270,7 @@ public class DataBaseInitService {
             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
@@ -298,7 +294,7 @@ public class DataBaseInitService {
             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