aboutsummaryrefslogtreecommitdiffstats
path: root/blueprints-processor/plugin/generator-provider/src/main/java/org/onap/ccsdk/config/generator/service/ConfigGeneratorServiceImpl.java
blob: 472f5b2918cd0f5c575b0b32c6558d1ba8403825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.config.generator.service;

import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource;
import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;
import org.onap.ccsdk.config.generator.data.ConfigGeneratorInfo;
import org.onap.ccsdk.config.generator.tool.CustomJsonNodeFactory;
import org.onap.ccsdk.config.model.utils.TransformationUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ConfigGeneratorServiceImpl implements ConfigGeneratorService {

    private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorServiceImpl.class);
    private static final String CLASS_NAME = "ConfigGeneratorServiceImpl";

    private ConfigResourceService configResourceService;

    public ConfigGeneratorServiceImpl(ConfigResourceService configResourceService) {
        logger.info("{} Constuctor Initated...", CLASS_NAME);
        this.configResourceService = configResourceService;
    }

    @Override
    public ConfigGeneratorInfo generateConfiguration(ConfigGeneratorInfo configGeneratorInfo) throws SvcLogicException {

        if (configGeneratorInfo != null && StringUtils.isNotBlank(configGeneratorInfo.getResourceId())
                && StringUtils.isNotBlank(configGeneratorInfo.getResourceType())
                && StringUtils.isNotBlank(configGeneratorInfo.getRecipeName())
                && StringUtils.isNotBlank(configGeneratorInfo.getTemplateName())
                && StringUtils.isNotBlank(configGeneratorInfo.getTemplateContent())) {

            ConfigResource configResourceQuery = new ConfigResource();
            configResourceQuery.setResourceId(configGeneratorInfo.getResourceId());
            configResourceQuery.setResourceType(configGeneratorInfo.getResourceType());
            configResourceQuery.setTemplateName(configGeneratorInfo.getTemplateName());

            List<ConfigResource> configResourceList = configResourceService.getConfigResource(configResourceQuery);

            if (CollectionUtils.isEmpty(configResourceList)) {
                throw new SvcLogicException("No Config Resource found");
            } else if (configResourceList.size() > 1) {
                throw new SvcLogicException("More than one Config Resource found for specified parameter for"
                        + " resourceId " + configGeneratorInfo.getResourceId() + ", resourceType "
                        + configGeneratorInfo.getResourceType() + ", recipeName " + configGeneratorInfo.getRecipeName()
                        + ", templateName " + configGeneratorInfo.getTemplateName());
            }

            ConfigResource configResource = configResourceList.get(0);

            if (configResource != null && StringUtils.isNotBlank(configResource.getResourceData())) {
                configGeneratorInfo.setResourceData(configResource.getResourceData());
                logger.debug("Retrieve ConfigResource Data : ({})", configResource.getResourceData());
                ConfigGeneratorInfo generatorInfo = generateConfiguration(configGeneratorInfo.getTemplateContent(),
                        configResource.getResourceData());
                if (generatorInfo != null) {
                    configGeneratorInfo.setMashedData(generatorInfo.getMashedData());
                }
            } else {
                throw new SvcLogicException(
                        "Failed to get the Resource Data for the Resource Id :" + configGeneratorInfo.getResourceId()
                                + " of template :" + configGeneratorInfo.getTemplateName());
            }
        }
        return configGeneratorInfo;
    }

    @Override
    public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData)
            throws SvcLogicException {
        return generateConfiguration(templateContent, templateData, true);
    }

    @Override
    public ConfigGeneratorInfo generateConfiguration(String templateContent, String templateData, boolean ignoreNull)
            throws SvcLogicException {
        ConfigGeneratorInfo configGeneratorInfo = null;
        try {
            if (StringUtils.isNotBlank(templateContent) && StringUtils.isNotBlank(templateData)) {
                configGeneratorInfo = new ConfigGeneratorInfo();

                Velocity.init();

                ObjectMapper mapper = new ObjectMapper();
                CustomJsonNodeFactory f = new CustomJsonNodeFactory();
                mapper.setNodeFactory(f);

                JsonNode jsonObj = mapper.readValue(templateData, JsonNode.class);
                if (ignoreNull) {
                    TransformationUtils.removeJsonNullNode(jsonObj);
                }

                VelocityContext context = new VelocityContext();
                context.put("StringUtils", org.apache.commons.lang3.StringUtils.class);
                context.put("BooleanUtils", org.apache.commons.lang3.BooleanUtils.class);

                Iterator<String> ii = jsonObj.fieldNames();
                while (ii.hasNext()) {
                    String key = ii.next();
                    JsonNode node = jsonObj.get(key);
                    logger.info("Adding key ({}) with value ({})", key, node);
                    context.put(key, node);
                }

                StringWriter writer = new StringWriter();
                Velocity.evaluate(context, writer, "TemplateData", templateContent);
                writer.flush();
                configGeneratorInfo.setMashedData(writer.toString());
            }
        } catch (Exception e) {
            logger.error("Failed to generate Configuration ({})", e.getMessage());
            throw new SvcLogicException(e.getMessage(), e);
        }
        return configGeneratorInfo;
    }

}