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.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModelContent;
\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;
\r
33 import org.slf4j.Logger;
\r
34 import org.slf4j.LoggerFactory;
\r
35 import org.springframework.stereotype.Service;
\r
37 import java.io.IOException;
\r
38 import java.nio.charset.Charset;
\r
39 import java.util.ArrayList;
\r
40 import java.util.List;
\r
41 import java.util.Optional;
\r
44 * ServiceTemplateCreateService.java Purpose: Provide Service Template Create Service processing
\r
45 * ServiceTemplateCreateService
\r
47 * @author Brinda Santh
\r
52 public class ConfigModelCreateService {
\r
54 private static Logger log = LoggerFactory.getLogger(ConfigModelCreateService.class);
\r
56 private ConfigModelRepository configModelRepository;
\r
57 private ConfigModelValidatorService configModelValidatorService;
\r
60 * This is a ConfigModelCreateService
\r
62 * @param configModelRepository ConfigModelRepository
\r
63 * @param configModelValidatorService ConfigModelValidatorService
\r
65 public ConfigModelCreateService(ConfigModelRepository configModelRepository,
\r
66 ConfigModelValidatorService configModelValidatorService) {
\r
67 this.configModelRepository = configModelRepository;
\r
68 this.configModelValidatorService = configModelValidatorService;
\r
72 * This is a createInitialServiceTemplateContent method
\r
74 * @param templateName templateName
\r
76 * @throws BluePrintException BluePrintException
\r
78 public String createInitialServiceTemplateContent(String templateName) throws BluePrintException {
\r
79 String serviceTemplateContent = null;
\r
80 if (StringUtils.isNotBlank(templateName)) {
\r
82 serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()
\r
83 .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());
\r
84 } catch (IOException e) {
\r
85 throw new BluePrintException(e.getMessage(), e);
\r
89 return serviceTemplateContent;
\r
93 * This is a createInitialServiceTemplate method
\r
95 * @param templateName templateName
\r
96 * @return ServiceTemplate
\r
97 * @throws BluePrintException BluePrintException
\r
99 public ServiceTemplate createInitialServiceTemplate(String templateName) throws BluePrintException {
\r
100 ServiceTemplate serviceTemplate = null;
\r
101 if (StringUtils.isNotBlank(templateName)) {
\r
103 String serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()
\r
104 .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());
\r
105 if (StringUtils.isNotBlank(serviceTemplateContent)) {
\r
106 serviceTemplate = JacksonUtils.readValue(serviceTemplateContent, ServiceTemplate.class);
\r
108 } catch (IOException e) {
\r
109 throw new BluePrintException(e.getMessage(), e);
\r
113 return serviceTemplate;
\r
117 * This is a saveConfigModel method
\r
119 * @param configModel configModel
\r
120 * @return ConfigModel
\r
121 * @throws BluePrintException BluePrintException
\r
123 public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {
\r
125 if (configModel != null) {
\r
126 String artifactName = configModel.getArtifactName();
\r
127 String artifactVersion = configModel.getArtifactVersion();
\r
128 String author = configModel.getUpdatedBy();
\r
129 // configModel.setTags(artifactName);
\r
131 if (StringUtils.isBlank(author)) {
\r
132 throw new BluePrintException("Artifact Author is missing in the Service Template");
\r
135 if (StringUtils.isBlank(artifactName)) {
\r
136 throw new BluePrintException("Artifact Name is missing in the Service Template");
\r
139 if (StringUtils.isBlank(artifactVersion)) {
\r
140 throw new BluePrintException("Artifact Version is missing in the Service Template");
\r
142 ConfigModel updateConfigModel;
\r
144 Optional<ConfigModel> dbConfigModelOptional = Optional.empty();
\r
146 if (configModel.getId() != null) {
\r
147 log.info("Searching for config model id : {}", configModel.getId());
\r
148 dbConfigModelOptional = configModelRepository.findById(configModel.getId());
\r
151 if (!dbConfigModelOptional.isPresent()) {
\r
152 log.info("Searching for config model name :"
\r
153 + configModel.getArtifactName() + ", version " + configModel.getArtifactVersion());
\r
154 dbConfigModelOptional = configModelRepository.findByArtifactNameAndArtifactVersion(
\r
155 configModel.getArtifactName(), configModel.getArtifactVersion());
\r
158 if (dbConfigModelOptional.isPresent()) {
\r
159 updateConfigModel = dbConfigModelOptional.get();
\r
160 log.info("Processing for config model id : {} with config model content count : {}"
\r
161 , updateConfigModel.getId(), updateConfigModel.getConfigModelContents().size());
\r
163 ConfigModel tempConfigModel = new ConfigModel();
\r
164 tempConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
165 tempConfigModel.setArtifactName(artifactName);
\r
166 tempConfigModel.setArtifactVersion(artifactVersion);
\r
167 tempConfigModel.setUpdatedBy(author);
\r
168 tempConfigModel.setPublished(ApplicationConstants.ACTIVE_N);
\r
169 tempConfigModel.setTags(artifactName);
\r
170 configModelRepository.saveAndFlush(tempConfigModel);
\r
171 updateConfigModel = tempConfigModel;
\r
174 Long dbConfigModelId = updateConfigModel.getId();
\r
176 if (dbConfigModelId == null) {
\r
177 throw new BluePrintException("failed to get the initial saved config model id.");
\r
180 log.info("Processing for config model id : {}", dbConfigModelId);
\r
182 deleteConfigModelContent(dbConfigModelId);
\r
184 addConfigModelContent(dbConfigModelId, configModel);
\r
186 // Populate Content model types
\r
187 updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author);
\r
190 return updateConfigModel;
\r
192 throw new BluePrintException("Config model information is missing");
\r
197 private void deleteConfigModelContent(Long dbConfigModelId) {
\r
198 if (dbConfigModelId != null) {
\r
199 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
200 if (CollectionUtils.isNotEmpty(dbConfigModel.getConfigModelContents())) {
\r
201 dbConfigModel.getConfigModelContents().clear();
\r
202 log.debug("Configuration Model content deleting : {}", dbConfigModel.getConfigModelContents());
\r
203 configModelRepository.saveAndFlush(dbConfigModel);
\r
209 private void addConfigModelContent(Long dbConfigModelId, ConfigModel configModel) {
\r
210 if (dbConfigModelId != null && configModel != null
\r
211 && CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {
\r
212 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
213 for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {
\r
214 if (configModelContent != null) {
\r
215 configModelContent.setId(null);
\r
216 configModelContent.setConfigModel(dbConfigModel);
\r
217 dbConfigModel.getConfigModelContents().add(configModelContent);
\r
218 log.debug("Configuration Model content adding : {}", configModelContent);
\r
221 configModelRepository.saveAndFlush(dbConfigModel);
\r
225 private ConfigModel updateConfigModel(Long dbConfigModelId, String artifactName, String artifactVersion,
\r
226 String author) throws BluePrintException {
\r
228 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
229 // Populate tags from metadata
\r
230 String tags = getConfigModelTags(dbConfigModel);
\r
231 if (StringUtils.isBlank(tags)) {
\r
232 throw new BluePrintException("Failed to populate tags for the config model name " + artifactName);
\r
234 dbConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
235 dbConfigModel.setArtifactName(artifactName);
\r
236 dbConfigModel.setArtifactVersion(artifactVersion);
\r
237 dbConfigModel.setUpdatedBy(author);
\r
238 dbConfigModel.setPublished(ApplicationConstants.ACTIVE_N);
\r
239 dbConfigModel.setTags(tags);
\r
240 configModelRepository.saveAndFlush(dbConfigModel);
\r
241 log.info("Config model ({}) saved successfully.", dbConfigModel.getId());
\r
242 return dbConfigModel;
\r
245 private List<String> getValidContentTypes() {
\r
246 List<String> valids = new ArrayList<>();
\r
247 valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);
\r
248 valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE);
\r
253 private String getConfigModelTags(ConfigModel configModel) throws BluePrintException {
\r
254 String tags = null;
\r
255 if (CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {
\r
257 for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {
\r
258 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {
\r
260 if (!getValidContentTypes().contains(configModelContent.getContentType())) {
\r
261 throw new BluePrintException(configModelContent.getContentType()
\r
262 + " is not a valid content type, It should be any one of this "
\r
263 + getValidContentTypes());
\r
266 if (configModelContent.getContentType().equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {
\r
267 ServiceTemplate serviceTemplate =
\r
268 JacksonUtils.readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
269 Preconditions.checkNotNull(serviceTemplate, "failed to transform service template content");
\r
270 if (serviceTemplate.getMetadata() != null) {
\r
271 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_AUTHOR,
\r
272 configModel.getUpdatedBy());
\r
273 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_VERSION,
\r
274 configModel.getArtifactVersion());
\r
275 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_NAME,
\r
276 configModel.getArtifactName());
\r
278 tags = String.valueOf(serviceTemplate.getMetadata());
\r
287 * This is a publishConfigModel method
\r
290 * @return ConfigModel
\r
291 * @throws BluePrintException BluePrintException
\r
293 public ConfigModel publishConfigModel(Long id) throws BluePrintException {
\r
294 ConfigModel dbConfigModel = null;
\r
296 Optional<ConfigModel> dbConfigModelOptional = configModelRepository.findById(id);
\r
297 if (dbConfigModelOptional.isPresent()) {
\r
298 dbConfigModel = dbConfigModelOptional.get();
\r
299 List<ConfigModelContent> configModelContents = dbConfigModel.getConfigModelContents();
\r
300 if (configModelContents != null && !configModelContents.isEmpty()) {
\r
301 for (ConfigModelContent configModelContent : configModelContents) {
\r
302 if (configModelContent.getContentType()
\r
303 .equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {
\r
304 ServiceTemplate serviceTemplate = JacksonUtils
\r
305 .readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
306 if (serviceTemplate != null) {
\r
307 validateServiceTemplate(serviceTemplate);
\r
312 dbConfigModel.setPublished(ApplicationConstants.ACTIVE_Y);
\r
313 configModelRepository.save(dbConfigModel);
\r
314 log.info("Config model ({}) published successfully.", id);
\r
319 return dbConfigModel;
\r
323 * This is a validateServiceTemplate method
\r
325 * @param serviceTemplate Service Template
\r
326 * @return ServiceTemplate
\r
327 * @throws BluePrintException BluePrintException
\r
329 public ServiceTemplate validateServiceTemplate(ServiceTemplate serviceTemplate) throws BluePrintException {
\r
330 return this.configModelValidatorService.validateServiceTemplate(serviceTemplate);
\r