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.jetbrains.annotations.NotNull;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;
\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModelContent;
\r
33 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;
\r
34 import com.att.eelf.configuration.EELFLogger;
\r
35 import com.att.eelf.configuration.EELFManager;
\r
36 import org.springframework.stereotype.Service;
\r
38 import java.io.IOException;
\r
39 import java.nio.charset.Charset;
\r
40 import java.util.ArrayList;
\r
41 import java.util.List;
\r
42 import java.util.Optional;
\r
45 * ServiceTemplateCreateService.java Purpose: Provide Service Template Create Service processing
\r
46 * ServiceTemplateCreateService
\r
48 * @author Brinda Santh
\r
53 public class ConfigModelCreateService {
\r
55 private static EELFLogger log = EELFManager.getInstance().getLogger(ConfigModelCreateService.class);
\r
57 private ConfigModelRepository configModelRepository;
\r
58 private ConfigModelValidatorService configModelValidatorService;
\r
61 * This is a ConfigModelCreateService
\r
63 * @param configModelRepository ConfigModelRepository
\r
64 * @param configModelValidatorService ConfigModelValidatorService
\r
66 public ConfigModelCreateService(ConfigModelRepository configModelRepository,
\r
67 ConfigModelValidatorService configModelValidatorService) {
\r
68 this.configModelRepository = configModelRepository;
\r
69 this.configModelValidatorService = configModelValidatorService;
\r
73 * This is a createInitialServiceTemplateContent method
\r
75 * @param templateName templateName
\r
77 * @throws BluePrintException BluePrintException
\r
79 public String createInitialServiceTemplateContent(String templateName) throws BluePrintException {
\r
80 String serviceTemplateContent = null;
\r
81 if (StringUtils.isNotBlank(templateName)) {
\r
83 serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()
\r
84 .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());
\r
85 } catch (IOException e) {
\r
86 throw new BluePrintException(e.getMessage(), e);
\r
90 return serviceTemplateContent;
\r
94 * This is a createInitialServiceTemplate method
\r
96 * @param templateName templateName
\r
97 * @return ServiceTemplate
\r
98 * @throws BluePrintException BluePrintException
\r
100 public ServiceTemplate createInitialServiceTemplate(String templateName) throws BluePrintException {
\r
101 ServiceTemplate serviceTemplate = null;
\r
102 if (StringUtils.isNotBlank(templateName)) {
\r
104 String serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()
\r
105 .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());
\r
106 if (StringUtils.isNotBlank(serviceTemplateContent)) {
\r
107 serviceTemplate = JacksonUtils.readValue(serviceTemplateContent, ServiceTemplate.class);
\r
109 } catch (IOException e) {
\r
110 throw new BluePrintException(e.getMessage(), e);
\r
114 return serviceTemplate;
\r
118 * This is a saveConfigModel method
\r
120 * @param configModel configModel
\r
121 * @return ConfigModel
\r
122 * @throws BluePrintException BluePrintException
\r
124 public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {
\r
126 if (configModel != null) {
\r
127 String artifactName = configModel.getArtifactName();
\r
128 String artifactVersion = configModel.getArtifactVersion();
\r
129 String author = configModel.getUpdatedBy();
\r
130 // configModel.setTags(artifactName);
\r
132 if (StringUtils.isBlank(author)) {
\r
133 throw new BluePrintException("Artifact Author is missing in the Service Template");
\r
136 if (StringUtils.isBlank(artifactName)) {
\r
137 throw new BluePrintException("Artifact Name is missing in the Service Template");
\r
140 if (StringUtils.isBlank(artifactVersion)) {
\r
141 throw new BluePrintException("Artifact Version is missing in the Service Template");
\r
143 ConfigModel updateConfigModel;
\r
145 Optional<ConfigModel> dbConfigModelOptional = Optional.empty();
\r
147 if (configModel.getId() != null) {
\r
148 log.info("Searching for config model id : {}", configModel.getId());
\r
149 dbConfigModelOptional = configModelRepository.findById(configModel.getId());
\r
152 if (!dbConfigModelOptional.isPresent()) {
\r
153 log.info("Searching for config model name :"
\r
154 + configModel.getArtifactName() + ", version " + configModel.getArtifactVersion());
\r
155 dbConfigModelOptional = configModelRepository.findByArtifactNameAndArtifactVersion(
\r
156 configModel.getArtifactName(), configModel.getArtifactVersion());
\r
159 if (dbConfigModelOptional.isPresent()) {
\r
160 updateConfigModel = dbConfigModelOptional.get();
\r
161 log.info("Processing for config model id : {} with config model content count : {}"
\r
162 , updateConfigModel.getId(), updateConfigModel.getConfigModelContents().size());
\r
164 ConfigModel tempConfigModel = new ConfigModel();
\r
165 tempConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
166 tempConfigModel.setArtifactName(artifactName);
\r
167 tempConfigModel.setArtifactVersion(artifactVersion);
\r
168 tempConfigModel.setUpdatedBy(author);
\r
169 tempConfigModel.setPublished(ApplicationConstants.ACTIVE_N);
\r
170 tempConfigModel.setTags(artifactName);
\r
171 configModelRepository.saveAndFlush(tempConfigModel);
\r
172 updateConfigModel = tempConfigModel;
\r
175 Long dbConfigModelId = updateConfigModel.getId();
\r
177 if (dbConfigModelId == null) {
\r
178 throw new BluePrintException("failed to get the initial saved config model id.");
\r
181 log.info("Processing for config model id : {}", dbConfigModelId);
\r
183 deleteConfigModelContent(dbConfigModelId);
\r
185 addConfigModelContent(dbConfigModelId, configModel);
\r
187 // Populate Content model types
\r
188 updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author);
\r
191 return updateConfigModel;
\r
193 throw new BluePrintException("Config model information is missing");
\r
198 private void deleteConfigModelContent(Long dbConfigModelId) {
\r
199 if (dbConfigModelId != null) {
\r
200 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
201 if (CollectionUtils.isNotEmpty(dbConfigModel.getConfigModelContents())) {
\r
202 dbConfigModel.getConfigModelContents().clear();
\r
203 log.debug("Configuration Model content deleting : {}", dbConfigModel.getConfigModelContents());
\r
204 configModelRepository.saveAndFlush(dbConfigModel);
\r
210 private void addConfigModelContent(Long dbConfigModelId, ConfigModel configModel) {
\r
211 if (dbConfigModelId != null && configModel != null
\r
212 && CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {
\r
213 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
214 for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {
\r
215 if (configModelContent != null) {
\r
216 configModelContent.setId(null);
\r
217 configModelContent.setConfigModel(dbConfigModel);
\r
218 dbConfigModel.getConfigModelContents().add(configModelContent);
\r
219 log.debug("Configuration Model content adding : {}", configModelContent);
\r
222 configModelRepository.saveAndFlush(dbConfigModel);
\r
226 private ConfigModel updateConfigModel(Long dbConfigModelId, String artifactName, String artifactVersion,
\r
227 String author) throws BluePrintException {
\r
229 ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);
\r
230 // Populate tags from metadata
\r
231 String tags = getConfigModelTags(dbConfigModel);
\r
232 if (StringUtils.isBlank(tags)) {
\r
233 throw new BluePrintException("Failed to populate tags for the config model name " + artifactName);
\r
235 dbConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
236 dbConfigModel.setArtifactName(artifactName);
\r
237 dbConfigModel.setArtifactVersion(artifactVersion);
\r
238 dbConfigModel.setUpdatedBy(author);
\r
239 dbConfigModel.setPublished(ApplicationConstants.ACTIVE_N);
\r
240 dbConfigModel.setTags(tags);
\r
241 configModelRepository.saveAndFlush(dbConfigModel);
\r
242 log.info("Config model ({}) saved successfully.", dbConfigModel.getId());
\r
243 return dbConfigModel;
\r
246 private List<String> getValidContentTypes() {
\r
247 List<String> valids = new ArrayList<>();
\r
248 valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);
\r
249 valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE);
\r
254 private String getConfigModelTags(ConfigModel configModel) throws BluePrintException {
\r
255 String tags = null;
\r
256 if (CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {
\r
258 for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {
\r
259 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {
\r
261 if (!getValidContentTypes().contains(configModelContent.getContentType())) {
\r
262 throw new BluePrintException(configModelContent.getContentType()
\r
263 + " is not a valid content type, It should be any one of this "
\r
264 + getValidContentTypes());
\r
267 if (configModelContent.getContentType().equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {
\r
268 ServiceTemplate serviceTemplate =
\r
269 JacksonUtils.readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
270 Preconditions.checkNotNull(serviceTemplate, "failed to transform service template content");
\r
271 if (serviceTemplate.getMetadata() != null) {
\r
272 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_AUTHOR,
\r
273 configModel.getUpdatedBy());
\r
274 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_VERSION,
\r
275 configModel.getArtifactVersion());
\r
276 serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_NAME,
\r
277 configModel.getArtifactName());
\r
279 tags = String.valueOf(serviceTemplate.getMetadata());
\r
288 * This is a publishConfigModel method
\r
291 * @return ConfigModel
\r
292 * @throws BluePrintException BluePrintException
\r
294 public ConfigModel publishConfigModel(@NotNull Long id) throws BluePrintException {
\r
295 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
316 throw new BluePrintException(String.format("Couldn't get Config model for id :(%s)", id));
\r
318 return dbConfigModel;
\r
322 * This is a validateServiceTemplate method
\r
324 * @param serviceTemplate Service Template
\r
325 * @return ServiceTemplate
\r
326 * @throws BluePrintException BluePrintException
\r
328 public ServiceTemplate validateServiceTemplate(ServiceTemplate serviceTemplate) throws BluePrintException {
\r
329 return this.configModelValidatorService.validateServiceTemplate(serviceTemplate);
\r