Deterministic startup for resourceSourceMappings
[ccsdk/apps.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / apps / controllerblueprints / service / DataBaseInitService.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.service;\r
19 \r
20 import com.google.common.base.Preconditions;\r
21 import org.apache.commons.collections.CollectionUtils;\r
22 import org.apache.commons.io.IOUtils;\r
23 import org.apache.commons.lang3.StringUtils;\r
24 import org.apache.commons.lang3.text.StrBuilder;\r
25 import org.jetbrains.annotations.NotNull;\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactType;\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType;\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType;\r
31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;\r
32 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition;\r
33 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;\r
34 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;\r
35 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary;\r
36 import org.onap.ccsdk.apps.controllerblueprints.service.utils.ConfigModelUtils;\r
37 import com.att.eelf.configuration.EELFLogger;\r
38 import com.att.eelf.configuration.EELFManager;\r
39 import org.springframework.beans.factory.annotation.Autowired;\r
40 import org.springframework.beans.factory.annotation.Value;\r
41 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;\r
42 import org.springframework.context.annotation.DependsOn;\r
43 import org.springframework.core.io.Resource;\r
44 import org.springframework.core.io.support.ResourcePatternResolver;\r
45 import org.springframework.stereotype.Component;\r
46 \r
47 import javax.annotation.PostConstruct;\r
48 import java.io.IOException;\r
49 import java.nio.charset.Charset;\r
50 import java.util.List;\r
51 \r
52 /**\r
53  * DataBaseInitService.java Purpose: Provide DataBaseInitService Service\r
54  *\r
55  * @author Brinda Santh\r
56  * @version 1.0\r
57  */\r
58 \r
59 @Component\r
60 @DependsOn(value = "ApplicationRegistrationService")\r
61 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")\r
62 public class DataBaseInitService {\r
63 \r
64     private static EELFLogger log = EELFManager.getInstance().getLogger(DataBaseInitService.class);\r
65     private ModelTypeService modelTypeService;\r
66     private ResourceDictionaryService resourceDictionaryService;\r
67     private ConfigModelService configModelService;\r
68 \r
69     @Value("${load.dataTypePath}")\r
70     private String dataTypePath;\r
71     @Value("${load.nodeTypePath}")\r
72     private String nodeTypePath;\r
73     @Value("${load.artifactTypePath}")\r
74     private String artifactTypePath;\r
75     @Value("${load.resourceDictionaryPath}")\r
76     private String resourceDictionaryPath;\r
77     @Value("${load.blueprintsPath}")\r
78     private String bluePrintsPath;\r
79 \r
80     @Autowired\r
81     private ResourcePatternResolver resourceLoader;\r
82 \r
83     /**\r
84      * This is a DataBaseInitService, used to load the initial data\r
85      *\r
86      * @param modelTypeService modelTypeService\r
87      * @param resourceDictionaryService resourceDictionaryService\r
88      * @param configModelService configModelService\r
89      */\r
90     public DataBaseInitService(ModelTypeService modelTypeService, ResourceDictionaryService resourceDictionaryService,\r
91         ConfigModelService configModelService) {\r
92         this.modelTypeService = modelTypeService;\r
93         this.resourceDictionaryService = resourceDictionaryService;\r
94         this.configModelService = configModelService;\r
95         log.info("DataBaseInitService started...");\r
96 \r
97     }\r
98 \r
99     @PostConstruct\r
100     @SuppressWarnings("unused")\r
101     private void initDatabase() {\r
102         log.info("loading dataTypePath from DIR : {}", dataTypePath);\r
103         log.info("loading nodeTypePath from DIR : {}", nodeTypePath);\r
104         log.info("loading artifactTypePath from DIR : {}", artifactTypePath);\r
105         log.info("loading resourceDictionaryPath from DIR : {}", resourceDictionaryPath);\r
106         log.info("loading bluePrintsPath from DIR : {}", bluePrintsPath);\r
107 \r
108         loadModelType();\r
109         loadResourceDictionary();\r
110         loadBlueprints();\r
111     }\r
112 \r
113     private void loadModelType() {\r
114         log.info(" *************************** loadModelType **********************");\r
115         try {\r
116             Resource[] dataTypefiles = getPathResources(dataTypePath, ".json");\r
117             StrBuilder errorBuilder = new StrBuilder();\r
118             for (Resource file : dataTypefiles) {\r
119                 if (file != null) {\r
120                     loadDataType(file, errorBuilder);\r
121                 }\r
122             }\r
123 \r
124             Resource[] nodeTypefiles = getPathResources(nodeTypePath, ".json");\r
125             for (Resource file : nodeTypefiles) {\r
126                 if (file != null) {\r
127                     loadNodeType(file, errorBuilder);\r
128                 }\r
129             }\r
130 \r
131             Resource[] artifactTypefiles = getPathResources(artifactTypePath, ".json");\r
132 \r
133             for (Resource file : artifactTypefiles) {\r
134                 if (file != null) {\r
135                     loadArtifactType(file, errorBuilder);\r
136                 }\r
137             }\r
138 \r
139             if (!errorBuilder.isEmpty()) {\r
140                 log.error(errorBuilder.toString());\r
141             }\r
142         } catch (Exception e) {\r
143             log.error("Failed in Data type loading", e);\r
144         }\r
145     }\r
146 \r
147     private void loadResourceDictionary() {\r
148         log.info(\r
149             " *************************** loadResourceDictionary **********************");\r
150         try {\r
151             Resource[] dataTypefiles = getPathResources(resourceDictionaryPath, ".json");\r
152 \r
153             StrBuilder errorBuilder = new StrBuilder();\r
154             String fileName;\r
155             for (Resource file : dataTypefiles) {\r
156                 try {\r
157                     fileName = file.getFilename();\r
158                     log.trace("Loading : {}", fileName);\r
159                     String definitionContent = getResourceContent(file);\r
160                     ResourceDefinition resourceDefinition =\r
161                         JacksonUtils.readValue(definitionContent, ResourceDefinition.class);\r
162                     if (resourceDefinition != null) {\r
163                         Preconditions\r
164                             .checkNotNull(resourceDefinition.getProperty(), "Failed to get Property Definition");\r
165                         ResourceDictionary resourceDictionary = new ResourceDictionary();\r
166                         resourceDictionary.setName(resourceDefinition.getName());\r
167                         resourceDictionary.setDefinition(resourceDefinition);\r
168 \r
169                         Preconditions.checkNotNull(resourceDefinition.getProperty(), "Property field is missing");\r
170                         resourceDictionary.setDescription(resourceDefinition.getProperty().getDescription());\r
171                         resourceDictionary.setDataType(resourceDefinition.getProperty().getType());\r
172                         if (resourceDefinition.getProperty().getEntrySchema() != null) {\r
173                             resourceDictionary\r
174                                 .setEntrySchema(resourceDefinition.getProperty().getEntrySchema().getType());\r
175                         }\r
176                         resourceDictionary.setUpdatedBy(resourceDefinition.getUpdatedBy());\r
177                         if (StringUtils.isBlank(resourceDefinition.getTags())) {\r
178                             resourceDictionary.setTags(\r
179                                 resourceDefinition.getName() + ", " + resourceDefinition.getUpdatedBy()\r
180                                     + ", " + resourceDefinition.getUpdatedBy());\r
181 \r
182                         } else {\r
183                             resourceDictionary.setTags(resourceDefinition.getTags());\r
184                         }\r
185                         resourceDictionaryService.saveResourceDictionary(resourceDictionary);\r
186 \r
187                         log.trace(" Loaded successfully : {}", file.getFilename());\r
188                     } else {\r
189                         throw new BluePrintException("couldn't get dictionary from content information");\r
190                     }\r
191                 } catch (Exception e) {\r
192                     errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());\r
193                 }\r
194             }\r
195             if (!errorBuilder.isEmpty()) {\r
196                 log.error(errorBuilder.toString());\r
197             }\r
198 \r
199 \r
200         } catch (Exception e) {\r
201             log.error(\r
202                 "Failed in Resource dictionary loading", e);\r
203         }\r
204     }\r
205 \r
206     private void loadBlueprints() {\r
207         log.info("*************************** loadServiceTemplate **********************");\r
208         try {\r
209             List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(bluePrintsPath);\r
210             if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {\r
211                 StrBuilder errorBuilder = new StrBuilder();\r
212                 for (String fileName : serviceTemplateDirs) {\r
213                     try {\r
214                         String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);\r
215                         log.debug("***** Loading service template :  {}", bluePrintPath);\r
216                         ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);\r
217 \r
218                         configModel = this.configModelService.saveConfigModel(configModel);\r
219 \r
220                         log.info("Publishing : {}", configModel.getId());\r
221 \r
222                         this.configModelService.publishConfigModel(configModel.getId());\r
223 \r
224                         log.info("Loaded service template successfully: {}", fileName);\r
225 \r
226                     } catch (Exception e) {\r
227                         errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());\r
228                     }\r
229                 }\r
230 \r
231                 if (!errorBuilder.isEmpty()) {\r
232                     log.error(errorBuilder.toString());\r
233                 }\r
234             }\r
235         } catch (Exception e) {\r
236             log.error("Failed in Service Template loading", e);\r
237         }\r
238     }\r
239 \r
240     private void loadNodeType(Resource file, StrBuilder errorBuilder) {\r
241         try {\r
242             log.trace("Loading Node Type : {}", file.getFilename());\r
243             String nodeKey = file.getFilename().replace(".json", "");\r
244             String definitionContent = getResourceContent(file);\r
245             NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);\r
246             Preconditions\r
247                 .checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));\r
248             ModelType modelType = new ModelType();\r
249             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);\r
250             modelType.setDerivedFrom(nodeType.getDerivedFrom());\r
251             modelType.setDescription(nodeType.getDescription());\r
252             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
253             modelType.setModelName(nodeKey);\r
254             modelType.setVersion(nodeType.getVersion());\r
255             modelType.setUpdatedBy("System");\r
256             modelType.setTags(nodeKey + "," + BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE + ","\r
257                 + nodeType.getDerivedFrom());\r
258             modelTypeService.saveModel(modelType);\r
259             log.trace("Loaded Node Type successfully : {}", file.getFilename());\r
260         } catch (Exception e) {\r
261             errorBuilder.appendln("Node type loading error : " + file.getFilename() + ":" + e.getMessage());\r
262         }\r
263     }\r
264 \r
265     private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {\r
266         try {\r
267             log.trace("Loading Data Type: {}", file.getFilename());\r
268             String dataKey = file.getFilename().replace(".json", "");\r
269             String definitionContent = getResourceContent(file);\r
270             DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);\r
271             Preconditions\r
272                 .checkNotNull(dataType, String.format("failed to get data type from file : %s", file.getFilename()));\r
273             ModelType modelType = new ModelType();\r
274             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
275             modelType.setDerivedFrom(dataType.getDerivedFrom());\r
276             modelType.setDescription(dataType.getDescription());\r
277             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
278             modelType.setModelName(dataKey);\r
279             modelType.setVersion(dataType.getVersion());\r
280             modelType.setUpdatedBy("System");\r
281             modelType.setTags(dataKey + "," + dataType.getDerivedFrom() + ","\r
282                 + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
283             modelTypeService.saveModel(modelType);\r
284             log.trace(" Loaded Data Type successfully : {}", file.getFilename());\r
285         } catch (Exception e) {\r
286             errorBuilder.appendln("Data type loading error : " + file.getFilename() + ":" + e.getMessage());\r
287         }\r
288     }\r
289 \r
290     private void loadArtifactType(Resource file, StrBuilder errorBuilder) {\r
291         try {\r
292             log.trace("Loading Artifact Type: {}", file.getFilename());\r
293             String dataKey = file.getFilename().replace(".json", "");\r
294             String definitionContent = getResourceContent(file);\r
295             ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);\r
296             Preconditions.checkNotNull(artifactType,\r
297                 String.format("failed to get artifact type from file : %s", file.getFilename()));\r
298             ModelType modelType = new ModelType();\r
299             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
300             modelType.setDerivedFrom(artifactType.getDerivedFrom());\r
301             modelType.setDescription(artifactType.getDescription());\r
302             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
303             modelType.setModelName(dataKey);\r
304             modelType.setVersion(artifactType.getVersion());\r
305             modelType.setUpdatedBy("System");\r
306             modelType.setTags(dataKey + "," + artifactType.getDerivedFrom() + ","\r
307                 + BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
308             modelTypeService.saveModel(modelType);\r
309             log.trace("Loaded Artifact Type successfully : {}", file.getFilename());\r
310         } catch (Exception e) {\r
311             errorBuilder.appendln("Artifact type loading error : " + file.getFilename() + ":" + e.getMessage());\r
312         }\r
313     }\r
314 \r
315     private Resource[] getPathResources(String path, String extension) throws IOException {\r
316         return resourceLoader.getResources("file:" + path + "/*" + extension);\r
317     }\r
318 \r
319     private String getResourceContent(Resource resource) throws IOException {\r
320         return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());\r
321     }\r
322 \r
323 }\r