2 * Copyright © 2017-2018 AT&T Intellectual Property.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.ccsdk.apps.controllerblueprints.service;
\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
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
49 * DataBaseInitService.java Purpose: Provide DataBaseInitService Service
\r
51 * @author Brinda Santh
\r
56 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true", matchIfMissing = false)
\r
57 public class DataBaseInitService {
\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
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
73 private ResourcePatternResolver resourceLoader;
\r
76 * This is a DataBaseInitService, used to load the initial data
\r
78 * @param modelTypeService
\r
79 * @param resourceDictionaryService
\r
80 * @param configModelService
\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
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
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
107 loadResourceDictionary();
\r
111 private void loadModelType() {
\r
112 log.info(" *************************** loadModelType **********************");
\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
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
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
142 if (!errorBuilder.isEmpty()) {
\r
143 log.error(errorBuilder.toString());
\r
145 } catch (Exception e) {
\r
146 log.error("Failed in Data type loading", e);
\r
150 private void loadResourceDictionary() {
\r
152 " *************************** loadResourceDictionary **********************");
\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
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
171 if (dictionaryDefinition.getValidValues() != null)
\r
173 .setValidValues(String.valueOf(dictionaryDefinition.getValidValues()));
\r
175 if (dictionaryDefinition.getSampleValue() != null)
\r
177 .setValidValues(String.valueOf(dictionaryDefinition.getSampleValue()));
\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
191 resourceDictionary.setTags(dictionaryDefinition.getTags());
\r
193 resourceDictionaryService.saveResourceDictionary(resourceDictionary);
\r
195 log.trace(" Loaded successfully : {}", file.getFilename());
\r
197 throw new BluePrintException("couldn't get dictionary from content information");
\r
199 } catch (Exception e) {
\r
200 errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());
\r
203 if (!errorBuilder.isEmpty()) {
\r
204 log.error(errorBuilder.toString());
\r
208 } catch (Exception e) {
\r
210 "Failed in Resource dictionary loading", e);
\r
214 private void loadBlueprints() {
\r
215 log.info("*************************** loadServiceTemplate **********************");
\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
222 String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);
\r
223 log.debug("***** Loading service template : {}", bluePrintPath);
\r
224 ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);
\r
226 configModel = this.configModelService.saveConfigModel(configModel);
\r
228 log.info("Publishing : {}", configModel.getId());
\r
230 this.configModelService.publishConfigModel(configModel.getId());
\r
232 log.info("Loaded service template successfully: {}", fileName);
\r
234 } catch (Exception e) {
\r
235 errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());
\r
239 if (!errorBuilder.isEmpty()) {
\r
240 log.error(errorBuilder.toString());
\r
243 } catch (Exception e) {
\r
244 log.error("Failed in Service Template loading", e);
\r
248 private void loadNodeType(Resource file, StrBuilder errorBuilder) {
\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
271 private void loadDataType(Resource file, StrBuilder errorBuilder) {
\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
294 private void loadArtifactType(Resource file, StrBuilder errorBuilder) {
\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
317 private Resource[] getPathResources(String path, String extension) throws IOException {
\r
318 return resourceLoader.getResources("file:" + path + "/*" + extension);
\r
321 private String getResourceContent(Resource resource) throws IOException {
\r
322 return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
\r