Second part of onap rename
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / transform / XSLTTransformerNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdnc.config.generator.transform;
22
23 import java.io.File;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.nio.charset.Charset;
27 import java.util.Map;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
32
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;
38
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;
45
46
47 public class XSLTTransformerNode implements SvcLogicJavaPlugin {
48
49     private static final EELFLogger log = EELFManager.getInstance().getLogger(XSLTTransformerNode.class);
50
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);
54         try {
55             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
56
57             String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
58
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());
63                 }
64             }
65             if (StringUtils.isBlank(templateData)) {
66                 throw new Exception("In-param templateFile/templateData value is missing");
67             }
68
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");
72             }
73
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) {
79             e.printStackTrace();
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());
85         }
86     }
87
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();
94     }
95
96 }