Merge "Controller Blueprints Microservice"
[ccsdk/apps.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / apps / controllerblueprints / service / ConfigModelService.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\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
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\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
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.service;\r
19 \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
37 \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
42 \r
43 /**\r
44  * ConfigModelService.java Purpose: Provide Service Template Service processing ConfigModelService\r
45  *\r
46  * @author Brinda Santh\r
47  * @version 1.0\r
48  */\r
49 \r
50 @Service\r
51 public class ConfigModelService {\r
52 \r
53     private static EELFLogger log = EELFManager.getInstance().getLogger(ConfigModelService.class);\r
54 \r
55     private ConfigModelRepository configModelRepository;\r
56     private ConfigModelContentRepository configModelContentRepository;\r
57     private ConfigModelCreateService configModelCreateService;\r
58 \r
59     /**\r
60      * This is a ConfigModelService constructor.\r
61      *\r
62      * @param configModelRepository        configModelRepository\r
63      * @param configModelContentRepository configModelContentRepository\r
64      * @param configModelCreateService     configModelCreateService\r
65      */\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
73     }\r
74 \r
75     /**\r
76      * This is a getInitialConfigModel method\r
77      *\r
78      * @param templateName templateName\r
79      * @return ConfigModel\r
80      * @throws BluePrintException BluePrintException\r
81      */\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
94 \r
95             List<ConfigModelContent> configModelContents = new ArrayList<>();\r
96             configModelContents.add(configModelContent);\r
97 \r
98             configModel.setConfigModelContents(configModelContents);\r
99         }\r
100         return configModel;\r
101     }\r
102 \r
103     /**\r
104      * This is a saveConfigModel method\r
105      *\r
106      * @param configModel configModel\r
107      * @return ConfigModel\r
108      * @throws BluePrintException BluePrintException\r
109      */\r
110     public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {\r
111         return this.configModelCreateService.saveConfigModel(configModel);\r
112     }\r
113 \r
114     /**\r
115      * This is a publishConfigModel method\r
116      *\r
117      * @param id id\r
118      * @return ConfigModel\r
119      * @throws BluePrintException BluePrintException\r
120      */\r
121     public ConfigModel publishConfigModel(Long id) throws BluePrintException {\r
122         return this.configModelCreateService.publishConfigModel(id);\r
123     }\r
124 \r
125     /**\r
126      * This is a searchConfigModels method\r
127      *\r
128      * @param tags tags\r
129      * @return ConfigModel\r
130      */\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
136             }\r
137         }\r
138         return models;\r
139     }\r
140 \r
141     /**\r
142      * This is a getConfigModelByNameAndVersion method\r
143      *\r
144      * @param name    name\r
145      * @param version version\r
146      * @return ConfigModel\r
147      */\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
153         } else {\r
154             dbConfigModel = configModelRepository.findTopByArtifactNameOrderByArtifactVersionDesc(name);\r
155         }\r
156         if (dbConfigModel.isPresent()) {\r
157             configModel = dbConfigModel.get();\r
158         } else {\r
159             throw new BluePrintException(String.format("failed to get config model name(%s), version(%s) from repo", name, version));\r
160         }\r
161         return configModel;\r
162     }\r
163 \r
164     /**\r
165      * This is a getConfigModel method\r
166      *\r
167      * @param id id\r
168      * @return ConfigModel\r
169      * @throws BluePrintException BluePrintException\r
170      */\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
176         } else {\r
177             throw new BluePrintException(String.format("failed to get config model id(%d) from repo", id));\r
178         }\r
179 \r
180         return configModel;\r
181     }\r
182 \r
183     /**\r
184      * This method returns clone of the given model id, by masking the other unrelated fields\r
185      *\r
186      * @param id id\r
187      * @return ConfigModel\r
188      * @throws BluePrintException BluePrintException\r
189      */\r
190 \r
191     public ConfigModel getCloneConfigModel(@NotNull Long id) throws BluePrintException {\r
192 \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
208 \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
214 \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
226 \r
227                                 configModelContent.setContent(JacksonUtils.getJson(serviceTemplate));\r
228                             }\r
229                         }\r
230                     }\r
231 \r
232                 }\r
233             }\r
234         } else {\r
235             throw new BluePrintException(String.format("failed to get config model id(%d) from repo", id));\r
236         }\r
237 \r
238         return cloneConfigModel;\r
239     }\r
240 \r
241     /**\r
242      * This is a deleteConfigModel method\r
243      *\r
244      * @param id id\r
245      * @throws BluePrintException BluePrintException\r
246      */\r
247 \r
248     @Transactional\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
254         } else {\r
255             throw new BluePrintException(String.format("failed to get config model id(%d) from repo", id));\r
256         }\r
257     }\r
258 \r
259 }\r