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.core.io.Resource;
\r
43 import org.springframework.core.io.support.ResourcePatternResolver;
\r
44 import org.springframework.stereotype.Component;
\r
46 import javax.annotation.PostConstruct;
\r
47 import java.io.IOException;
\r
48 import java.nio.charset.Charset;
\r
49 import java.util.List;
\r
52 * DataBaseInitService.java Purpose: Provide DataBaseInitService Service
\r
54 * @author Brinda Santh
\r
59 @ConditionalOnProperty(name = "blueprints.load.initial-data", havingValue = "true")
\r
60 public class DataBaseInitService {
\r
62 private static EELFLogger log = EELFManager.getInstance().getLogger(DataBaseInitService.class);
\r
63 @Value("${blueprints.load.path}")
\r
64 private String modelLoadPath;
\r
65 private ModelTypeService modelTypeService;
\r
66 private ResourceDictionaryService resourceDictionaryService;
\r
67 private ConfigModelService configModelService;
\r
69 private String dataTypePath;
\r
70 private String nodeTypePath;
\r
71 private String artifactTypePath;
\r
72 private String resourceDictionaryPath;
\r
73 private String bluePrintsPath;
\r
76 private ResourcePatternResolver resourceLoader;
\r
79 * This is a DataBaseInitService, used to load the initial data
\r
81 * @param modelTypeService modelTypeService
\r
82 * @param resourceDictionaryService resourceDictionaryService
\r
83 * @param configModelService configModelService
\r
85 public DataBaseInitService(ModelTypeService modelTypeService, ResourceDictionaryService resourceDictionaryService,
\r
86 ConfigModelService configModelService) {
\r
87 this.modelTypeService = modelTypeService;
\r
88 this.resourceDictionaryService = resourceDictionaryService;
\r
89 this.configModelService = configModelService;
\r
90 log.info("DataBaseInitService started...");
\r
95 @SuppressWarnings("unused")
\r
96 private void initDatabase() {
\r
97 log.info("loading Blueprints from DIR : {}", modelLoadPath);
\r
98 dataTypePath = modelLoadPath + "/model_type/data_type";
\r
99 nodeTypePath = modelLoadPath + "/model_type/node_type";
\r
100 artifactTypePath = modelLoadPath + "/model_type/artifact_type";
\r
101 resourceDictionaryPath = modelLoadPath + "/resource_dictionary";
\r
102 bluePrintsPath = modelLoadPath + "/blueprints";
\r
104 log.info("loading dataTypePath from DIR : {}", dataTypePath);
\r
105 log.info("loading nodeTypePath from DIR : {}", nodeTypePath);
\r
106 log.info("loading artifactTypePath from DIR : {}", artifactTypePath);
\r
107 log.info("loading resourceDictionaryPath from DIR : {}", resourceDictionaryPath);
\r
108 log.info("loading bluePrintsPath from DIR : {}", bluePrintsPath);
\r
111 loadResourceDictionary();
\r
115 private void loadModelType() {
\r
116 log.info(" *************************** loadModelType **********************");
\r
118 Resource[] dataTypefiles = getPathResources(dataTypePath, ".json");
\r
119 StrBuilder errorBuilder = new StrBuilder();
\r
120 for (Resource file : dataTypefiles) {
\r
121 if (file != null) {
\r
122 loadDataType(file, errorBuilder);
\r
126 Resource[] nodeTypefiles = getPathResources(nodeTypePath, ".json");
\r
127 for (Resource file : nodeTypefiles) {
\r
128 if (file != null) {
\r
129 loadNodeType(file, errorBuilder);
\r
134 Resource[] artifactTypefiles = getPathResources(artifactTypePath, ".json");
\r
136 for (Resource file : artifactTypefiles) {
\r
137 if (file != null) {
\r
138 loadArtifactType(file, errorBuilder);
\r
143 if (!errorBuilder.isEmpty()) {
\r
144 log.error(errorBuilder.toString());
\r
146 } catch (Exception e) {
\r
147 log.error("Failed in Data type loading", e);
\r
151 private void loadResourceDictionary() {
\r
153 " *************************** loadResourceDictionary **********************");
\r
155 Resource[] dataTypefiles = getPathResources(resourceDictionaryPath, ".json");
\r
157 StrBuilder errorBuilder = new StrBuilder();
\r
159 for (Resource file : dataTypefiles) {
\r
161 fileName = file.getFilename();
\r
162 log.trace("Loading : {}", fileName);
\r
163 String definitionContent = getResourceContent(file);
\r
164 ResourceDefinition dictionaryDefinition =
\r
165 JacksonUtils.readValue(definitionContent, ResourceDefinition.class);
\r
166 if (dictionaryDefinition != null) {
\r
167 Preconditions.checkNotNull(dictionaryDefinition.getProperty(), "Failed to get Property Definition");
\r
168 ResourceDictionary resourceDictionary = new ResourceDictionary();
\r
169 resourceDictionary.setResourcePath(dictionaryDefinition.getResourcePath());
\r
170 resourceDictionary.setName(dictionaryDefinition.getName());
\r
171 resourceDictionary.setDefinition(dictionaryDefinition);
\r
173 resourceDictionary.setResourceType(dictionaryDefinition.getResourceType());
\r
174 resourceDictionary.setDescription(dictionaryDefinition.getProperty().getDescription());
\r
175 resourceDictionary.setDataType(dictionaryDefinition.getProperty().getType());
\r
176 if(dictionaryDefinition.getProperty().getEntrySchema() != null){
\r
177 resourceDictionary.setEntrySchema(dictionaryDefinition.getProperty().getEntrySchema().getType());
\r
179 resourceDictionary.setUpdatedBy(dictionaryDefinition.getUpdatedBy());
\r
180 if (StringUtils.isBlank(dictionaryDefinition.getTags())) {
\r
181 resourceDictionary.setTags(
\r
182 dictionaryDefinition.getName() + ", " + dictionaryDefinition.getUpdatedBy()
\r
183 + ", " + dictionaryDefinition.getResourceType() + ", "
\r
184 + dictionaryDefinition.getUpdatedBy());
\r
187 resourceDictionary.setTags(dictionaryDefinition.getTags());
\r
189 resourceDictionaryService.saveResourceDictionary(resourceDictionary);
\r
191 log.trace(" Loaded successfully : {}", file.getFilename());
\r
193 throw new BluePrintException("couldn't get dictionary from content information");
\r
195 } catch (Exception e) {
\r
196 errorBuilder.appendln("Dictionary loading Errors : " + file.getFilename() + ":" + e.getMessage());
\r
199 if (!errorBuilder.isEmpty()) {
\r
200 log.error(errorBuilder.toString());
\r
204 } catch (Exception e) {
\r
206 "Failed in Resource dictionary loading", e);
\r
210 private void loadBlueprints() {
\r
211 log.info("*************************** loadServiceTemplate **********************");
\r
213 List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(bluePrintsPath);
\r
214 if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {
\r
215 StrBuilder errorBuilder = new StrBuilder();
\r
216 for (String fileName : serviceTemplateDirs) {
\r
218 String bluePrintPath = this.bluePrintsPath.concat("/").concat(fileName);
\r
219 log.debug("***** Loading service template : {}", bluePrintPath);
\r
220 ConfigModel configModel = ConfigModelUtils.getConfigModel(bluePrintPath);
\r
222 configModel = this.configModelService.saveConfigModel(configModel);
\r
224 log.info("Publishing : {}", configModel.getId());
\r
226 this.configModelService.publishConfigModel(configModel.getId());
\r
228 log.info("Loaded service template successfully: {}", fileName);
\r
230 } catch (Exception e) {
\r
231 errorBuilder.appendln("load config model " + fileName + " error : " + e.getMessage());
\r
235 if (!errorBuilder.isEmpty()) {
\r
236 log.error(errorBuilder.toString());
\r
239 } catch (Exception e) {
\r
240 log.error("Failed in Service Template loading", e);
\r
244 private void loadNodeType(Resource file, StrBuilder errorBuilder) {
\r
246 log.trace("Loading Node Type : {}", file.getFilename());
\r
247 String nodeKey = file.getFilename().replace(".json", "");
\r
248 String definitionContent = getResourceContent(file);
\r
249 NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);
\r
250 Preconditions.checkNotNull(nodeType, String.format("failed to get node type from file : %s", file.getFilename()));
\r
251 ModelType modelType = new ModelType();
\r
252 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);
\r
253 modelType.setDerivedFrom(nodeType.getDerivedFrom());
\r
254 modelType.setDescription(nodeType.getDescription());
\r
255 modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));
\r
256 modelType.setModelName(nodeKey);
\r
257 modelType.setVersion(nodeType.getVersion());
\r
258 modelType.setUpdatedBy("System");
\r
259 modelType.setTags(nodeKey + "," + BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE + ","
\r
260 + nodeType.getDerivedFrom());
\r
261 modelTypeService.saveModel(modelType);
\r
262 log.trace("Loaded Node Type successfully : {}", file.getFilename());
\r
263 } catch (Exception e) {
\r
264 errorBuilder.appendln("Node type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
268 private void loadDataType(@NotNull Resource file, StrBuilder errorBuilder) {
\r
270 log.trace("Loading Data Type: {}", file.getFilename());
\r
271 String dataKey = file.getFilename().replace(".json", "");
\r
272 String definitionContent = getResourceContent(file);
\r
273 DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);
\r
274 Preconditions.checkNotNull(dataType, String.format("failed to get data type from file : %s", file.getFilename()));
\r
275 ModelType modelType = new ModelType();
\r
276 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
\r
277 modelType.setDerivedFrom(dataType.getDerivedFrom());
\r
278 modelType.setDescription(dataType.getDescription());
\r
279 modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));
\r
280 modelType.setModelName(dataKey);
\r
281 modelType.setVersion(dataType.getVersion());
\r
282 modelType.setUpdatedBy("System");
\r
283 modelType.setTags(dataKey + "," + dataType.getDerivedFrom() + ","
\r
284 + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
\r
285 modelTypeService.saveModel(modelType);
\r
286 log.trace(" Loaded Data Type successfully : {}", file.getFilename());
\r
287 } catch (Exception e) {
\r
288 errorBuilder.appendln("Data type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
292 private void loadArtifactType(Resource file, StrBuilder errorBuilder) {
\r
294 log.trace("Loading Artifact Type: {}", file.getFilename());
\r
295 String dataKey = file.getFilename().replace(".json", "");
\r
296 String definitionContent = getResourceContent(file);
\r
297 ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);
\r
298 Preconditions.checkNotNull(artifactType, String.format("failed to get artifact type from file : %s", file.getFilename()));
\r
299 ModelType modelType = new ModelType();
\r
300 modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);
\r
301 modelType.setDerivedFrom(artifactType.getDerivedFrom());
\r
302 modelType.setDescription(artifactType.getDescription());
\r
303 modelType.setDefinition(JacksonUtils.jsonNode(definitionContent));
\r
304 modelType.setModelName(dataKey);
\r
305 modelType.setVersion(artifactType.getVersion());
\r
306 modelType.setUpdatedBy("System");
\r
307 modelType.setTags(dataKey + "," + artifactType.getDerivedFrom() + ","
\r
308 + BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);
\r
309 modelTypeService.saveModel(modelType);
\r
310 log.trace("Loaded Artifact Type successfully : {}", file.getFilename());
\r
311 } catch (Exception e) {
\r
312 errorBuilder.appendln("Artifact type loading error : " + file.getFilename() + ":" + e.getMessage());
\r
316 private Resource[] getPathResources(String path, String extension) throws IOException {
\r
317 return resourceLoader.getResources("file:" + path + "/*" + extension);
\r
320 private String getResourceContent(Resource resource) throws IOException {
\r
321 return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
\r