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