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