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 org.apache.commons.collections.CollectionUtils;
\r
21 import org.apache.commons.lang3.StringUtils;
\r
22 import org.jetbrains.annotations.NotNull;
\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.ConfigModelContentRepository;
\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;
\r
33 import com.att.eelf.configuration.EELFLogger;
\r
34 import com.att.eelf.configuration.EELFManager;
\r
35 import org.springframework.stereotype.Service;
\r
36 import org.springframework.transaction.annotation.Transactional;
\r
38 import java.util.ArrayList;
\r
39 import java.util.Date;
\r
40 import java.util.List;
\r
41 import java.util.Optional;
\r
44 * ConfigModelService.java Purpose: Provide Service Template Service processing ConfigModelService
\r
46 * @author Brinda Santh
\r
51 public class ConfigModelService {
\r
53 private static EELFLogger log = EELFManager.getInstance().getLogger(ConfigModelService.class);
\r
55 private ConfigModelRepository configModelRepository;
\r
56 private ConfigModelContentRepository configModelContentRepository;
\r
57 private ConfigModelCreateService configModelCreateService;
\r
60 * This is a ConfigModelService constructor.
\r
62 * @param configModelRepository configModelRepository
\r
63 * @param configModelContentRepository configModelContentRepository
\r
64 * @param configModelCreateService configModelCreateService
\r
66 public ConfigModelService(ConfigModelRepository configModelRepository,
\r
67 ConfigModelContentRepository configModelContentRepository,
\r
68 ConfigModelCreateService configModelCreateService) {
\r
69 this.configModelRepository = configModelRepository;
\r
70 this.configModelContentRepository = configModelContentRepository;
\r
71 this.configModelCreateService = configModelCreateService;
\r
72 log.info("Config Model Service Initiated...");
\r
76 * This is a getInitialConfigModel method
\r
78 * @param templateName templateName
\r
79 * @return ConfigModel
\r
80 * @throws BluePrintException BluePrintException
\r
82 public ConfigModel getInitialConfigModel(String templateName) throws BluePrintException {
\r
83 ConfigModel configModel = null;
\r
84 if (StringUtils.isNotBlank(templateName)) {
\r
85 configModel = new ConfigModel();
\r
86 configModel.setArtifactName(templateName);
\r
87 configModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
88 configModel.setUpdatedBy("xxxxx@xxx.com");
\r
89 ConfigModelContent configModelContent = new ConfigModelContent();
\r
90 configModelContent.setContentType(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);
\r
91 configModelContent.setName(templateName);
\r
92 String content = this.configModelCreateService.createInitialServiceTemplateContent(templateName);
\r
93 configModelContent.setContent(content);
\r
95 List<ConfigModelContent> configModelContents = new ArrayList<>();
\r
96 configModelContents.add(configModelContent);
\r
98 configModel.setConfigModelContents(configModelContents);
\r
100 return configModel;
\r
104 * This is a saveConfigModel method
\r
106 * @param configModel configModel
\r
107 * @return ConfigModel
\r
108 * @throws BluePrintException BluePrintException
\r
110 public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {
\r
111 return this.configModelCreateService.saveConfigModel(configModel);
\r
115 * This is a publishConfigModel method
\r
118 * @return ConfigModel
\r
119 * @throws BluePrintException BluePrintException
\r
121 public ConfigModel publishConfigModel(Long id) throws BluePrintException {
\r
122 return this.configModelCreateService.publishConfigModel(id);
\r
126 * This is a searchConfigModels method
\r
129 * @return ConfigModel
\r
131 public List<ConfigModel> searchConfigModels(String tags) {
\r
132 List<ConfigModel> models = configModelRepository.findByTagsContainingIgnoreCase(tags);
\r
133 if (models != null) {
\r
134 for (ConfigModel configModel : models) {
\r
135 configModel.setConfigModelContents(null);
\r
142 * This is a getConfigModelByNameAndVersion method
\r
145 * @param version version
\r
146 * @return ConfigModel
\r
148 public ConfigModel getConfigModelByNameAndVersion(@NotNull String name, String version) throws BluePrintException {
\r
149 ConfigModel configModel;
\r
150 Optional<ConfigModel> dbConfigModel;
\r
151 if (StringUtils.isNotBlank(version)) {
\r
152 dbConfigModel = configModelRepository.findByArtifactNameAndArtifactVersion(name, version);
\r
154 dbConfigModel = configModelRepository.findTopByArtifactNameOrderByArtifactVersionDesc(name);
\r
156 if (dbConfigModel.isPresent()) {
\r
157 configModel = dbConfigModel.get();
\r
159 throw new BluePrintException(String.format("failed to get config model name(%s), version(%s) from repo", name, version));
\r
161 return configModel;
\r
165 * This is a getConfigModel method
\r
168 * @return ConfigModel
\r
169 * @throws BluePrintException BluePrintException
\r
171 public ConfigModel getConfigModel(@NotNull Long id) throws BluePrintException {
\r
172 ConfigModel configModel;
\r
173 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
174 if (dbConfigModel.isPresent()) {
\r
175 configModel = dbConfigModel.get();
\r
177 throw new BluePrintException(String.format("failed to get config model id(%d) from repo", id));
\r
180 return configModel;
\r
184 * This method returns clone of the given model id, by masking the other unrelated fields
\r
187 * @return ConfigModel
\r
188 * @throws BluePrintException BluePrintException
\r
191 public ConfigModel getCloneConfigModel(@NotNull Long id) throws BluePrintException {
\r
193 ConfigModel configModel;
\r
194 ConfigModel cloneConfigModel;
\r
195 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
196 if (dbConfigModel.isPresent()) {
\r
197 configModel = dbConfigModel.get();
\r
198 cloneConfigModel = configModel;
\r
199 cloneConfigModel.setUpdatedBy("xxxxx@xxx.com");
\r
200 cloneConfigModel.setArtifactName("XXXX");
\r
201 cloneConfigModel.setPublished("XXXX");
\r
202 cloneConfigModel.setPublished("XXXX");
\r
203 cloneConfigModel.setUpdatedBy("XXXX");
\r
204 cloneConfigModel.setId(null);
\r
205 cloneConfigModel.setTags(null);
\r
206 cloneConfigModel.setCreatedDate(new Date());
\r
207 List<ConfigModelContent> configModelContents = cloneConfigModel.getConfigModelContents();
\r
209 if (CollectionUtils.isNotEmpty(configModelContents)) {
\r
210 for (ConfigModelContent configModelContent : configModelContents) {
\r
211 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {
\r
212 configModelContent.setId(null);
\r
213 configModelContent.setCreationDate(new Date());
\r
215 if (ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON
\r
216 .equalsIgnoreCase(configModelContent.getContentType())) {
\r
217 ServiceTemplate serviceTemplate = JacksonUtils
\r
218 .readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
219 if (serviceTemplate != null && serviceTemplate.getMetadata() != null) {
\r
220 serviceTemplate.getMetadata()
\r
221 .put(BluePrintConstants.METADATA_TEMPLATE_AUTHOR, "XXXX");
\r
222 serviceTemplate.getMetadata()
\r
223 .put(BluePrintConstants.METADATA_TEMPLATE_VERSION, "1.0.0");
\r
224 serviceTemplate.getMetadata()
\r
225 .put(BluePrintConstants.METADATA_TEMPLATE_NAME, "XXXXXX");
\r
227 configModelContent.setContent(JacksonUtils.getJson(serviceTemplate));
\r
235 throw new BluePrintException(String.format("failed to get config model id(%d) from repo", id));
\r
238 return cloneConfigModel;
\r
242 * This is a deleteConfigModel method
\r
245 * @throws BluePrintException BluePrintException
\r
249 public void deleteConfigModel(@NotNull Long id) throws BluePrintException {
\r
250 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
251 if (dbConfigModel.isPresent()) {
\r
252 configModelContentRepository.deleteByConfigModel(dbConfigModel.get());
\r
253 configModelRepository.delete(dbConfigModel.get());
\r
255 throw new BluePrintException(String.format("failed to get config model id(%d) from repo", id));
\r