Change the header to SO
[so.git] / adapters / mso-sdnc-adapter / src / main / java / org / openecomp / mso / adapters / sdnc / impl / Utils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.mso.adapters.sdnc.impl;
22
23
24 import java.io.StringWriter;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamResult;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
37
38 import org.openecomp.mso.logger.MsoLogger;
39 import org.openecomp.mso.logger.MessageEnum;
40 public class Utils {
41
42         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
43
44         public static String genSdncReq(Document reqDoc, RequestTunables rt) {
45                 try {
46
47                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
48                         DocumentBuilder db = dbf.newDocumentBuilder();
49
50                         //NewDoc for output
51                         //Root
52                         Document newdoc = db.newDocument();
53                         Element root = newdoc.createElementNS(rt.getNamespace(), "input");
54                         newdoc.appendChild(root);
55
56                         //Header
57                         Element hdr = newdoc.createElement(rt.getHeaderName());
58                         root.appendChild(hdr);
59
60                         String elemData = rt.getReqId();
61                         Element hdrChild;
62                         if (elemData != null && elemData.length() > 0)
63                         {
64                                 hdrChild = newdoc.createElement("svc-request-id");
65                                 hdrChild.appendChild(newdoc.createTextNode(elemData));
66                                 hdr.appendChild(hdrChild);
67                         }
68
69                         elemData = rt.getAction();
70                         if (elemData != null && elemData.length() > 0)
71                         {
72                                 hdrChild = newdoc.createElement("svc-action");
73                                 hdrChild.appendChild(newdoc.createTextNode(elemData));
74                                 hdr.appendChild(hdrChild);
75                         }
76
77                         elemData = rt.getSdncaNotificationUrl();
78                         if (elemData != null && elemData.length() > 0)
79                         {
80                                 hdrChild = newdoc.createElement("svc-notification-url");
81                                 hdrChild.appendChild(newdoc.createTextNode(elemData));
82                                 hdr.appendChild(hdrChild);
83                         }
84
85                         //RequestData
86                         NodeList nodes = reqDoc.getDocumentElement().getChildNodes();
87                         for (int i = 0; i < nodes.getLength(); i++) {
88                                 Node n = nodes.item(i);
89                                 Node newNode = newdoc.importNode(n, true);
90                                 root.appendChild(newNode);
91                         }
92
93                         String s = domToStr(newdoc);
94                         msoLogger.debug("Formatted SdncReq:\n" + s);
95                         return (s);
96
97                 } catch (Exception e) {
98                         msoLogger.error(MessageEnum.RA_ERROR_CREATE_SDNC_REQUEST, "SDNC", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in genSdncReq", e);
99                 }
100                 return(null);
101         }
102         
103         public static String genSdncPutReq(Document reqDoc, RequestTunables rt) {
104                 try {
105
106                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
107                         DocumentBuilder db = dbf.newDocumentBuilder();
108
109                         //NewDoc for output
110                         //Root
111                         Document newdoc = db.newDocument();
112                         
113                         //RequestData
114                         NodeList nodes = reqDoc.getDocumentElement().getChildNodes();
115                         
116                         
117                         Element root = newdoc.createElement(nodes.item(0).getNodeName());
118                         newdoc.appendChild(root);
119
120                         NodeList childNodes = nodes.item(0).getChildNodes();
121                         for (int i = 0; i < childNodes.getLength(); i++) {                              
122                                         Node n = childNodes.item(i);
123                                         Node newNode = newdoc.importNode(n, true);
124                                         root.appendChild(newNode);                              
125                         }
126
127                         String s = domToStr(newdoc);
128                         msoLogger.debug("Formatted SdncPutReq:\n" + s);
129                         return (s);
130
131                 } catch (Exception e) {
132                         msoLogger.error(MessageEnum.RA_ERROR_CREATE_SDNC_REQUEST, "SDNC", "", MsoLogger.ErrorCode.DataError, "Exception in genSdncPutReq", e);
133                 }
134                 return(null);
135         }
136
137         public static String genMsoFailResp(SDNCResponse resp) {
138                 try {
139
140                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
141                         DocumentBuilder db = dbf.newDocumentBuilder();
142
143                         //NewDoc for output
144                         //Root
145                         Document newdoc = db.newDocument();
146                         Element root = newdoc.createElement("output");
147                         newdoc.appendChild(root);
148
149                         Element elem1 = newdoc.createElement("svc-request-id");
150                         elem1.appendChild(newdoc.createTextNode(resp.getReqId()));
151                         root.appendChild(elem1);
152
153                         Element elem2 = newdoc.createElement("response-code");
154                         elem2.appendChild(newdoc.createTextNode(String.valueOf(resp.getRespCode())));
155                         root.appendChild(elem2);
156
157                         Element elem3 = newdoc.createElement("response-message");
158                         elem3.appendChild(newdoc.createTextNode(String.valueOf(resp.getRespMsg())));
159                         root.appendChild(elem3);
160
161                         String s = domToStr(newdoc);
162                         msoLogger.debug("Formatted SdncReq:" + s);
163                         return (s);
164
165                 } catch (Exception e) {
166                         msoLogger.error(MessageEnum.RA_ERROR_CREATE_SDNC_RESPONSE, "SDNC", "", MsoLogger.ErrorCode.DataError, "Exception in genMsoFailResp", e);
167                 }
168                 return(null);
169         }
170
171
172         public static String domToStr(Document doc)
173         {
174                 if (doc != null)
175                 {
176                         try {
177                                 DOMSource ds = new DOMSource(doc);
178                                 StringWriter sw = new StringWriter();
179                                 StreamResult sr = new StreamResult(sw);
180                                 TransformerFactory tf = TransformerFactory.newInstance();
181                                 Transformer t = tf.newTransformer();
182                                 //t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");//<?xml version="1.0" encoding="UTF-8"?>
183                                 t.transform(ds, sr);
184                                 String s = sw.toString();
185                                 
186                                 // This is an awful fix for now but we don't want that xmlns="" to be generated
187                                 s = s.replaceAll("xmlns=\"\"", "");
188                                 return(s);
189                         } catch (Exception e) {
190                                 msoLogger.error(MessageEnum.RA_ERROR_CONVERT_XML2STR, "", "", MsoLogger.ErrorCode.DataError, "Exception - domToStr", e);
191                         }
192                 }
193                 return(null);
194         }
195 }