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