Sonar fix: TemplateNode.java
[ccsdk/sli/plugins.git] / template-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / template / TemplateNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.sli.plugins.template;
24
25 import java.io.FileInputStream;
26 import java.io.StringWriter;
27 import java.util.Map;
28 import java.util.Properties;
29 import org.apache.velocity.Template;
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.VelocityEngine;
32 import org.apache.velocity.runtime.RuntimeConstants;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class TemplateNode implements SvcLogicJavaPlugin {
40     private static final Logger logger = LoggerFactory.getLogger(TemplateNode.class);
41     public static final String TEMPLATE_PATH = "templatePath";
42     public static final String OUTPUT_PATH_KEY = "output";
43     public static final String PREFIX_KEY = "prefix";
44     public static final String REQUIRED_PARAMETERS_ERROR_MESSAGE = "templateName & outputPath are required fields";
45     protected static final String TEMPLATE_PROPERTIES_FILE_NAME = "template-node.properties";
46     protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
47     protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
48
49     protected VelocityEngine ve;
50
51     public TemplateNode() {
52         ve = new VelocityEngine();
53         setProperties();
54         ve.init();
55     }
56
57     protected void setProperties() {
58         String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
59         Properties props = new Properties();
60
61         try (FileInputStream in = new FileInputStream(configDir + "/" + TEMPLATE_PROPERTIES_FILE_NAME)) {
62             props.load(in);
63         } catch (Exception e) {
64             logger.error("Caught exception loading properties!", e);
65         }
66
67         // give sensible defaults for required properties
68         ve.setProperty(RuntimeConstants.RESOURCE_LOADER, props.getProperty("velocity.resource.loader", "file"));
69         ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
70                 props.getProperty("velocity.file.resource.loader.path", "/opt/onap/sdnc/restapi/templates"));
71         ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
72                 props.getProperty("velocity.file.resource.loader.cache", "false"));
73
74         // allow flexible reading of additional velocity properties
75         for (String propertyName : props.stringPropertyNames()) {
76             if (propertyName.startsWith("velocity")) {
77                 logger.error("set " + propertyName.substring(9) + "=" + props.get(propertyName));
78                 ve.setProperty(propertyName.substring(9), props.get(propertyName));
79             }
80         }
81     }
82
83     public void evaluateTemplate(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
84         String templateName = params.get(TEMPLATE_PATH);
85         String outputPath = params.get(OUTPUT_PATH_KEY);
86         String prefix = params.get(PREFIX_KEY);
87
88         if (prefix != null && prefix.length() > 0) {
89             outputPath = prefix + "." + outputPath;
90         }
91
92         if (templateName == null || outputPath == null) {
93             throw new SvcLogicException(REQUIRED_PARAMETERS_ERROR_MESSAGE);
94         } else {
95             try {
96                 Template template = ve.getTemplate(templateName);
97                 VelocityContext context = new VelocityContext();
98                 context.put("ctx", ctx);
99                 context.put("params", params);
100                 StringWriter sw = new StringWriter();
101                 template.merge(context, sw);
102                 ctx.setAttribute(outputPath, sw.toString());
103             } catch (Exception e) {
104                 throw new SvcLogicException(e.getMessage());
105             }
106         }
107     }
108
109 }