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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=======================================================================
19 package org.commscope.tr069adapter.netconf.rpc;
21 import java.io.StringReader;
22 import java.io.StringWriter;
24 import java.net.URISyntaxException;
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;
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;
46 public class XmlUtility {
47 private static final Logger logger = LoggerFactory.getLogger(XmlUtility.class);
49 private XmlUtility() {}
51 private static final Logger LOG = LoggerFactory.getLogger(XmlUtility.class);
53 public static String convertDocumentToString(XmlElement element) {
54 return convertDocumentToString(element.getDomElement());
57 public static String convertDocumentToString(Element element) {
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());
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;
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());
91 public static NetConfResponse invokeMapperCall(String requestUrl, String requestXml,
92 String deviceID, String swVersion, String hwVersion) {
95 uri = new URI(requestUrl);
96 } catch (URISyntaxException e) {
97 logger.error("invalid URI {}", e.toString());
100 RestTemplate restTemplate = new RestTemplate();
101 HttpHeaders headers = new HttpHeaders();
102 NetConfRequest req = new NetConfRequest(requestXml, deviceID, swVersion, hwVersion);
104 HttpEntity<NetConfRequest> entity = new HttpEntity<>(req, headers);
105 NetConfResponse restResponse = null;
107 restResponse = restTemplate.postForObject(uri, entity, NetConfResponse.class);