X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=netconf-server%2Fsrc%2Fmain%2Fjava%2Forg%2Fcommscope%2Ftr069adapter%2Fnetconf%2Frpc%2FXmlUtility.java;fp=netconf-server%2Fsrc%2Fmain%2Fjava%2Forg%2Fcommscope%2Ftr069adapter%2Fnetconf%2Frpc%2FXmlUtility.java;h=e6c65e231f56840ce320b50a0e85efb4f1393be6;hb=76744e810f35c84ecbd1d9998e361052466e9483;hp=4ca1ce622f4b5b40d539fd60dc7094f057e3fe7c;hpb=ce4e2d38e3d42725f61c39dd172325d2def4bc44;p=oam%2Ftr069-adapter.git diff --git a/netconf-server/src/main/java/org/commscope/tr069adapter/netconf/rpc/XmlUtility.java b/netconf-server/src/main/java/org/commscope/tr069adapter/netconf/rpc/XmlUtility.java index 4ca1ce6..e6c65e2 100644 --- a/netconf-server/src/main/java/org/commscope/tr069adapter/netconf/rpc/XmlUtility.java +++ b/netconf-server/src/main/java/org/commscope/tr069adapter/netconf/rpc/XmlUtility.java @@ -1,112 +1,112 @@ -/* - * ============LICENSE_START======================================================================== - * ONAP : tr-069-adapter - * ================================================================================================= - * Copyright (C) 2020 CommScope Inc Intellectual Property. - * ================================================================================================= - * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License, - * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You - * may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions and - * limitations under the License. - * ===============LICENSE_END======================================================================= - */ - -package org.commscope.tr069adapter.netconf.rpc; - -import java.io.StringReader; -import java.io.StringWriter; -import java.net.URI; -import java.net.URISyntaxException; - -import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.commscope.tr069adapter.mapper.model.NetConfRequest; -import org.commscope.tr069adapter.mapper.model.NetConfResponse; -import org.opendaylight.netconf.api.xml.XmlElement; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.web.client.RestTemplate; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.InputSource; - -public class XmlUtility { - private static final Logger logger = LoggerFactory.getLogger(XmlUtility.class); - - private XmlUtility() {} - - private static final Logger LOG = LoggerFactory.getLogger(XmlUtility.class); - - public static String convertDocumentToString(XmlElement element) { - return convertDocumentToString(element.getDomElement()); - } - - public static String convertDocumentToString(Element element) { - String strxml = null; - try { - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); - transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); - Transformer transformer = transformerFactory.newTransformer(); - DOMSource source = new DOMSource(element); - StreamResult result = new StreamResult(new StringWriter()); - transformer.transform(source, result); - strxml = result.getWriter().toString(); - } catch (Exception e) { - LOG.error("Error while converting Element to String {}", e.toString()); - } - - return strxml; - - } - - public static Element convertStringToDocument(String xmlStr) { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); - factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); - DocumentBuilder builder; - try { - builder = factory.newDocumentBuilder(); - Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); - return doc.getDocumentElement(); - } catch (Exception e) { - LOG.error("Error while converting String to element {}", e.toString()); - } - return null; - } - - public static NetConfResponse invokeMapperCall(String requestUrl, String requestXml, - String deviceID, String swVersion, String hwVersion) { - URI uri = null; - try { - uri = new URI(requestUrl); - } catch (URISyntaxException e) { - logger.error("invalid URI {}", e.toString()); - } - - RestTemplate restTemplate = new RestTemplate(); - HttpHeaders headers = new HttpHeaders(); - NetConfRequest req = new NetConfRequest(requestXml, deviceID, swVersion, hwVersion); - - HttpEntity entity = new HttpEntity<>(req, headers); - NetConfResponse restResponse = null; - if (uri != null) { - restResponse = restTemplate.postForObject(uri, entity, NetConfResponse.class); - } - - return restResponse; - } -} +/* + * ============LICENSE_START======================================================================== + * ONAP : tr-069-adapter + * ================================================================================================= + * Copyright (C) 2020 CommScope Inc Intellectual Property. + * ================================================================================================= + * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License, + * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You + * may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + * ===============LICENSE_END======================================================================= + */ + +package org.commscope.tr069adapter.netconf.rpc; + +import java.io.StringReader; +import java.io.StringWriter; +import java.net.URI; +import java.net.URISyntaxException; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.commscope.tr069adapter.mapper.model.NetConfRequest; +import org.commscope.tr069adapter.mapper.model.NetConfResponse; +import org.opendaylight.netconf.api.xml.XmlElement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.web.client.RestTemplate; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; + +public class XmlUtility { + private static final Logger logger = LoggerFactory.getLogger(XmlUtility.class); + + private XmlUtility() {} + + private static final Logger LOG = LoggerFactory.getLogger(XmlUtility.class); + + public static String convertDocumentToString(XmlElement element) { + return convertDocumentToString(element.getDomElement()); + } + + public static String convertDocumentToString(Element element) { + String strxml = null; + try { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(element); + StreamResult result = new StreamResult(new StringWriter()); + transformer.transform(source, result); + strxml = result.getWriter().toString(); + } catch (Exception e) { + LOG.error("Error while converting Element to String {}", e.toString()); + } + + return strxml; + + } + + public static Element convertStringToDocument(String xmlStr) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); + DocumentBuilder builder; + try { + builder = factory.newDocumentBuilder(); + Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); + return doc.getDocumentElement(); + } catch (Exception e) { + LOG.error("Error while converting String to element {}", e.toString()); + } + return null; + } + + public static NetConfResponse invokeMapperCall(String requestUrl, String requestXml, + String deviceID, String swVersion, String hwVersion) { + URI uri = null; + try { + uri = new URI(requestUrl); + } catch (URISyntaxException e) { + logger.error("invalid URI {}", e.toString()); + } + + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders headers = new HttpHeaders(); + NetConfRequest req = new NetConfRequest(requestXml, deviceID, swVersion, hwVersion); + + HttpEntity entity = new HttpEntity<>(req, headers); + NetConfResponse restResponse = null; + if (uri != null) { + restResponse = restTemplate.postForObject(uri, entity, NetConfResponse.class); + } + + return restResponse; + } +}