Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / generator-provider / src / main / java / org / onap / ccsdk / config / generator / service / ConfigGeneratorServiceImpl.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.config.generator.service;\r
19 \r
20 import java.io.StringWriter;\r
21 import java.util.Iterator;\r
22 import java.util.List;\r
23 import org.apache.commons.collections.CollectionUtils;\r
24 import org.apache.commons.lang3.StringUtils;\r
25 import org.apache.velocity.VelocityContext;\r
26 import org.apache.velocity.app.Velocity;\r
27 import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource;\r
28 import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;\r
29 import org.onap.ccsdk.config.generator.data.ConfigGeneratorInfo;\r
30 import org.onap.ccsdk.config.generator.tool.CustomJsonNodeFactory;\r
31 import org.onap.ccsdk.config.model.utils.TransformationUtils;\r
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
33 import com.att.eelf.configuration.EELFLogger;\r
34 import com.att.eelf.configuration.EELFManager;\r
35 import com.fasterxml.jackson.databind.JsonNode;\r
36 import com.fasterxml.jackson.databind.ObjectMapper;\r
37 \r
38 public class ConfigGeneratorServiceImpl implements ConfigGeneratorService {\r
39 \r
40     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorServiceImpl.class);\r
41     private static final String CLASS_NAME = "ConfigGeneratorServiceImpl";\r
42 \r
43     private ConfigResourceService configResourceService;\r
44 \r
45     public ConfigGeneratorServiceImpl(ConfigResourceService configResourceService) {\r
46         logger.info("{} Constuctor Initated...", CLASS_NAME);\r
47         this.configResourceService = configResourceService;\r
48     }\r
49 \r
50     @Override\r
51     public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException {\r
52 \r
53         if (configGeneratorInfo != null && StringUtils.isNotBlank(configGeneratorInfo.getResourceId())\r
54                 && StringUtils.isNotBlank(configGeneratorInfo.getResourceType())\r
55                 && StringUtils.isNotBlank(configGeneratorInfo.getRecipeName())\r
56                 && StringUtils.isNotBlank(configGeneratorInfo.getTemplateName())\r
57                 && StringUtils.isNotBlank(configGeneratorInfo.getTemplateContent())) {\r
58 \r
59             ConfigResource configResourceQuery = new ConfigResource();\r
60             configResourceQuery.setResourceId(configGeneratorInfo.getResourceId());\r
61             configResourceQuery.setResourceType(configGeneratorInfo.getResourceType());\r
62             configResourceQuery.setTemplateName(configGeneratorInfo.getTemplateName());\r
63 \r
64             List<ConfigResource> configResourceList = configResourceService.getConfigResource(configResourceQuery);\r
65 \r
66             if (CollectionUtils.isEmpty(configResourceList)) {\r
67                 throw new SvcLogicException("No Config Resource found");\r
68             } else if (configResourceList.size() > 1) {\r
69                 throw new SvcLogicException("More than one Config Resource found for specified parameter for"\r
70                         + " resourceId " + configGeneratorInfo.getResourceId() + ", resourceType "\r
71                         + configGeneratorInfo.getResourceType() + ", recipeName " + configGeneratorInfo.getRecipeName()\r
72                         + ", templateName " + configGeneratorInfo.getTemplateName());\r
73             }\r
74 \r
75             ConfigResource configResource = configResourceList.get(0);\r
76 \r
77             if (configResource != null && StringUtils.isNotBlank(configResource.getResourceData())) {\r
78                 configGeneratorInfo.setResourceData(configResource.getResourceData());\r
79                 logger.debug("Retrieve ConfigResource Data : ({})", configResource.getResourceData());\r
80                 ConfigGeneratorInfo generatorInfo = generateConfiguration(configGeneratorInfo.getTemplateContent(),\r
81                         configResource.getResourceData());\r
82                 if (generatorInfo != null) {\r
83                     configGeneratorInfo.setMashedData(generatorInfo.getMashedData());\r
84                 }\r
85             } else {\r
86                 throw new SvcLogicException(\r
87                         "Failed to get the Resource Data for the Resource Id :" + configGeneratorInfo.getResourceId()\r
88                                 + " of template :" + configGeneratorInfo.getTemplateName());\r
89             }\r
90         }\r
91         return configGeneratorInfo;\r
92     }\r
93 \r
94     @Override\r
95     public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData)\r
96             throws SvcLogicException {\r
97         return generateConfiguration(templateContent, templateData, true);\r
98     }\r
99 \r
100     @Override\r
101     public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull)\r
102             throws SvcLogicException {\r
103         ConfigGeneratorInfo configGeneratorInfo = null;\r
104         try {\r
105             if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) {\r
106                 configGeneratorInfo = new ConfigGeneratorInfo();\r
107 \r
108                 Velocity.init();\r
109 \r
110                 ObjectMapper mapper = new ObjectMapper();\r
111                 CustomJsonNodeFactory f = new CustomJsonNodeFactory();\r
112                 mapper.setNodeFactory(f);\r
113 \r
114                 JsonNode jsonObj = mapper.readValue(templateData, JsonNode.class);\r
115                 if (ignoreNull) {\r
116                     TransformationUtils.removeJsonNullNode(jsonObj);\r
117                 }\r
118 \r
119                 VelocityContext context = new VelocityContext();\r
120                 context.put("StringUtils", org.apache.commons.lang3.StringUtils.class);\r
121                 context.put("BooleanUtils", org.apache.commons.lang3.BooleanUtils.class);\r
122 \r
123                 Iterator<String> ii = jsonObj.fieldNames();\r
124                 while (ii.hasNext()) {\r
125                     String key = ii.next();\r
126                     JsonNode node = jsonObj.get(key);\r
127                     logger.info("Adding key ({}) with value ({})", key, node);\r
128                     context.put(key, node);\r
129                 }\r
130 \r
131                 StringWriter writer = new StringWriter();\r
132                 Velocity.evaluate(context, writer, "TemplateData", templateContent);\r
133                 writer.flush();\r
134                 configGeneratorInfo.setMashedData(writer.toString());\r
135             }\r
136         } catch (Exception e) {\r
137             logger.error("Failed to generate Configuration ({})", e.getMessage());\r
138             throw new SvcLogicException(e.getMessage(), e);\r
139         }\r
140         return configGeneratorInfo;\r
141     }\r
142 \r
143 }\r