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