Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / XmlUtility.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.netconf.rpc;
20
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.net.URI;
24 import java.net.URISyntaxException;
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.commscope.tr069adapter.mapper.model.NetConfRequest;
35 import org.commscope.tr069adapter.mapper.model.NetConfResponse;
36 import org.opendaylight.netconf.api.xml.XmlElement;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.http.HttpEntity;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.web.client.RestTemplate;
42 import org.w3c.dom.Document;
43 import org.w3c.dom.Element;
44 import org.xml.sax.InputSource;
45
46 public class XmlUtility {
47   private static final Logger logger = LoggerFactory.getLogger(XmlUtility.class);
48
49   private XmlUtility() {}
50
51   private static final Logger LOG = LoggerFactory.getLogger(XmlUtility.class);
52
53   public static String convertDocumentToString(XmlElement element) {
54     return convertDocumentToString(element.getDomElement());
55   }
56
57   public static String convertDocumentToString(Element element) {
58     String strxml = null;
59     try {
60       TransformerFactory transformerFactory = TransformerFactory.newInstance();
61       transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
62       transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
63       Transformer transformer = transformerFactory.newTransformer();
64       DOMSource source = new DOMSource(element);
65       StreamResult result = new StreamResult(new StringWriter());
66       transformer.transform(source, result);
67       strxml = result.getWriter().toString();
68     } catch (Exception e) {
69       LOG.error("Error while converting Element to String {}", e.toString());
70     }
71
72     return strxml;
73
74   }
75
76   public static Element convertStringToDocument(String xmlStr) {
77     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
78     factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
79     factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
80     DocumentBuilder builder;
81     try {
82       builder = factory.newDocumentBuilder();
83       Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
84       return doc.getDocumentElement();
85     } catch (Exception e) {
86       LOG.error("Error while converting String to element {}", e.toString());
87     }
88     return null;
89   }
90
91   public static NetConfResponse invokeMapperCall(String requestUrl, String requestXml,
92       String deviceID, String swVersion, String hwVersion) {
93     URI uri = null;
94     try {
95       uri = new URI(requestUrl);
96     } catch (URISyntaxException e) {
97       logger.error("invalid URI {}", e.toString());
98     }
99
100     RestTemplate restTemplate = new RestTemplate();
101     HttpHeaders headers = new HttpHeaders();
102     NetConfRequest req = new NetConfRequest(requestXml, deviceID, swVersion, hwVersion);
103
104     HttpEntity<NetConfRequest> entity = new HttpEntity<>(req, headers);
105     NetConfResponse restResponse = null;
106     if (uri != null) {
107       restResponse = restTemplate.postForObject(uri, entity, NetConfResponse.class);
108     }
109
110     return restResponse;
111   }
112 }