2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.ccsdk.sli.plugins.template;
25 import java.io.FileInputStream;
26 import java.io.StringWriter;
28 import java.util.Map.Entry;
29 import java.util.Properties;
30 import org.apache.velocity.Template;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.app.VelocityEngine;
33 import org.apache.velocity.runtime.RuntimeConstants;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class TemplateNode implements SvcLogicJavaPlugin {
41 private static final Logger logger = LoggerFactory.getLogger(TemplateNode.class);
42 public static final String TEMPLATE_PATH = "templatePath";
43 public static final String OUTPUT_PATH_KEY = "output";
44 public static final String PREFIX_KEY = "prefix";
45 public static final String REQUIRED_PARAMETERS_ERROR_MESSAGE = "templateName & outputPath are required fields";
46 protected static final String TEMPLATE_PROPERTIES_FILE_NAME = "template-node.properties";
47 protected static final String DEFAULT_PROPERTIES_DIR = "/opt/onap/ccsdk/data/properties";
48 protected static final String PROPERTIES_DIR_KEY = "SDNC_CONFIG_DIR";
50 protected VelocityEngine ve;
52 public TemplateNode() {
53 ve = new VelocityEngine();
56 ve.loadDirective("org.onap.ccsdk.sli.plugins.template.HideNullJson");
59 protected void setProperties() {
60 String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR);
61 Properties props = new Properties();
63 try (FileInputStream in = new FileInputStream(configDir + "/" + TEMPLATE_PROPERTIES_FILE_NAME)) {
65 } catch (Exception e) {
66 logger.error("Caught exception loading properties!", e);
69 // give sensible defaults for required properties
70 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, props.getProperty("velocity.resource.loader", "file"));
71 ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
72 props.getProperty("velocity.file.resource.loader.path", "/opt/onap/sdnc/restapi/templates"));
73 ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
74 props.getProperty("velocity.file.resource.loader.cache", "false"));
76 // allow flexible reading of additional velocity properties
77 for (String propertyName : props.stringPropertyNames()) {
78 if (propertyName.startsWith("velocity")) {
79 logger.error("set " + propertyName.substring(9) + "=" + props.get(propertyName));
80 ve.setProperty(propertyName.substring(9), props.get(propertyName));
85 public void evaluateTemplate(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
86 String templateName = params.get(TEMPLATE_PATH);
87 String outputPath = params.get(OUTPUT_PATH_KEY);
88 String prefix = params.get(PREFIX_KEY);
90 if (prefix != null && prefix.length() > 0) {
91 outputPath = prefix + "." + outputPath;
94 if (templateName == null || outputPath == null) {
95 throw new SvcLogicException(REQUIRED_PARAMETERS_ERROR_MESSAGE);
98 Template template = ve.getTemplate(templateName);
99 VelocityContext context = new VelocityContext();
100 context.put("ctx", ctx);
101 context.put("params", params);
102 //Adding these values directly to context makes working with the values cleaner
103 for (Entry<String, String> entry : params.entrySet()) {
104 context.put(entry.getKey(), entry.getValue());
106 StringWriter sw = new StringWriter();
107 template.merge(context, sw);
108 ctx.setAttribute(outputPath, sw.toString());
109 } catch (Exception e) {
110 throw new SvcLogicException(e.getMessage());