c6d80cfb63c2115b4cedb18ab5e39aeb27e4a5ce
[ccsdk/cds.git] /
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.core.io.Resource;\r
43 import org.springframework.core.io.support.ResourcePatternResolver;\r
44 import org.springframework.stereotype.Component;\r
45 \r
46 import javax.annotation.PostConstruct;\r
47 import java.io.IOException;\r
48 import java.nio.charset.Charset;\r
49 import java.util.List;\r
50 \r
51 /**\r
52  * DataBaseInitService.java Purpose: Provide DataBaseInitService Service\r
53  *\r
54  * @author Brinda Santh\r
55  * @version 1.0\r
56  */\r
57 \r
58 @Component\r
59 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")\r
60 public class DataBaseInitService {\r
61 \r
62     private static EELFLogger log = EELFManager.getInstance().getLogger(DataBaseInitService.class);\r
63     private ModelTypeService modelTypeService;\r
64     private ResourceDictionaryService resourceDictionaryService;\r
65     private ConfigModelService configModelService;\r
66 \r
67     @Value("${load.dataTypePath}")\r
68     private String dataTypePath;\r
69     @Value("${load.nodeTypePath}")\r
70     private String nodeTypePath;\r
71     @Value("${load.artifactTypePath}")\r
72     private String artifactTypePath;\r
73     @Value("${load.resourceDictionaryPath}")\r
74     private String resourceDictionaryPath;\r
75     @Value("${load.blueprintsPath}")\r
76     private String bluePrintsPath;\r
77 \r
78     @Autowired\r
79     private ResourcePatternResolver resourceLoader;\r
80 \r
81     /**\r
82      * This is a DataBaseInitService, used to load the initial data\r
83      *\r
84      * @param modelTypeService modelTypeService\r
85      * @param resourceDictionaryService resourceDictionaryService\r
86      * @param configModelService configModelService\r
87      */\r
88     public DataBaseInitService(ModelTypeService modelTypeService, ResourceDictionaryService resourceDictionaryService,\r
89                                ConfigModelService configModelService) {\r
90         this.modelTypeService = modelTypeService;\r
91         this.resourceDictionaryService = resourceDictionaryService;\r
92         this.configModelService = configModelService;\r
93         log.info("DataBaseInitService started...");\r
94 \r
95     }\r
96 \r
97     @PostConstruct\r
98     @SuppressWarnings("unused")\r
99     private void initDatabase() {\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                 for (Resource file : dataTypefiles) {\r
117                     if (file != null) {\r
118                         loadDataType(file, errorBuilder);\r
119                     }\r
120                 }\r
121 \r
122             Resource[] nodeTypefiles = getPathResources(nodeTypePath, ".json");\r
123                        for (Resource file : nodeTypefiles) {\r
124                     if (file != null) {\r
125                         loadNodeType(file, errorBuilder);\r
126                     }\r
127                 }\r
128 \r
129 \r
130             Resource[] artifactTypefiles = getPathResources(artifactTypePath, ".json");\r
131 \r
132                 for (Resource file : artifactTypefiles) {\r
133                     if (file != null) {\r
134                         loadArtifactType(file, errorBuilder);\r
135                     }\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 dictionaryDefinition =\r
161                                 JacksonUtils.readValue(definitionContent, ResourceDefinition.class);\r
162                         if (dictionaryDefinition != null) {\r
163                             Preconditions.checkNotNull(dictionaryDefinition.getProperty(), "Failed to get Property Definition");\r
164                             ResourceDictionary resourceDictionary = new ResourceDictionary();\r
165                             resourceDictionary.setResourcePath(dictionaryDefinition.getResourcePath());\r
166                             resourceDictionary.setName(dictionaryDefinition.getName());\r
167                             resourceDictionary.setDefinition(dictionaryDefinition);\r
168 \r
169                             resourceDictionary.setResourceType(dictionaryDefinition.getResourceType());\r
170                             resourceDictionary.setDescription(dictionaryDefinition.getProperty().getDescription());\r
171                             resourceDictionary.setDataType(dictionaryDefinition.getProperty().getType());\r
172                             if(dictionaryDefinition.getProperty().getEntrySchema() != null){\r
173                                 resourceDictionary.setEntrySchema(dictionaryDefinition.getProperty().getEntrySchema().getType());\r
174                             }\r
175                             resourceDictionary.setUpdatedBy(dictionaryDefinition.getUpdatedBy());\r
176                             if (StringUtils.isBlank(dictionaryDefinition.getTags())) {\r
177                                 resourceDictionary.setTags(\r
178                                         dictionaryDefinition.getName() + ", " + dictionaryDefinition.getUpdatedBy()\r
179                                                 + ", " + dictionaryDefinition.getResourceType() + ", "\r
180                                                 + dictionaryDefinition.getUpdatedBy());\r
181 \r
182                             } else {\r
183                                 resourceDictionary.setTags(dictionaryDefinition.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.checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));\r
247             ModelType modelType = new ModelType();\r
248             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);\r
249             modelType.setDerivedFrom(nodeType.getDerivedFrom());\r
250             modelType.setDescription(nodeType.getDescription());\r
251             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
252             modelType.setModelName(nodeKey);\r
253             modelType.setVersion(nodeType.getVersion());\r
254             modelType.setUpdatedBy("System");\r
255             modelType.setTags(nodeKey + "," + BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE + ","\r
256                     + nodeType.getDerivedFrom());\r
257             modelTypeService.saveModel(modelType);\r
258             log.trace("Loaded Node Type successfully : {}", file.getFilename());\r
259         } catch (Exception e) {\r
260             errorBuilder.appendln("Node type loading error : " + file.getFilename() + ":" + e.getMessage());\r
261         }\r
262     }\r
263 \r
264     private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {\r
265         try {\r
266             log.trace("Loading Data Type: {}", file.getFilename());\r
267             String dataKey = file.getFilename().replace(".json", "");\r
268             String definitionContent = getResourceContent(file);\r
269             DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);\r
270             Preconditions.checkNotNull(dataType, String.format("failed to get data type from file : %s", file.getFilename()));\r
271             ModelType modelType = new ModelType();\r
272             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
273             modelType.setDerivedFrom(dataType.getDerivedFrom());\r
274             modelType.setDescription(dataType.getDescription());\r
275             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
276             modelType.setModelName(dataKey);\r
277             modelType.setVersion(dataType.getVersion());\r
278             modelType.setUpdatedBy("System");\r
279             modelType.setTags(dataKey + "," + dataType.getDerivedFrom() + ","\r
280                     + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
281             modelTypeService.saveModel(modelType);\r
282             log.trace(" Loaded Data Type successfully : {}", file.getFilename());\r
283         } catch (Exception e) {\r
284             errorBuilder.appendln("Data type loading error : " + file.getFilename() + ":" + e.getMessage());\r
285         }\r
286     }\r
287 \r
288     private void loadArtifactType(Resource file, StrBuilder errorBuilder) {\r
289         try {\r
290             log.trace("Loading Artifact Type: {}", file.getFilename());\r
291             String dataKey = file.getFilename().replace(".json", "");\r
292             String definitionContent = getResourceContent(file);\r
293             ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);\r
294             Preconditions.checkNotNull(artifactType, String.format("failed to get artifact type from file : %s", file.getFilename()));\r
295             ModelType modelType = new ModelType();\r
296             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
297             modelType.setDerivedFrom(artifactType.getDerivedFrom());\r
298             modelType.setDescription(artifactType.getDescription());\r
299             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
300             modelType.setModelName(dataKey);\r
301             modelType.setVersion(artifactType.getVersion());\r
302             modelType.setUpdatedBy("System");\r
303             modelType.setTags(dataKey + "," + artifactType.getDerivedFrom() + ","\r
304                     + BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
305             modelTypeService.saveModel(modelType);\r
306             log.trace("Loaded Artifact Type successfully : {}", file.getFilename());\r
307         } catch (Exception e) {\r
308             errorBuilder.appendln("Artifact type loading error : " + file.getFilename() + ":" + e.getMessage());\r
309         }\r
310     }\r
311 \r
312     private Resource[] getPathResources(String path, String extension) throws IOException {\r
313         return resourceLoader.getResources("file:" + path + "/*" + extension);\r
314     }\r
315 \r
316     private String getResourceContent(Resource resource) throws IOException {\r
317         return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());\r
318     }\r
319 \r
320 }\r