2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.service;
\r
19 import com.google.common.base.Preconditions;
\r
20 import org.apache.commons.collections.CollectionUtils;
\r
21 import org.apache.commons.io.IOUtils;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModelContent;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
34 import org.springframework.stereotype.Service;
\r
36 import java.io.IOException;
\r
37 import java.nio.charset.Charset;
\r
38 import java.util.ArrayList;
\r
39 import java.util.List;
\r
40 import java.util.Optional;
\r
43 * ServiceTemplateCreateService.java Purpose: Provide Service Template Create Service processing
\r
44 * ServiceTemplateCreateService
\r
46 * @author Brinda Santh
\r
51 public class ConfigModelCreateService {
\r
53 private static Logger log = LoggerFactory.getLogger(ConfigModelCreateService.class);
\r
55 private ConfigModelRepository configModelRepository;
\r
56 private ConfigModelValidatorService configModelValidatorService;
\r
59 * This is a ConfigModelCreateService
\r
61 * @param configModelRepository ConfigModelRepository
\r
62 * @param configModelValidatorService ConfigModelValidatorService
\r
64 public ConfigModelCreateService(ConfigModelRepository configModelRepository,
\r
65 ConfigModelValidatorService configModelValidatorService) {
\r
66 this.configModelRepository = configModelRepository;
\r
67 this.configModelValidatorService = configModelValidatorService;
\r
71 * This is a createInitialServiceTemplateContent method
\r
73 * @param templateName templateName
\r
75 * @throws BluePrintException BluePrintException
\r
77 public String createInitialServiceTemplateContent(String templateName) throws BluePrintException {
\r
78 String serviceTemplateContent = null;
\r
79 if (StringUtils.isNotBlank(templateName)) {
\r
81 serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()
\r
82 .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());
\r
83 } catch (IOException e) {
\r
84 throw new BluePrintException(e.getMessage(), e);
\r
88 return serviceTemplateContent;
\r
92 * This is a createInitialServiceTemplate method
\r
94 * @param templateName templateName
\r
95 * @return ServiceTemplate
\r
96 * @throws BluePrintException BluePrintException
\r
98 public ServiceTemplate createInitialServiceTemplate(String templateName) throws BluePrintException {
\r
99 ServiceTemplate serviceTemplate = null;
\r
100 if (StringUtils.isNotBlank(templateName)) {
\r
102 String serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()
\r
103 .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());
\r
104 if (StringUtils.isNotBlank(serviceTemplateContent)) {
\r
105 serviceTemplate = JacksonUtils.readValue(serviceTemplateContent, ServiceTemplate.class);
\r
107 } catch (IOException e) {
\r
108 throw new BluePrintException(e.getMessage(), e);
\r
112 return serviceTemplate;
\r
116 * This is a saveConfigModel method
\r
118 * @param configModel configModel
\r
119 * @return ConfigModel
\r
120 * @throws BluePrintException BluePrintException
\r
122 public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {
\r
124 if (configModel != null) {
\r
125 String artifactName = configModel.getArtifactName();
\r
126 String artifactVersion = configModel.getArtifactVersion();
\r
127 String author = configModel.getUpdatedBy();
\r
128 // configModel.setTags(artifactName);
\r
130 if (StringUtils.isBlank(author)) {
\r
131 throw new BluePrintException("Artifact Author is missing in the Service Template");
\r
134 if (StringUtils.isBlank(artifactName)) {
\r
135 throw new BluePrintException("Artifact Name is missing in the Service Template");
\r
138 if (StringUtils.isBlank(artifactVersion)) {
\r
139 throw new BluePrintException("Artifact Version is missing in the Service Template");
\r
141 ConfigModel updateConfigModel = null;
\r
143 Optional<ConfigModel> dbConfigModelOptional = Optional.empty();
\r
145 if (configModel.getId() != null) {
\r
146 log.info("Searching for config model id : {}", configModel.getId());
\r
147 dbConfigModelOptional = configModelRepository.findById(configModel.getId());
\r
150 if (!dbConfigModelOptional.isPresent()) {
\r
151 log.info("Searching for config model name :"
\r
152 + configModel.getArtifactName() + ", version " + configModel.getArtifactVersion());
\r
153 dbConfigModelOptional = configModelRepository.findByArtifactNameAndArtifactVersion(
\r
154 configModel.getArtifactName(), configModel.getArtifactVersion());
\r
157 if (dbConfigModelOptional.isPresent()) {
\r
158 updateConfigModel = dbConfigModelOptional.get();
\r
159 log.info("Processing for config model id : {} with config model content count : {}"
\r
160 , updateConfigModel.getId(), updateConfigModel.getConfigModelContents().size());
\r
162 ConfigModel tempConfigModel = new ConfigModel();
\r
163 tempConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
164 tempConfigModel.setArtifactName(artifactName);
\r
165 tempConfigModel.setArtifactVersion(artifactVersion);
\r
166 tempConfigModel.setUpdatedBy(author);
\r
167 tempConfigModel.setPublished(ApplicationConstants.ACTIVE_N);
\r
168 tempConfigModel.setTags(artifactName);
\r
169 configModelRepository.saveAndFlush(tempConfigModel);
\r
170 updateConfigModel = tempConfigModel;
\r
173 Long dbConfigModelId = updateConfigModel.getId();
\r
175 if (dbConfigModelId == null) {
\r
176 throw new BluePrintException("failed to get the initial saved config model id.");
\r
179 log.info("Processing for config model id : {}", dbConfigModelId);
\r
181 deleteConfigModelContent(dbConfigModelId);
\r
183 addConfigModelContent(dbConfigModelId, configModel);
\r
185 // Populate Content model types
\r
186 updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author);
\r
189 return updateConfigModel;
\r
191 throw new BluePrintException("Config model information is missing");
\r
196 private void deleteConfigModelContent(Long dbConfigModelId) {
\r
197 if (dbConfigModelId != null) {
\r
198 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
199 if (dbConfigModel != null && CollectionUtils.isNotEmpty(dbConfigModel.getConfigModelContents())) {
\r
200 dbConfigModel.getConfigModelContents().clear();
\r
201 log.debug("Configuration Model content deleting : {}", dbConfigModel.getConfigModelContents());
\r
202 configModelRepository.saveAndFlush(dbConfigModel);
\r
208 private void addConfigModelContent(Long dbConfigModelId, ConfigModel configModel) {
\r
209 if (dbConfigModelId != null && configModel != null
\r
210 && CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {
\r
211 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
212 if (dbConfigModel != null) {
\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
227 private ConfigModel updateConfigModel(Long dbConfigModelId, String artifactName, String artifactVersion,
\r
228 String author) throws BluePrintException {
\r
230 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
231 if (dbConfigModel != null) {
\r
232 // Populate tags from metadata
\r
233 String tags = getConfigModelTags(dbConfigModel);
\r
234 if (StringUtils.isBlank(tags)) {
\r
235 throw new BluePrintException("Failed to populate tags for the config model name " + artifactName);
\r
237 dbConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
238 dbConfigModel.setArtifactName(artifactName);
\r
239 dbConfigModel.setArtifactVersion(artifactVersion);
\r
240 dbConfigModel.setUpdatedBy(author);
\r
241 dbConfigModel.setPublished(ApplicationConstants.ACTIVE_N);
\r
242 dbConfigModel.setTags(tags);
\r
243 configModelRepository.saveAndFlush(dbConfigModel);
\r
245 log.info("Config model ({}) saved successfully.", dbConfigModel.getId());
\r
247 return dbConfigModel;
\r
250 private List<String> getValidContentTypes() {
\r
251 List<String> valids = new ArrayList<>();
\r
252 valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);
\r
253 valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE);
\r
258 private String getConfigModelTags(ConfigModel configModel) throws BluePrintException {
\r
259 String tags = null;
\r
260 if (CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {
\r
262 for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {
\r
263 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {
\r
265 if (!getValidContentTypes().contains(configModelContent.getContentType())) {
\r
266 throw new BluePrintException(configModelContent.getContentType()
\r
267 + " is not a valid content type, It should be any one of this "
\r
268 + getValidContentTypes());
\r
271 if (configModelContent.getContentType().equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {
\r
272 ServiceTemplate serviceTemplate =
\r
273 JacksonUtils.readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
274 Preconditions.checkNotNull(serviceTemplate, "failed to transform service template content");
\r
275 if (serviceTemplate.getMetadata() != null) {
\r
276 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_AUTHOR,
\r
277 configModel.getUpdatedBy());
\r
278 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_VERSION,
\r
279 configModel.getArtifactVersion());
\r
280 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_NAME,
\r
281 configModel.getArtifactName());
\r
283 tags = String.valueOf(serviceTemplate.getMetadata());
\r
294 * This is a publishConfigModel method
\r
297 * @return ConfigModel
\r
298 * @throws BluePrintException BluePrintException
\r
300 public ConfigModel publishConfigModel(Long id) throws BluePrintException {
\r
301 ConfigModel dbConfigModel = null;
\r
303 Optional<ConfigModel> dbConfigModelOptional = configModelRepository.findById(id);
\r
304 if (dbConfigModelOptional.isPresent()) {
\r
305 dbConfigModel = dbConfigModelOptional.get();
\r
306 List<ConfigModelContent> configModelContents = dbConfigModel.getConfigModelContents();
\r
307 if (configModelContents != null && !configModelContents.isEmpty()) {
\r
308 for (ConfigModelContent configModelContent : configModelContents) {
\r
309 if (configModelContent.getContentType()
\r
310 .equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {
\r
311 ServiceTemplate serviceTemplate = JacksonUtils
\r
312 .readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
313 if (serviceTemplate != null) {
\r
314 validateServiceTemplate(serviceTemplate);
\r
319 dbConfigModel.setPublished(ApplicationConstants.ACTIVE_Y);
\r
320 configModelRepository.save(dbConfigModel);
\r
321 log.info("Config model ({}) published successfully.", id);
\r
326 return dbConfigModel;
\r
330 * This is a validateServiceTemplate method
\r
332 * @param serviceTemplate Service Template
\r
333 * @return ServiceTemplate
\r
334 * @throws BluePrintException BluePrintException
\r
336 public ServiceTemplate validateServiceTemplate(ServiceTemplate serviceTemplate) throws BluePrintException {
\r
337 return this.configModelValidatorService.validateServiceTemplate(serviceTemplate);
\r