cfcf93d291cdf16dced3d07e36d64ee6e91d6133
[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 resourceDefinition =\r
161                                 JacksonUtils.readValue(definitionContent, ResourceDefinition.class);\r
162                         if (resourceDefinition != null) {\r
163                             Preconditions.checkNotNull(resourceDefinition.getProperty(), "Failed to get Property Definition");\r
164                             ResourceDictionary resourceDictionary = new ResourceDictionary();\r
165                             resourceDictionary.setName(resourceDefinition.getName());\r
166                             resourceDictionary.setDefinition(resourceDefinition);\r
167 \r
168                             Preconditions.checkNotNull(resourceDefinition.getProperty(), "Property field is missing");\r
169                             resourceDictionary.setDescription(resourceDefinition.getProperty().getDescription());\r
170                             resourceDictionary.setDataType(resourceDefinition.getProperty().getType());\r
171                             if(resourceDefinition.getProperty().getEntrySchema() != null){\r
172                                 resourceDictionary.setEntrySchema(resourceDefinition.getProperty().getEntrySchema().getType());\r
173                             }\r
174                             resourceDictionary.setUpdatedBy(resourceDefinition.getUpdatedBy());\r
175                             if (StringUtils.isBlank(resourceDefinition.getTags())) {\r
176                                 resourceDictionary.setTags(\r
177                                         resourceDefinition.getName() + ", " + resourceDefinition.getUpdatedBy()\r
178                                                 + ", " + resourceDefinition.getUpdatedBy());\r
179 \r
180                             } else {\r
181                                 resourceDictionary.setTags(resourceDefinition.getTags());\r
182                             }\r
183                             resourceDictionaryService.saveResourceDictionary(resourceDictionary);\r
184 \r
185                             log.trace(" Loaded successfully : {}", file.getFilename());\r
186                         } else {\r
187                             throw new BluePrintException("couldn't get dictionary from content information");\r
188                         }\r
189                     } catch (Exception e) {\r
190                         errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());\r
191                     }\r
192                 }\r
193                 if (!errorBuilder.isEmpty()) {\r
194                     log.error(errorBuilder.toString());\r
195                 }\r
196 \r
197 \r
198         } catch (Exception e) {\r
199             log.error(\r
200                     "Failed in Resource dictionary loading", e);\r
201         }\r
202     }\r
203 \r
204     private void loadBlueprints() {\r
205         log.info("*************************** loadServiceTemplate **********************");\r
206         try {\r
207             List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(bluePrintsPath);\r
208             if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {\r
209                 StrBuilder errorBuilder = new StrBuilder();\r
210                 for (String fileName : serviceTemplateDirs) {\r
211                     try {\r
212                         String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);\r
213                         log.debug("***** Loading service template :  {}", bluePrintPath);\r
214                         ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);\r
215 \r
216                         configModel = this.configModelService.saveConfigModel(configModel);\r
217 \r
218                         log.info("Publishing : {}", configModel.getId());\r
219 \r
220                         this.configModelService.publishConfigModel(configModel.getId());\r
221 \r
222                         log.info("Loaded service template successfully: {}", fileName);\r
223 \r
224                     } catch (Exception e) {\r
225                         errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());\r
226                     }\r
227                 }\r
228 \r
229                 if (!errorBuilder.isEmpty()) {\r
230                     log.error(errorBuilder.toString());\r
231                 }\r
232             }\r
233         } catch (Exception e) {\r
234             log.error("Failed in Service Template loading", e);\r
235         }\r
236     }\r
237 \r
238     private void loadNodeType(Resource file, StrBuilder errorBuilder) {\r
239         try {\r
240             log.trace("Loading Node Type : {}", file.getFilename());\r
241             String nodeKey = file.getFilename().replace(".json", "");\r
242             String definitionContent = getResourceContent(file);\r
243             NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);\r
244             Preconditions.checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));\r
245             ModelType modelType = new ModelType();\r
246             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);\r
247             modelType.setDerivedFrom(nodeType.getDerivedFrom());\r
248             modelType.setDescription(nodeType.getDescription());\r
249             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
250             modelType.setModelName(nodeKey);\r
251             modelType.setVersion(nodeType.getVersion());\r
252             modelType.setUpdatedBy("System");\r
253             modelType.setTags(nodeKey + "," + BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE + ","\r
254                     + nodeType.getDerivedFrom());\r
255             modelTypeService.saveModel(modelType);\r
256             log.trace("Loaded Node Type successfully : {}", file.getFilename());\r
257         } catch (Exception e) {\r
258             errorBuilder.appendln("Node type loading error : " + file.getFilename() + ":" + e.getMessage());\r
259         }\r
260     }\r
261 \r
262     private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {\r
263         try {\r
264             log.trace("Loading Data Type: {}", file.getFilename());\r
265             String dataKey = file.getFilename().replace(".json", "");\r
266             String definitionContent = getResourceContent(file);\r
267             DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);\r
268             Preconditions.checkNotNull(dataType, String.format("failed to get data type from file : %s", file.getFilename()));\r
269             ModelType modelType = new ModelType();\r
270             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
271             modelType.setDerivedFrom(dataType.getDerivedFrom());\r
272             modelType.setDescription(dataType.getDescription());\r
273             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
274             modelType.setModelName(dataKey);\r
275             modelType.setVersion(dataType.getVersion());\r
276             modelType.setUpdatedBy("System");\r
277             modelType.setTags(dataKey + "," + dataType.getDerivedFrom() + ","\r
278                     + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
279             modelTypeService.saveModel(modelType);\r
280             log.trace(" Loaded Data Type successfully : {}", file.getFilename());\r
281         } catch (Exception e) {\r
282             errorBuilder.appendln("Data type loading error : " + file.getFilename() + ":" + e.getMessage());\r
283         }\r
284     }\r
285 \r
286     private void loadArtifactType(Resource file, StrBuilder errorBuilder) {\r
287         try {\r
288             log.trace("Loading Artifact Type: {}", file.getFilename());\r
289             String dataKey = file.getFilename().replace(".json", "");\r
290             String definitionContent = getResourceContent(file);\r
291             ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);\r
292             Preconditions.checkNotNull(artifactType, String.format("failed to get artifact type from file : %s", file.getFilename()));\r
293             ModelType modelType = new ModelType();\r
294             modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
295             modelType.setDerivedFrom(artifactType.getDerivedFrom());\r
296             modelType.setDescription(artifactType.getDescription());\r
297             modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));\r
298             modelType.setModelName(dataKey);\r
299             modelType.setVersion(artifactType.getVersion());\r
300             modelType.setUpdatedBy("System");\r
301             modelType.setTags(dataKey + "," + artifactType.getDerivedFrom() + ","\r
302                     + BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
303             modelTypeService.saveModel(modelType);\r
304             log.trace("Loaded Artifact Type successfully : {}", file.getFilename());\r
305         } catch (Exception e) {\r
306             errorBuilder.appendln("Artifact type loading error : " + file.getFilename() + ":" + e.getMessage());\r
307         }\r
308     }\r
309 \r
310     private Resource[] getPathResources(String path, String extension) throws IOException {\r
311         return resourceLoader.getResources("file:" + path + "/*" + extension);\r
312     }\r
313 \r
314     private String getResourceContent(Resource resource) throws IOException {\r
315         return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());\r
316     }\r
317 \r
318 }\r