f31a0ceb1edd11b27f645ad5a62e0373b51099b6
[ccsdk/apps.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / apps / controllerblueprints / service / ConfigModelCreateService.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 com.att.eelf.configuration.EELFLogger;\r
21 import com.att.eelf.configuration.EELFManager;\r
22 import com.google.common.base.Preconditions;\r
23 import org.apache.commons.collections.CollectionUtils;\r
24 import org.apache.commons.io.IOUtils;\r
25 import org.apache.commons.lang3.StringUtils;\r
26 import org.jetbrains.annotations.NotNull;\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate;\r
31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.common.ApplicationConstants;\r
33 import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent;\r
34 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;\r
35 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModelContent;\r
36 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;\r
37 import org.springframework.stereotype.Service;\r
38 \r
39 import java.io.IOException;\r
40 import java.nio.charset.Charset;\r
41 import java.util.ArrayList;\r
42 import java.util.List;\r
43 import java.util.Optional;\r
44 \r
45 /**\r
46  * ServiceTemplateCreateService.java Purpose: Provide Service Template Create Service processing\r
47  * ServiceTemplateCreateService\r
48  *\r
49  * @author Brinda Santh\r
50  * @version 1.0\r
51  */\r
52 @Deprecated\r
53 @Service\r
54 public class ConfigModelCreateService {\r
55 \r
56     private static EELFLogger log = EELFManager.getInstance().getLogger(ConfigModelCreateService.class);\r
57 \r
58     private ConfigModelRepository configModelRepository;\r
59 \r
60     /**\r
61      * This is a ConfigModelCreateService\r
62      *\r
63      * @param configModelRepository ConfigModelRepository\r
64      */\r
65     public ConfigModelCreateService(ConfigModelRepository configModelRepository) {\r
66         this.configModelRepository = configModelRepository;\r
67     }\r
68 \r
69     /**\r
70      * This is a createInitialServiceTemplateContent method\r
71      *\r
72      * @param templateName templateName\r
73      * @return String\r
74      * @throws BluePrintException BluePrintException\r
75      */\r
76     public String createInitialServiceTemplateContent(String templateName) throws BluePrintException {\r
77         String serviceTemplateContent = null;\r
78         if (StringUtils.isNotBlank(templateName)) {\r
79             try {\r
80                 serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()\r
81                         .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());\r
82             } catch (IOException e) {\r
83                 throw new BluePrintException(e.getMessage(), e);\r
84             }\r
85 \r
86         }\r
87         return serviceTemplateContent;\r
88     }\r
89 \r
90     /**\r
91      * This is a createInitialServiceTemplate method\r
92      *\r
93      * @param templateName templateName\r
94      * @return ServiceTemplate\r
95      * @throws BluePrintException BluePrintException\r
96      */\r
97     public ServiceTemplate createInitialServiceTemplate(String templateName) throws BluePrintException {\r
98         ServiceTemplate serviceTemplate = null;\r
99         if (StringUtils.isNotBlank(templateName)) {\r
100             try {\r
101                 String serviceTemplateContent = IOUtils.toString(ConfigModelCreateService.class.getClassLoader()\r
102                         .getResourceAsStream("service_template/" + templateName + ".json"), Charset.defaultCharset());\r
103                 if (StringUtils.isNotBlank(serviceTemplateContent)) {\r
104                     serviceTemplate = JacksonUtils.readValue(serviceTemplateContent, ServiceTemplate.class);\r
105                 }\r
106             } catch (IOException e) {\r
107                 throw new BluePrintException(e.getMessage(), e);\r
108             }\r
109 \r
110         }\r
111         return serviceTemplate;\r
112     }\r
113 \r
114     /**\r
115      * This is a saveConfigModel method\r
116      *\r
117      * @param configModel configModel\r
118      * @return ConfigModel\r
119      * @throws BluePrintException BluePrintException\r
120      */\r
121     public ConfigModel saveConfigModel(ConfigModel configModel) throws BluePrintException {\r
122 \r
123         if (configModel != null) {\r
124             String artifactName = configModel.getArtifactName();\r
125             String artifactVersion = configModel.getArtifactVersion();\r
126             String author = configModel.getUpdatedBy();\r
127             CbaContent configModelCBA = configModel.getConfigModelCBA();\r
128 \r
129             if (StringUtils.isBlank(author)) {\r
130                 throw new BluePrintException("Artifact Author is missing in the Service Template");\r
131             }\r
132 \r
133             if (StringUtils.isBlank(artifactName)) {\r
134                 throw new BluePrintException("Artifact Name is missing in the Service Template");\r
135             }\r
136 \r
137             if (StringUtils.isBlank(artifactVersion)) {\r
138                 throw new BluePrintException("Artifact Version is missing in the Service Template");\r
139             }\r
140             ConfigModel updateConfigModel;\r
141 \r
142             Optional<ConfigModel> dbConfigModelOptional = Optional.empty();\r
143 \r
144             if (configModel.getId() != null) {\r
145                 log.info("Searching for config model id : {}", configModel.getId());\r
146                 dbConfigModelOptional = configModelRepository.findById(configModel.getId());\r
147             }\r
148 \r
149             if (!dbConfigModelOptional.isPresent()) {\r
150                 log.info("Searching for config model name :"\r
151                         + configModel.getArtifactName() + ", version " + configModel.getArtifactVersion());\r
152                 dbConfigModelOptional = configModelRepository.findByArtifactNameAndArtifactVersion(\r
153                         configModel.getArtifactName(), configModel.getArtifactVersion());\r
154             }\r
155 \r
156             if (dbConfigModelOptional.isPresent()) {\r
157                 updateConfigModel = dbConfigModelOptional.get();\r
158                 log.info("Processing for config model id : {} with config model content count : {}"\r
159                         , updateConfigModel.getId(), updateConfigModel.getConfigModelContents().size());\r
160             } else {\r
161                 ConfigModel tempConfigModel = new ConfigModel();\r
162                 tempConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);\r
163                 tempConfigModel.setArtifactName(artifactName);\r
164                 tempConfigModel.setArtifactVersion(artifactVersion);\r
165                 tempConfigModel.setUpdatedBy(author);\r
166                 tempConfigModel.setPublished(ApplicationConstants.ACTIVE_N);\r
167                 tempConfigModel.setTags(artifactName);\r
168                 configModelRepository.saveAndFlush(tempConfigModel);\r
169                 updateConfigModel = tempConfigModel;\r
170             }\r
171 \r
172             Long dbConfigModelId = updateConfigModel.getId();\r
173 \r
174             if (dbConfigModelId == null) {\r
175                 throw new BluePrintException("failed to get the initial saved config model id.");\r
176             }\r
177 \r
178             log.info("Processing for config model id : {}", dbConfigModelId);\r
179 \r
180             deleteConfigModelContent(dbConfigModelId);\r
181 \r
182             addConfigModelContent(dbConfigModelId, configModel);\r
183 \r
184             // Populate Content model types\r
185             updateConfigModel = updateConfigModel(dbConfigModelId, artifactName, artifactVersion, author, configModelCBA);\r
186 \r
187 \r
188             return updateConfigModel;\r
189         } else {\r
190             throw new BluePrintException("Config model information is missing");\r
191         }\r
192 \r
193     }\r
194 \r
195     private void deleteConfigModelContent(Long dbConfigModelId) {\r
196         if (dbConfigModelId != null) {\r
197             ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);\r
198             if (CollectionUtils.isNotEmpty(dbConfigModel.getConfigModelContents())) {\r
199                 dbConfigModel.getConfigModelContents().clear();\r
200                 log.debug("Configuration Model content deleting : {}", dbConfigModel.getConfigModelContents());\r
201                 configModelRepository.saveAndFlush(dbConfigModel);\r
202             }\r
203 \r
204         }\r
205     }\r
206 \r
207     private void addConfigModelContent(Long dbConfigModelId, ConfigModel configModel) {\r
208         if (dbConfigModelId != null && configModel != null\r
209                 && CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {\r
210             ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);\r
211             for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {\r
212                 if (configModelContent != null) {\r
213                     configModelContent.setId(null);\r
214                     configModelContent.setConfigModel(dbConfigModel);\r
215                     dbConfigModel.getConfigModelContents().add(configModelContent);\r
216                     log.debug("Configuration Model content adding : {}", configModelContent);\r
217                 }\r
218             }\r
219             configModelRepository.saveAndFlush(dbConfigModel);\r
220         }\r
221     }\r
222 \r
223     private ConfigModel updateConfigModel(Long dbConfigModelId, String artifactName, String artifactVersion,\r
224                                           String author, CbaContent configModelCBA) throws BluePrintException {\r
225 \r
226         ConfigModel dbConfigModel = configModelRepository.getOne(dbConfigModelId);\r
227         // Populate tags from metadata\r
228         String tags = getConfigModelTags(dbConfigModel);\r
229         if (StringUtils.isBlank(tags)) {\r
230             throw new BluePrintException("Failed to populate tags for the config model name " + artifactName);\r
231         }\r
232         dbConfigModel.setArtifactType(ApplicationConstants.ASDC_ARTIFACT_TYPE_SDNC_MODEL);\r
233         dbConfigModel.setArtifactName(artifactName);\r
234         dbConfigModel.setArtifactVersion(artifactVersion);\r
235         dbConfigModel.setUpdatedBy(author);\r
236         dbConfigModel.setPublished(ApplicationConstants.ACTIVE_N);\r
237         dbConfigModel.setTags(tags);\r
238         dbConfigModel.setConfigModelCBA(configModelCBA);\r
239         configModelRepository.saveAndFlush(dbConfigModel);\r
240         log.info("Config model ({}) saved successfully.", dbConfigModel.getId());\r
241         return dbConfigModel;\r
242     }\r
243 \r
244     private List<String> getValidContentTypes() {\r
245         List<String> valids = new ArrayList<>();\r
246         valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);\r
247         valids.add(ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE);\r
248         return valids;\r
249 \r
250     }\r
251 \r
252     private String getConfigModelTags(ConfigModel configModel) throws BluePrintException {\r
253         String tags = null;\r
254         if (CollectionUtils.isNotEmpty(configModel.getConfigModelContents())) {\r
255 \r
256             for (ConfigModelContent configModelContent : configModel.getConfigModelContents()) {\r
257                 if (configModelContent != null && StringUtils.isNotBlank(configModelContent.getContentType())) {\r
258 \r
259                     if (!getValidContentTypes().contains(configModelContent.getContentType())) {\r
260                         throw new BluePrintException(configModelContent.getContentType()\r
261                                 + " is not a valid content type, It should be any one of this "\r
262                                 + getValidContentTypes());\r
263                     }\r
264 \r
265                     if (configModelContent.getContentType().equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {\r
266                         ServiceTemplate serviceTemplate =\r
267                                 JacksonUtils.readValue(configModelContent.getContent(), ServiceTemplate.class);\r
268                         Preconditions.checkNotNull(serviceTemplate, "failed to transform service template content");\r
269                         if (serviceTemplate.getMetadata() != null) {\r
270                             serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_AUTHOR,\r
271                                     configModel.getUpdatedBy());\r
272                             serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_VERSION,\r
273                                     configModel.getArtifactVersion());\r
274                             serviceTemplate.getMetadata().put(BluePrintConstants.METADATA_TEMPLATE_NAME,\r
275                                     configModel.getArtifactName());\r
276                         }\r
277                         tags = String.valueOf(serviceTemplate.getMetadata());\r
278                     }\r
279                 }\r
280             }\r
281         }\r
282         return tags;\r
283     }\r
284 \r
285     /**\r
286      * This is a publishConfigModel method\r
287      *\r
288      * @param id id\r
289      * @return ConfigModel\r
290      * @throws BluePrintException BluePrintException\r
291      */\r
292     public ConfigModel publishConfigModel(@NotNull Long id) throws BluePrintException {\r
293         ConfigModel dbConfigModel = null;\r
294         Optional<ConfigModel> dbConfigModelOptional = configModelRepository.findById(id);\r
295         if (dbConfigModelOptional.isPresent()) {\r
296             dbConfigModel = dbConfigModelOptional.get();\r
297             List<ConfigModelContent> configModelContents = dbConfigModel.getConfigModelContents();\r
298             if (configModelContents != null && !configModelContents.isEmpty()) {\r
299                 for (ConfigModelContent configModelContent : configModelContents) {\r
300                     if (configModelContent.getContentType()\r
301                             .equals(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON)) {\r
302                         ServiceTemplate serviceTemplate = JacksonUtils\r
303                                 .readValue(configModelContent.getContent(), ServiceTemplate.class);\r
304                         if (serviceTemplate != null) {\r
305                             validateServiceTemplate(serviceTemplate);\r
306                         }\r
307                     }\r
308                 }\r
309             }\r
310             dbConfigModel.setPublished(ApplicationConstants.ACTIVE_Y);\r
311             configModelRepository.save(dbConfigModel);\r
312             log.info("Config model ({}) published successfully.", id);\r
313         } else {\r
314             throw new BluePrintException(String.format("Couldn't get Config model for id :(%s)", id));\r
315         }\r
316         return dbConfigModel;\r
317     }\r
318 \r
319     /**\r
320      * This is a validateServiceTemplate method\r
321      *\r
322      * @param serviceTemplate Service Template\r
323      * @return ServiceTemplate\r
324      * @throws BluePrintException BluePrintException\r
325      */\r
326     public ServiceTemplate validateServiceTemplate(ServiceTemplate serviceTemplate) throws BluePrintException {\r
327         // FIXME("Plug right Validator")\r
328         return serviceTemplate;\r
329     }\r
330 }\r