3c92f7e9487d8b1d81da4da71aebce9f3a8dbc95
[ccsdk/cds.git] /
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 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
37 \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
43 \r
44 /**\r
45  * ServiceTemplateCreateService.java Purpose: Provide Service Template Create Service processing\r
46  * ServiceTemplateCreateService\r
47  *\r
48  * @author Brinda Santh\r
49  * @version 1.0\r
50  */\r
51 \r
52 @Service\r
53 public class ConfigModelCreateService {\r
54 \r
55     private static EELFLogger log = EELFManager.getInstance().getLogger(ConfigModelCreateService.class);\r
56 \r
57     private ConfigModelRepository configModelRepository;\r
58     private ConfigModelValidatorService configModelValidatorService;\r
59 \r
60     /**\r
61      * This is a ConfigModelCreateService\r
62      *\r
63      * @param configModelRepository       ConfigModelRepository\r
64      * @param configModelValidatorService ConfigModelValidatorService\r
65      */\r
66     public ConfigModelCreateService(ConfigModelRepository configModelRepository,\r
67                                     ConfigModelValidatorService configModelValidatorService) {\r
68         this.configModelRepository = configModelRepository;\r
69         this.configModelValidatorService = configModelValidatorService;\r
70     }\r
71 \r
72     /**\r
73      * This is a createInitialServiceTemplateContent method\r
74      *\r
75      * @param templateName templateName\r
76      * @return String\r
77      * @throws BluePrintException BluePrintException\r
78      */\r
79     public String createInitialServiceTemplateContent(String templateName) throws BluePrintException {\r
80         String serviceTemplateContent = null;\r
81         if (StringUtils.isNotBlank(templateName)) {\r
82             try {\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
87             }\r
88 \r
89         }\r
90         return serviceTemplateContent;\r
91     }\r
92 \r
93     /**\r
94      * This is a createInitialServiceTemplate method\r
95      *\r
96      * @param templateName templateName\r
97      * @return ServiceTemplate\r
98      * @throws BluePrintException BluePrintException\r
99      */\r
100     public ServiceTemplate createInitialServiceTemplate(String templateName) throws BluePrintException {\r
101         ServiceTemplate serviceTemplate = null;\r
102         if (StringUtils.isNotBlank(templateName)) {\r
103             try {\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
108                 }\r
109             } catch (IOException e) {\r
110                 throw new BluePrintException(e.getMessage(), e);\r
111             }\r
112 \r
113         }\r
114         return serviceTemplate;\r
115     }\r
116 \r
117     /**\r
118      * This is a saveConfigModel method\r
119      *\r
120      * @param configModel configModel\r
121      * @return ConfigModel\r
122      * @throws BluePrintException BluePrintException\r
123      */\r
124     public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {\r
125 \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
131 \r
132             if (StringUtils.isBlank(author)) {\r
133                 throw new BluePrintException("Artifact Author is missing in the Service Template");\r
134             }\r
135 \r
136             if (StringUtils.isBlank(artifactName)) {\r
137                 throw new BluePrintException("Artifact Name is missing in the Service Template");\r
138             }\r
139 \r
140             if (StringUtils.isBlank(artifactVersion)) {\r
141                 throw new BluePrintException("Artifact Version is missing in the Service Template");\r
142             }\r
143             ConfigModel updateConfigModel;\r
144 \r
145             Optional<ConfigModel> dbConfigModelOptional = Optional.empty();\r
146 \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
150             }\r
151 \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
157             }\r
158 \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
163             } else {\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
173             }\r
174 \r
175             Long dbConfigModelId = updateConfigModel.getId();\r
176 \r
177             if (dbConfigModelId == null) {\r
178                 throw new BluePrintException("failed to get the initial saved config model id.");\r
179             }\r
180 \r
181             log.info("Processing for config model id : {}", dbConfigModelId);\r
182 \r
183             deleteConfigModelContent(dbConfigModelId);\r
184 \r
185             addConfigModelContent(dbConfigModelId, configModel);\r
186 \r
187             // Populate Content model types\r
188             updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author);\r
189 \r
190 \r
191             return updateConfigModel;\r
192         } else {\r
193             throw new BluePrintException("Config model information is missing");\r
194         }\r
195 \r
196     }\r
197 \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
205             }\r
206 \r
207         }\r
208     }\r
209 \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
220                 }\r
221             }\r
222             configModelRepository.saveAndFlush(dbConfigModel);\r
223         }\r
224     }\r
225 \r
226     private ConfigModel updateConfigModel(Long dbConfigModelId, String artifactName, String artifactVersion,\r
227                                           String author) throws BluePrintException {\r
228 \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
234         }\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
244     }\r
245 \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
250         return valids;\r
251 \r
252     }\r
253 \r
254     private String getConfigModelTags(ConfigModel configModel) throws BluePrintException {\r
255         String tags = null;\r
256         if (CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {\r
257 \r
258             for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {\r
259                 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {\r
260 \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
265                     }\r
266 \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
278                         }\r
279                         tags = String.valueOf(serviceTemplate.getMetadata());\r
280                     }\r
281                 }\r
282             }\r
283         }\r
284         return tags;\r
285     }\r
286 \r
287     /**\r
288      * This is a publishConfigModel method\r
289      *\r
290      * @param id id\r
291      * @return ConfigModel\r
292      * @throws BluePrintException BluePrintException\r
293      */\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
308                         }\r
309                     }\r
310                 }\r
311             }\r
312             dbConfigModel.setPublished(ApplicationConstants.ACTIVE_Y);\r
313             configModelRepository.save(dbConfigModel);\r
314             log.info("Config model ({}) published successfully.", id);\r
315         } else {\r
316             throw new BluePrintException(String.format("Couldn't get Config model for id :(%s)", id));\r
317         }\r
318         return dbConfigModel;\r
319     }\r
320 \r
321     /**\r
322      * This is a validateServiceTemplate method\r
323      *\r
324      * @param serviceTemplate Service Template\r
325      * @return ServiceTemplate\r
326      * @throws BluePrintException BluePrintException\r
327      */\r
328     public ServiceTemplate validateServiceTemplate(ServiceTemplate serviceTemplate) throws BluePrintException {\r
329         return this.configModelValidatorService.validateServiceTemplate(serviceTemplate);\r
330     }\r
331 }\r