Merge "Reorder modifiers"
[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         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                         
120                         Element root = newdoc.createElement(nodes.item(0).getNodeName());
121                         newdoc.appendChild(root);
122
123                         NodeList childNodes = nodes.item(0).getChildNodes();
124                         for (int i = 0; i < childNodes.getLength(); i++) {                              
125                                         Node n = childNodes.item(i);
126                                         Node newNode = newdoc.importNode(n, true);
127                                         root.appendChild(newNode);                              
128                         }
129
130                         String s = domToStr(newdoc);
131                         msoLogger.debug("Formatted SdncPutReq:\n" + s);
132                         return s;
133
134                 } catch (Exception e) {
135                         msoLogger.error(MessageEnum.RA_ERROR_CREATE_SDNC_REQUEST, "SDNC", "", MsoLogger.ErrorCode.DataError, "Exception in genSdncPutReq", e);
136                 }
137                 return null;
138         }
139
140         public static String genMsoFailResp(SDNCResponse resp) {
141                 try {
142
143                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
144                         DocumentBuilder db = dbf.newDocumentBuilder();
145
146                         //NewDoc for output
147                         //Root
148                         Document newdoc = db.newDocument();
149                         Element root = newdoc.createElement("output");
150                         newdoc.appendChild(root);
151
152                         Element elem1 = newdoc.createElement("svc-request-id");
153                         elem1.appendChild(newdoc.createTextNode(resp.getReqId()));
154                         root.appendChild(elem1);
155
156                         Element elem2 = newdoc.createElement("response-code");
157                         elem2.appendChild(newdoc.createTextNode(String.valueOf(resp.getRespCode())));
158                         root.appendChild(elem2);
159
160                         Element elem3 = newdoc.createElement("response-message");
161                         elem3.appendChild(newdoc.createTextNode(String.valueOf(resp.getRespMsg())));
162                         root.appendChild(elem3);
163
164                         String s = domToStr(newdoc);
165                         msoLogger.debug("Formatted SdncReq:" + s);
166                         return s;
167
168                 } catch (Exception e) {
169                         msoLogger.error(MessageEnum.RA_ERROR_CREATE_SDNC_RESPONSE, "SDNC", "", MsoLogger.ErrorCode.DataError, "Exception in genMsoFailResp", e);
170                 }
171                 return null;
172         }
173
174
175         public static String domToStr(Document doc)
176         {
177                 if (doc != null)
178                 {
179                         try {
180                                 DOMSource ds = new DOMSource(doc);
181                                 StringWriter sw = new StringWriter();
182                                 StreamResult sr = new StreamResult(sw);
183                                 TransformerFactory tf = TransformerFactory.newInstance();
184                                 Transformer t = tf.newTransformer();
185                                 //t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");//<?xml version="1.0" encoding="UTF-8"?>
186                                 t.transform(ds, sr);
187                                 String s = sw.toString();
188                                 
189                                 // This is an awful fix for now but we don't want that xmlns="" to be generated
190                                 s = s.replaceAll("xmlns=\"\"", "");
191                                 return s;
192                         } catch (Exception e) {
193                                 msoLogger.error(MessageEnum.RA_ERROR_CONVERT_XML2STR, "", "", MsoLogger.ErrorCode.DataError, "Exception - domToStr", e);
194                         }
195                 }
196                 return null;
197         }
198 }