2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.features.generator.service;
\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.features.data.adaptor.domain.ConfigResource;
\r
28 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;
\r
29 import org.onap.ccsdk.features.generator.data.ConfigGeneratorInfo;
\r
30 import org.onap.ccsdk.features.generator.tool.CustomJsonNodeFactory;
\r
31 import org.onap.ccsdk.features.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
38 public class ConfigGeneratorServiceImpl implements ConfigGeneratorService {
\r
40 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorServiceImpl.class);
\r
41 private static final String CLASS_NAME = "ConfigGeneratorServiceImpl";
\r
43 private ConfigResourceService configResourceService;
\r
45 public ConfigGeneratorServiceImpl(ConfigResourceService configResourceService) {
\r
46 logger.info("{} Constuctor Initated...", CLASS_NAME);
\r
47 this.configResourceService = configResourceService;
\r
51 public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException {
\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
59 ConfigResource configResourceQuery = new ConfigResource();
\r
60 configResourceQuery.setResourceId(configGeneratorInfo.getResourceId());
\r
61 configResourceQuery.setResourceType(configGeneratorInfo.getResourceType());
\r
62 configResourceQuery.setTemplateName(configGeneratorInfo.getTemplateName());
\r
64 List<ConfigResource> configResourceList = configResourceService.getConfigResource(configResourceQuery);
\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
75 ConfigResource configResource = configResourceList.get(0);
\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
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
91 return configGeneratorInfo;
\r
95 public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData)
\r
96 throws SvcLogicException {
\r
97 return generateConfiguration(templateContent, templateData, true);
\r
101 public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull)
\r
102 throws SvcLogicException {
\r
103 ConfigGeneratorInfo configGeneratorInfo = null;
\r
105 if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) {
\r
106 configGeneratorInfo = new ConfigGeneratorInfo();
\r
110 ObjectMapper mapper = new ObjectMapper();
\r
111 CustomJsonNodeFactory f = new CustomJsonNodeFactory();
\r
112 mapper.setNodeFactory(f);
\r
114 JsonNode jsonObj = mapper.readValue(templateData, JsonNode.class);
\r
116 TransformationUtils.removeJsonNullNode(jsonObj);
\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
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
131 StringWriter writer = new StringWriter();
\r
132 Velocity.evaluate(context, writer, "TemplateData", templateContent);
\r
134 configGeneratorInfo.setMashedData(writer.toString());
\r
136 } catch (Exception e) {
\r
137 logger.error("Failed to generate Configuration ({})", e.getMessage());
\r
138 throw new SvcLogicException(e.getMessage(), e);
\r
140 return configGeneratorInfo;
\r