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