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.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.context.annotation.DependsOn;
\r
43 import org.springframework.core.io.Resource;
\r
44 import org.springframework.core.io.support.ResourcePatternResolver;
\r
45 import org.springframework.stereotype.Component;
\r
47 import javax.annotation.PostConstruct;
\r
48 import java.io.IOException;
\r
49 import java.nio.charset.Charset;
\r
50 import java.util.List;
\r
53 * DataBaseInitService.java Purpose: Provide DataBaseInitService Service
\r
55 * @author Brinda Santh
\r
60 @DependsOn(value = "ApplicationRegistrationService")
\r
61 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")
\r
62 public class DataBaseInitService {
\r
64 private static EELFLogger log = EELFManager.getInstance().getLogger(DataBaseInitService.class);
\r
65 private ModelTypeService modelTypeService;
\r
66 private ResourceDictionaryService resourceDictionaryService;
\r
67 private ConfigModelService configModelService;
\r
69 @Value("${load.dataTypePath}")
\r
70 private String dataTypePath;
\r
71 @Value("${load.nodeTypePath}")
\r
72 private String nodeTypePath;
\r
73 @Value("${load.artifactTypePath}")
\r
74 private String artifactTypePath;
\r
75 @Value("${load.resourceDictionaryPath}")
\r
76 private String resourceDictionaryPath;
\r
77 @Value("${load.blueprintsPath}")
\r
78 private String bluePrintsPath;
\r
81 private ResourcePatternResolver resourceLoader;
\r
84 * This is a DataBaseInitService, used to load the initial data
\r
86 * @param modelTypeService modelTypeService
\r
87 * @param resourceDictionaryService resourceDictionaryService
\r
88 * @param configModelService configModelService
\r
90 public DataBaseInitService(ModelTypeService modelTypeService, ResourceDictionaryService resourceDictionaryService,
\r
91 ConfigModelService configModelService) {
\r
92 this.modelTypeService = modelTypeService;
\r
93 this.resourceDictionaryService = resourceDictionaryService;
\r
94 this.configModelService = configModelService;
\r
95 log.info("DataBaseInitService started...");
\r
100 @SuppressWarnings("unused")
\r
101 private void initDatabase() {
\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
131 Resource[] artifactTypefiles = getPathResources(artifactTypePath, ".json");
\r
133 for (Resource file : artifactTypefiles) {
\r
134 if (file != null) {
\r
135 loadArtifactType(file, errorBuilder);
\r
139 if (!errorBuilder.isEmpty()) {
\r
140 log.error(errorBuilder.toString());
\r
142 } catch (Exception e) {
\r
143 log.error("Failed in Data type loading", e);
\r
147 private void loadResourceDictionary() {
\r
149 " *************************** loadResourceDictionary **********************");
\r
151 Resource[] dataTypefiles = getPathResources(resourceDictionaryPath, ".json");
\r
153 StrBuilder errorBuilder = new StrBuilder();
\r
155 for (Resource file : dataTypefiles) {
\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
164 .checkNotNull(resourceDefinition.getProperty(), "Failed to get Property Definition");
\r
165 ResourceDictionary resourceDictionary = new ResourceDictionary();
\r
166 resourceDictionary.setName(resourceDefinition.getName());
\r
167 resourceDictionary.setDefinition(resourceDefinition);
\r
169 Preconditions.checkNotNull(resourceDefinition.getProperty(), "Property field is missing");
\r
170 resourceDictionary.setDescription(resourceDefinition.getProperty().getDescription());
\r
171 resourceDictionary.setDataType(resourceDefinition.getProperty().getType());
\r
172 if (resourceDefinition.getProperty().getEntrySchema() != null) {
\r
174 .setEntrySchema(resourceDefinition.getProperty().getEntrySchema().getType());
\r
176 resourceDictionary.setUpdatedBy(resourceDefinition.getUpdatedBy());
\r
177 if (StringUtils.isBlank(resourceDefinition.getTags())) {
\r
178 resourceDictionary.setTags(
\r
179 resourceDefinition.getName() + ", " + resourceDefinition.getUpdatedBy()
\r
180 + ", " + resourceDefinition.getUpdatedBy());
\r
183 resourceDictionary.setTags(resourceDefinition.getTags());
\r
185 resourceDictionaryService.saveResourceDictionary(resourceDictionary);
\r
187 log.trace(" Loaded successfully : {}", file.getFilename());
\r
189 throw new BluePrintException("couldn't get dictionary from content information");
\r
191 } catch (Exception e) {
\r
192 errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());
\r
195 if (!errorBuilder.isEmpty()) {
\r
196 log.error(errorBuilder.toString());
\r
200 } catch (Exception e) {
\r
202 "Failed in Resource dictionary loading", e);
\r
206 private void loadBlueprints() {
\r
207 log.info("*************************** loadServiceTemplate **********************");
\r
209 List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(bluePrintsPath);
\r
210 if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {
\r
211 StrBuilder errorBuilder = new StrBuilder();
\r
212 for (String fileName : serviceTemplateDirs) {
\r
214 String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);
\r
215 log.debug("***** Loading service template : {}", bluePrintPath);
\r
216 ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);
\r
218 configModel = this.configModelService.saveConfigModel(configModel);
\r
220 log.info("Publishing : {}", configModel.getId());
\r
222 this.configModelService.publishConfigModel(configModel.getId());
\r
224 log.info("Loaded service template successfully: {}", fileName);
\r
226 } catch (Exception e) {
\r
227 errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());
\r
231 if (!errorBuilder.isEmpty()) {
\r
232 log.error(errorBuilder.toString());
\r
235 } catch (Exception e) {
\r
236 log.error("Failed in Service Template loading", e);
\r
240 private void loadNodeType(Resource file, StrBuilder errorBuilder) {
\r
242 log.trace("Loading Node Type : {}", file.getFilename());
\r
243 String nodeKey = file.getFilename().replace(".json", "");
\r
244 String definitionContent = getResourceContent(file);
\r
245 NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);
\r
247 .checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));
\r
248 ModelType modelType = new ModelType();
\r
249 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);
\r
250 modelType.setDerivedFrom(nodeType.getDerivedFrom());
\r
251 modelType.setDescription(nodeType.getDescription());
\r
252 modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));
\r
253 modelType.setModelName(nodeKey);
\r
254 modelType.setVersion(nodeType.getVersion());
\r
255 modelType.setUpdatedBy("System");
\r
256 modelType.setTags(nodeKey + "," + BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE + ","
\r
257 + nodeType.getDerivedFrom());
\r
258 modelTypeService.saveModel(modelType);
\r
259 log.trace("Loaded Node Type successfully : {}", file.getFilename());
\r
260 } catch (Exception e) {
\r
261 errorBuilder.appendln("Node type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
265 private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {
\r
267 log.trace("Loading Data Type: {}", file.getFilename());
\r
268 String dataKey = file.getFilename().replace(".json", "");
\r
269 String definitionContent = getResourceContent(file);
\r
270 DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);
\r
272 .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(JacksonUtils.jsonNode(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,
\r
297 String.format("failed to get artifact type from file : %s", file.getFilename()));
\r
298 ModelType modelType = new ModelType();
\r
299 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);
\r
300 modelType.setDerivedFrom(artifactType.getDerivedFrom());
\r
301 modelType.setDescription(artifactType.getDescription());
\r
302 modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));
\r
303 modelType.setModelName(dataKey);
\r
304 modelType.setVersion(artifactType.getVersion());
\r
305 modelType.setUpdatedBy("System");
\r
306 modelType.setTags(dataKey + "," + artifactType.getDerivedFrom() + ","
\r
307 + BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);
\r
308 modelTypeService.saveModel(modelType);
\r
309 log.trace("Loaded Artifact Type successfully : {}", file.getFilename());
\r
310 } catch (Exception e) {
\r
311 errorBuilder.appendln("Artifact type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
315 private Resource[] getPathResources(String path, String extension) throws IOException {
\r
316 return resourceLoader.getResources("file:" + path + "/*" + extension);
\r
319 private String getResourceContent(Resource resource) throws IOException {
\r
320 return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
\r