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
index 4ca1ce6..e6c65e2 100644 (file)
-/*\r
- * ============LICENSE_START========================================================================\r
- * ONAP : tr-069-adapter\r
- * =================================================================================================\r
- * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
- * =================================================================================================\r
- * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
- * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
- * may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
- * either express or implied. See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ===============LICENSE_END=======================================================================\r
- */\r
-\r
-package org.commscope.tr069adapter.netconf.rpc;\r
-\r
-import java.io.StringReader;\r
-import java.io.StringWriter;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-\r
-import javax.xml.XMLConstants;\r
-import javax.xml.parsers.DocumentBuilder;\r
-import javax.xml.parsers.DocumentBuilderFactory;\r
-import javax.xml.transform.Transformer;\r
-import javax.xml.transform.TransformerFactory;\r
-import javax.xml.transform.dom.DOMSource;\r
-import javax.xml.transform.stream.StreamResult;\r
-\r
-import org.commscope.tr069adapter.mapper.model.NetConfRequest;\r
-import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
-import org.opendaylight.netconf.api.xml.XmlElement;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-import org.springframework.http.HttpEntity;\r
-import org.springframework.http.HttpHeaders;\r
-import org.springframework.web.client.RestTemplate;\r
-import org.w3c.dom.Document;\r
-import org.w3c.dom.Element;\r
-import org.xml.sax.InputSource;\r
-\r
-public class XmlUtility {\r
-  private static final Logger logger = LoggerFactory.getLogger(XmlUtility.class);\r
-\r
-  private XmlUtility() {}\r
-\r
-  private static final Logger LOG = LoggerFactory.getLogger(XmlUtility.class);\r
-\r
-  public static String convertDocumentToString(XmlElement element) {\r
-    return convertDocumentToString(element.getDomElement());\r
-  }\r
-\r
-  public static String convertDocumentToString(Element element) {\r
-    String strxml = null;\r
-    try {\r
-      TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
-      transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");\r
-      transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");\r
-      Transformer transformer = transformerFactory.newTransformer();\r
-      DOMSource source = new DOMSource(element);\r
-      StreamResult result = new StreamResult(new StringWriter());\r
-      transformer.transform(source, result);\r
-      strxml = result.getWriter().toString();\r
-    } catch (Exception e) {\r
-      LOG.error("Error while converting Element to String {}", e.toString());\r
-    }\r
-\r
-    return strxml;\r
-\r
-  }\r
-\r
-  public static Element convertStringToDocument(String xmlStr) {\r
-    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
-    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");\r
-    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");\r
-    DocumentBuilder builder;\r
-    try {\r
-      builder = factory.newDocumentBuilder();\r
-      Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));\r
-      return doc.getDocumentElement();\r
-    } catch (Exception e) {\r
-      LOG.error("Error while converting String to element {}", e.toString());\r
-    }\r
-    return null;\r
-  }\r
-\r
-  public static NetConfResponse invokeMapperCall(String requestUrl, String requestXml,\r
-      String deviceID, String swVersion, String hwVersion) {\r
-    URI uri = null;\r
-    try {\r
-      uri = new URI(requestUrl);\r
-    } catch (URISyntaxException e) {\r
-      logger.error("invalid URI {}", e.toString());\r
-    }\r
-\r
-    RestTemplate restTemplate = new RestTemplate();\r
-    HttpHeaders headers = new HttpHeaders();\r
-    NetConfRequest req = new NetConfRequest(requestXml, deviceID, swVersion, hwVersion);\r
-\r
-    HttpEntity<NetConfRequest> entity = new HttpEntity<>(req, headers);\r
-    NetConfResponse restResponse = null;\r
-    if (uri != null) {\r
-      restResponse = restTemplate.postForObject(uri, entity, NetConfResponse.class);\r
-    }\r
-\r
-    return restResponse;\r
-  }\r
-}\r
+/*
+ * ============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<NetConfRequest> entity = new HttpEntity<>(req, headers);
+    NetConfResponse restResponse = null;
+    if (uri != null) {
+      restResponse = restTemplate.postForObject(uri, entity, NetConfResponse.class);
+    }
+
+    return restResponse;
+  }
+}