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
58 private static final String CONFIG_MODEL_ID_FAILURE_MSG= "failed to get config model id(%d) from repo";
\r
61 * This is a ConfigModelService constructor.
\r
63 * @param configModelRepository configModelRepository
\r
64 * @param configModelContentRepository configModelContentRepository
\r
65 * @param configModelCreateService configModelCreateService
\r
67 public ConfigModelService(ConfigModelRepository configModelRepository,
\r
68 ConfigModelContentRepository configModelContentRepository,
\r
69 ConfigModelCreateService configModelCreateService) {
\r
70 this.configModelRepository = configModelRepository;
\r
71 this.configModelContentRepository = configModelContentRepository;
\r
72 this.configModelCreateService = configModelCreateService;
\r
73 log.info("Config Model Service Initiated...");
\r
77 * This is a getInitialConfigModel method
\r
79 * @param templateName templateName
\r
80 * @return ConfigModel
\r
81 * @throws BluePrintException BluePrintException
\r
83 public ConfigModel getInitialConfigModel(String templateName) throws BluePrintException {
\r
84 ConfigModel configModel = null;
\r
85 if (StringUtils.isNotBlank(templateName)) {
\r
86 configModel = new ConfigModel();
\r
87 configModel.setArtifactName(templateName);
\r
88 configModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);
\r
89 configModel.setUpdatedBy("xxxxx@xxx.com");
\r
90 ConfigModelContent configModelContent = new ConfigModelContent();
\r
91 configModelContent.setContentType(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);
\r
92 configModelContent.setName(templateName);
\r
93 String content = this.configModelCreateService.createInitialServiceTemplateContent(templateName);
\r
94 configModelContent.setContent(content);
\r
96 List<ConfigModelContent> configModelContents = new ArrayList<>();
\r
97 configModelContents.add(configModelContent);
\r
99 configModel.setConfigModelContents(configModelContents);
\r
101 return configModel;
\r
105 * This is a saveConfigModel method
\r
107 * @param configModel configModel
\r
108 * @return ConfigModel
\r
109 * @throws BluePrintException BluePrintException
\r
111 public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {
\r
112 return this.configModelCreateService.saveConfigModel(configModel);
\r
116 * This is a publishConfigModel method
\r
119 * @return ConfigModel
\r
120 * @throws BluePrintException BluePrintException
\r
122 public ConfigModel publishConfigModel(Long id) throws BluePrintException {
\r
123 return this.configModelCreateService.publishConfigModel(id);
\r
127 * This is a searchConfigModels method
\r
130 * @return ConfigModel
\r
132 public List<ConfigModel> searchConfigModels(String tags) {
\r
133 List<ConfigModel> models = configModelRepository.findByTagsContainingIgnoreCase(tags);
\r
134 if (models != null) {
\r
135 for (ConfigModel configModel : models) {
\r
136 configModel.setConfigModelContents(null);
\r
143 * This is a getConfigModelByNameAndVersion method
\r
146 * @param version version
\r
147 * @return ConfigModel
\r
149 public ConfigModel getConfigModelByNameAndVersion(@NotNull String name, String version) throws BluePrintException {
\r
150 ConfigModel configModel;
\r
151 Optional<ConfigModel> dbConfigModel;
\r
152 if (StringUtils.isNotBlank(version)) {
\r
153 dbConfigModel = configModelRepository.findByArtifactNameAndArtifactVersion(name, version);
\r
155 dbConfigModel = configModelRepository.findTopByArtifactNameOrderByArtifactVersionDesc(name);
\r
157 if (dbConfigModel.isPresent()) {
\r
158 configModel = dbConfigModel.get();
\r
160 throw new BluePrintException(String.format("failed to get config model name(%s), version(%s) from repo", name, version));
\r
162 return configModel;
\r
166 * This is a getConfigModel method
\r
169 * @return ConfigModel
\r
170 * @throws BluePrintException BluePrintException
\r
172 public ConfigModel getConfigModel(@NotNull Long id) throws BluePrintException {
\r
173 ConfigModel configModel;
\r
174 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
175 if (dbConfigModel.isPresent()) {
\r
176 configModel = dbConfigModel.get();
\r
178 throw new BluePrintException(String.format(CONFIG_MODEL_ID_FAILURE_MSG, id));
\r
181 return configModel;
\r
185 * This method returns clone of the given model id, by masking the other unrelated fields
\r
188 * @return ConfigModel
\r
189 * @throws BluePrintException BluePrintException
\r
192 public ConfigModel getCloneConfigModel(@NotNull Long id) throws BluePrintException {
\r
194 ConfigModel configModel;
\r
195 ConfigModel cloneConfigModel;
\r
196 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
197 if (dbConfigModel.isPresent()) {
\r
198 configModel = dbConfigModel.get();
\r
199 cloneConfigModel = configModel;
\r
200 cloneConfigModel.setUpdatedBy("xxxxx@xxx.com");
\r
201 cloneConfigModel.setArtifactName("XXXX");
\r
202 cloneConfigModel.setPublished("XXXX");
\r
203 cloneConfigModel.setPublished("XXXX");
\r
204 cloneConfigModel.setUpdatedBy("XXXX");
\r
205 cloneConfigModel.setId(null);
\r
206 cloneConfigModel.setTags(null);
\r
207 cloneConfigModel.setCreatedDate(new Date());
\r
208 List<ConfigModelContent> configModelContents = cloneConfigModel.getConfigModelContents();
\r
210 if (CollectionUtils.isNotEmpty(configModelContents)) {
\r
211 for (ConfigModelContent configModelContent : configModelContents) {
\r
212 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {
\r
213 configModelContent.setId(null);
\r
214 configModelContent.setCreationDate(new Date());
\r
216 if (ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON
\r
217 .equalsIgnoreCase(configModelContent.getContentType())) {
\r
218 ServiceTemplate serviceTemplate = JacksonUtils
\r
219 .readValue(configModelContent.getContent(), ServiceTemplate.class);
\r
220 if (serviceTemplate != null && serviceTemplate.getMetadata() != null) {
\r
221 serviceTemplate.getMetadata()
\r
222 .put(BluePrintConstants.METADATA_TEMPLATE_AUTHOR, "XXXX");
\r
223 serviceTemplate.getMetadata()
\r
224 .put(BluePrintConstants.METADATA_TEMPLATE_VERSION, "1.0.0");
\r
225 serviceTemplate.getMetadata()
\r
226 .put(BluePrintConstants.METADATA_TEMPLATE_NAME, "XXXXXX");
\r
228 configModelContent.setContent(JacksonUtils.getJson(serviceTemplate));
\r
236 throw new BluePrintException(String.format(CONFIG_MODEL_ID_FAILURE_MSG, id));
\r
239 return cloneConfigModel;
\r
243 * This is a deleteConfigModel method
\r
246 * @throws BluePrintException BluePrintException
\r
250 public void deleteConfigModel(@NotNull Long id) throws BluePrintException {
\r
251 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
252 if (dbConfigModel.isPresent()) {
\r
253 configModelContentRepository.deleteByConfigModel(dbConfigModel.get());
\r
254 configModelRepository.delete(dbConfigModel.get());
\r
256 throw new BluePrintException(String.format(CONFIG_MODEL_ID_FAILURE_MSG, id));
\r