2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.config.generator.transform;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.nio.charset.Charset;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
39 import javax.xml.transform.Transformer;
40 import javax.xml.transform.TransformerConfigurationException;
41 import javax.xml.transform.TransformerException;
42 import javax.xml.transform.TransformerFactory;
43 import javax.xml.transform.stream.StreamResult;
44 import javax.xml.transform.stream.StreamSource;
47 public class XSLTTransformerNode implements SvcLogicJavaPlugin {
49 private static final EELFLogger log = EELFManager.getInstance().getLogger(XSLTTransformerNode.class);
51 public void transformData(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
52 log.trace("Received convertJson2DGContext call with params : " + inParams);
53 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
55 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
57 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
59 if (StringUtils.isNotBlank(templateData)) {
60 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
61 if (StringUtils.isNotBlank(templateFile)) {
62 templateData = FileUtils.readFileToString(new File(templateFile), Charset.defaultCharset());
65 if (StringUtils.isBlank(templateData)) {
66 throw new Exception("In-param templateFile/templateData value is missing");
69 String requestData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_REQUEST_DATA);
70 if (StringUtils.isBlank(requestData)) {
71 throw new Exception("In-param requestData value is missing");
74 String transformedData = transform(requestData, templateData);
75 log.trace("Transformed Data : "+ transformedData);
76 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_TRANSFORMED_DATA, transformedData);
77 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
78 } catch (Exception e) {
80 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
81 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
82 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
83 log.error("Failed in XSLTTransformerNode : " + e.getMessage());
84 throw new SvcLogicException(e.getMessage());
88 public String transform(String requestData, String templateData)
89 throws TransformerConfigurationException, TransformerException {
90 StringWriter xmlResultResource = new StringWriter();
91 Transformer xmlTransformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(templateData)));
92 xmlTransformer.transform(new StreamSource(new StringReader(requestData)), new StreamResult(xmlResultResource));
93 return xmlResultResource.getBuffer().toString();