2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.apps.controllerblueprints.service;
\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
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
51 * DataBaseInitService.java Purpose: Provide DataBaseInitService Service
\r
53 * @author Brinda Santh
\r
58 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")
\r
59 public class DataBaseInitService {
\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
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
75 private ResourcePatternResolver resourceLoader;
\r
78 * This is a DataBaseInitService, used to load the initial data
\r
80 * @param modelTypeService modelTypeService
\r
81 * @param resourceDictionaryService resourceDictionaryService
\r
82 * @param configModelService configModelService
\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
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
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
109 loadResourceDictionary();
\r
113 private void loadModelType() {
\r
114 log.info(" *************************** loadModelType **********************");
\r
116 Resource[] dataTypefiles = getPathResources(dataTypePath, ".json");
\r
117 StrBuilder errorBuilder = new StrBuilder();
\r
118 for (Resource file : dataTypefiles) {
\r
119 if (file != null) {
\r
120 loadDataType(file, errorBuilder);
\r
124 Resource[] nodeTypefiles = getPathResources(nodeTypePath, ".json");
\r
125 for (Resource file : nodeTypefiles) {
\r
126 if (file != null) {
\r
127 loadNodeType(file, errorBuilder);
\r
132 Resource[] artifactTypefiles = getPathResources(artifactTypePath, ".json");
\r
134 for (Resource file : artifactTypefiles) {
\r
135 if (file != null) {
\r
136 loadArtifactType(file, errorBuilder);
\r
141 if (!errorBuilder.isEmpty()) {
\r
142 log.error(errorBuilder.toString());
\r
144 } catch (Exception e) {
\r
145 log.error("Failed in Data type loading", e);
\r
149 private void loadResourceDictionary() {
\r
151 " *************************** loadResourceDictionary **********************");
\r
153 Resource[] dataTypefiles = getPathResources(resourceDictionaryPath, ".json");
\r
155 StrBuilder errorBuilder = new StrBuilder();
\r
157 for (Resource file : dataTypefiles) {
\r
159 fileName = file.getFilename();
\r
160 log.trace("Loading : {}", fileName);
\r
161 String definitionContent = getResourceContent(file);
\r
162 ResourceDefinition dictionaryDefinition =
\r
163 JacksonUtils.readValue(definitionContent, ResourceDefinition.class);
\r
164 if (dictionaryDefinition != null) {
\r
165 Preconditions.checkNotNull(dictionaryDefinition.getProperty(), "Failed to get Property Definition");
\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 resourceDictionary.setResourceType(dictionaryDefinition.getResourceType());
\r
172 resourceDictionary.setDescription(dictionaryDefinition.getProperty().getDescription());
\r
173 resourceDictionary.setDataType(dictionaryDefinition.getProperty().getType());
\r
174 if(dictionaryDefinition.getProperty().getEntrySchema() != null){
\r
175 resourceDictionary.setEntrySchema(dictionaryDefinition.getProperty().getEntrySchema().getType());
\r
177 resourceDictionary.setUpdatedBy(dictionaryDefinition.getUpdatedBy());
\r
178 if (StringUtils.isBlank(dictionaryDefinition.getTags())) {
\r
179 resourceDictionary.setTags(
\r
180 dictionaryDefinition.getName() + ", " + dictionaryDefinition.getUpdatedBy()
\r
181 + ", " + dictionaryDefinition.getResourceType() + ", "
\r
182 + dictionaryDefinition.getUpdatedBy());
\r
185 resourceDictionary.setTags(dictionaryDefinition.getTags());
\r
187 resourceDictionaryService.saveResourceDictionary(resourceDictionary);
\r
189 log.trace(" Loaded successfully : {}", file.getFilename());
\r
191 throw new BluePrintException("couldn't get dictionary from content information");
\r
193 } catch (Exception e) {
\r
194 errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());
\r
197 if (!errorBuilder.isEmpty()) {
\r
198 log.error(errorBuilder.toString());
\r
202 } catch (Exception e) {
\r
204 "Failed in Resource dictionary loading", e);
\r
208 private void loadBlueprints() {
\r
209 log.info("*************************** loadServiceTemplate **********************");
\r
211 List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(bluePrintsPath);
\r
212 if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {
\r
213 StrBuilder errorBuilder = new StrBuilder();
\r
214 for (String fileName : serviceTemplateDirs) {
\r
216 String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);
\r
217 log.debug("***** Loading service template : {}", bluePrintPath);
\r
218 ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);
\r
220 configModel = this.configModelService.saveConfigModel(configModel);
\r
222 log.info("Publishing : {}", configModel.getId());
\r
224 this.configModelService.publishConfigModel(configModel.getId());
\r
226 log.info("Loaded service template successfully: {}", fileName);
\r
228 } catch (Exception e) {
\r
229 errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());
\r
233 if (!errorBuilder.isEmpty()) {
\r
234 log.error(errorBuilder.toString());
\r
237 } catch (Exception e) {
\r
238 log.error("Failed in Service Template loading", e);
\r
242 private void loadNodeType(Resource file, StrBuilder errorBuilder) {
\r
244 log.trace("Loading Node Type : {}", file.getFilename());
\r
245 String nodeKey = file.getFilename().replace(".json", "");
\r
246 String definitionContent = getResourceContent(file);
\r
247 NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);
\r
248 Preconditions.checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));
\r
249 ModelType modelType = new ModelType();
\r
250 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);
\r
251 modelType.setDerivedFrom(nodeType.getDerivedFrom());
\r
252 modelType.setDescription(nodeType.getDescription());
\r
253 modelType.setDefinition(definitionContent);
\r
254 modelType.setModelName(nodeKey);
\r
255 modelType.setVersion(nodeType.getVersion());
\r
256 modelType.setUpdatedBy("System");
\r
257 modelType.setTags(nodeKey + "," + BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE + ","
\r
258 + nodeType.getDerivedFrom());
\r
259 modelTypeService.saveModel(modelType);
\r
260 log.trace("Loaded Node Type successfully : {}", file.getFilename());
\r
261 } catch (Exception e) {
\r
262 errorBuilder.appendln("Node type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
266 private void loadDataType(Resource file, StrBuilder errorBuilder) {
\r
268 log.trace("Loading Data Type: {}", file.getFilename());
\r
269 String dataKey = file.getFilename().replace(".json", "");
\r
270 String definitionContent = getResourceContent(file);
\r
271 DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);
\r
272 Preconditions.checkNotNull(dataType, String.format("failed to get data type from file : %s", file.getFilename()));
\r
273 ModelType modelType = new ModelType();
\r
274 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
\r
275 modelType.setDerivedFrom(dataType.getDerivedFrom());
\r
276 modelType.setDescription(dataType.getDescription());
\r
277 modelType.setDefinition(definitionContent);
\r
278 modelType.setModelName(dataKey);
\r
279 modelType.setVersion(dataType.getVersion());
\r
280 modelType.setUpdatedBy("System");
\r
281 modelType.setTags(dataKey + "," + dataType.getDerivedFrom() + ","
\r
282 + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
\r
283 modelTypeService.saveModel(modelType);
\r
284 log.trace(" Loaded Data Type successfully : {}", file.getFilename());
\r
285 } catch (Exception e) {
\r
286 errorBuilder.appendln("Data type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
290 private void loadArtifactType(Resource file, StrBuilder errorBuilder) {
\r
292 log.trace("Loading Artifact Type: {}", file.getFilename());
\r
293 String dataKey = file.getFilename().replace(".json", "");
\r
294 String definitionContent = getResourceContent(file);
\r
295 ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);
\r
296 Preconditions.checkNotNull(artifactType, String.format("failed to get artifact type from file : %s", file.getFilename()));
\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
314 private Resource[] getPathResources(String path, String extension) throws IOException {
\r
315 return resourceLoader.getResources("file:" + path + "/*" + extension);
\r
318 private String getResourceContent(Resource resource) throws IOException {
\r
319 return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
\r