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