2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.apps.controllerblueprints.service;
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;
52 * DataBaseInitService.java Purpose: Provide DataBaseInitService Service
54 * @author Brinda Santh
59 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")
60 public class DataBaseInitService {
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";
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;
82 private ResourcePatternResolver resourceLoader;
85 * This is a DataBaseInitService, used to load the initial data
87 * @param modelTypeService modelTypeService
88 * @param resourceDictionaryService resourceDictionaryService
89 * @param configModelService configModelService
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...");
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);
110 loadResourceDictionary();
113 private void loadModelType() {
114 log.info(" *************************** loadModelType **********************");
116 Resource[] dataTypefiles = getPathResources(dataTypePath, JSON_EXTN);
117 StrBuilder errorBuilder = new StrBuilder();
118 for (Resource file : dataTypefiles) {
120 loadDataType(file, errorBuilder);
124 Resource[] nodeTypefiles = getPathResources(nodeTypePath, JSON_EXTN);
125 for (Resource file : nodeTypefiles) {
127 loadNodeType(file, errorBuilder);
132 Resource[] artifactTypefiles = getPathResources(artifactTypePath, JSON_EXTN);
134 for (Resource file : artifactTypefiles) {
136 loadArtifactType(file, errorBuilder);
141 if (!errorBuilder.isEmpty()) {
142 log.error(errorBuilder.toString());
144 } catch (Exception e) {
145 log.error("Failed in Data type loading", e);
149 private void loadResourceDictionary() {
151 " *************************** loadResourceDictionary **********************");
153 Resource[] dataTypefiles = getPathResources(resourceDictionaryPath, JSON_EXTN);
155 StrBuilder errorBuilder = new StrBuilder();
157 for (Resource file : dataTypefiles) {
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);
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());
176 resourceDictionary.setUpdatedBy(resourceDefinition.getUpdatedBy());
177 if (StringUtils.isBlank(resourceDefinition.getTags())) {
178 resourceDictionary.setTags(
179 resourceDefinition.getName() + ", " + resourceDefinition.getUpdatedBy()
180 + ", " + resourceDefinition.getUpdatedBy());
183 resourceDictionary.setTags(resourceDefinition.getTags());
185 resourceDictionaryService.saveResourceDictionary(resourceDictionary);
187 log.trace(" Loaded successfully : {}", file.getFilename());
189 throw new BluePrintException("couldn't get dictionary from content information");
191 } catch (Exception e) {
192 log.error(EXCEPTION, e);
193 errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());
196 if (!errorBuilder.isEmpty()) {
197 log.error(errorBuilder.toString());
201 } catch (Exception e) {
203 "Failed in Resource dictionary loading", e);
207 private void loadBlueprints() {
208 log.info("*************************** loadServiceTemplate **********************");
210 List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(bluePrintsPath);
211 if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {
212 StrBuilder errorBuilder = new StrBuilder();
213 for (String fileName : serviceTemplateDirs) {
215 String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);
216 log.debug("***** Loading service template : {}", bluePrintPath);
217 ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);
219 configModel = this.configModelService.saveConfigModel(configModel);
221 log.info("Publishing : {}", configModel.getId());
223 this.configModelService.publishConfigModel(configModel.getId());
225 log.info("Loaded service template successfully: {}", fileName);
227 } catch (Exception e) {
228 log.error(EXCEPTION, e);
229 errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());
233 if (!errorBuilder.isEmpty()) {
234 log.error(errorBuilder.toString());
237 } catch (Exception e) {
238 log.error("Failed in Service Template loading", e);
242 private void loadNodeType(Resource file, StrBuilder errorBuilder) {
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());
267 private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {
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());
292 private void loadArtifactType(Resource file, StrBuilder errorBuilder) {
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());
317 private Resource[] getPathResources(String path, String extension) throws IOException {
318 return resourceLoader.getResources("file:" + path + "/*" + extension);
321 private String getResourceContent(Resource resource) throws IOException {
322 return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());