VES Heartbeat and Software Management Feature
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / notification / NetConfSessionUtil.java
index 046db19..416df8f 100644 (file)
@@ -1,21 +1,3 @@
-/*\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.notification;\r
 \r
 import java.io.IOException;\r
@@ -27,7 +9,6 @@ import java.util.List;
 import java.util.Map;\r
 import java.util.StringTokenizer;\r
 \r
-import javax.xml.XMLConstants;\r
 import javax.xml.parsers.DocumentBuilder;\r
 import javax.xml.parsers.DocumentBuilderFactory;\r
 import javax.xml.parsers.ParserConfigurationException;\r
@@ -36,8 +17,8 @@ import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;\r
 import javax.xml.transform.stream.StreamResult;\r
 \r
-import org.commscope.tr069adapter.acs.common.DeviceInform;\r
 import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
+import org.commscope.tr069adapter.mapper.model.NetConfNotificationDTO;\r
 import org.commscope.tr069adapter.netconf.rpc.CreateSubscription;\r
 import org.opendaylight.netconf.api.NetconfMessage;\r
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
@@ -58,37 +39,39 @@ public class NetConfSessionUtil {
   private static final String INDEX_REGEX = "[0-9]{1,}";\r
   private static final String NS_URI = "urn:onf:otcc:wireless:yang:tr069-notification";\r
 \r
-  public void sendNetConfNotification(DeviceInform notification) {\r
-    NetconfMessage netconfMessage = convertToNetConfMessage(notification);\r
-    LOG.debug("Notification converted to NetConf format {}", netconfMessage);\r
-    CreateSubscription.sendNotification(netconfMessage,\r
-        notification.getDeviceDetails().getDeviceId());\r
+  public void sendNetConfNotification(NetConfNotificationDTO netConNotifDTO) {\r
+    NetconfMessage netconfMessage = convertToNetConfMessage(netConNotifDTO);\r
+    LOG.debug("Notification converted to NetConf format" + netconfMessage);\r
+    CreateSubscription.sendNotification(netconfMessage, netConNotifDTO.getDeviceID());\r
   }\r
 \r
-  private NetconfMessage convertToNetConfMessage(DeviceInform notification) {\r
+  private NetconfMessage convertToNetConfMessage(NetConfNotificationDTO netConNotifDTO) {\r
     try {\r
-      String netConfXmlMsg = getNetconfResponseXML(notification);\r
-      if (netConfXmlMsg == null)\r
-        throw new IllegalArgumentException("There are no parameters found in the response");\r
-      return new NetconfMessage(XmlUtil.readXmlToDocument(netConfXmlMsg));\r
+      String nameSpace = "";\r
+      if (netConNotifDTO.getUri() != null) {\r
+        nameSpace = netConNotifDTO.getUri();\r
+      } else {\r
+        nameSpace = NS_URI;\r
+      }\r
+      return new NetconfMessage(\r
+          XmlUtil.readXmlToDocument(getNetconfResponseXML(netConNotifDTO, nameSpace)));\r
     } catch (SAXException | IOException e) {\r
       throw new IllegalArgumentException("Cannot parse notifications", e);\r
     }\r
   }\r
 \r
-  private static String getNetconfResponseXML(DeviceInform notification) {\r
-    if (notification == null || notification.getParameters().isEmpty()) {\r
+  private static String getNetconfResponseXML(NetConfNotificationDTO netConNotifDTO,\r
+      String nameSpace) {\r
+    if (netConNotifDTO == null || netConNotifDTO.getParameters().isEmpty()) {\r
       LOG.debug("There are no parameters found in the response.");\r
       return null;\r
     }\r
 \r
-    List<ParameterDTO> parameters = notification.getParameters();\r
+    List<ParameterDTO> parameters = netConNotifDTO.getParameters();\r
 \r
     String result = null;\r
     try {\r
       DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();\r
-      docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");\r
-      docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");\r
       DocumentBuilder docBuilder = docFactory.newDocumentBuilder();\r
       Document doc = docBuilder.newDocument();\r
 \r
@@ -100,35 +83,38 @@ public class NetConfSessionUtil {
         String paramValue = paramDto.getParamValue();\r
         StringTokenizer tokenizer = new StringTokenizer(paramName, ".");\r
         String parentNodeName = null;\r
-        StringBuilder parentNodeKey = null;\r
+        String parentNodeKey = null;\r
         Element parentNode = null;\r
         while (tokenizer.hasMoreElements()) {\r
           String nodeName = (String) tokenizer.nextElement();\r
           if (null == parentNodeName) { // construct first node or\r
                                         // Device node\r
             parentNodeName = nodeName;\r
-            parentNodeKey = new StringBuilder(nodeName);\r
+            parentNodeKey = nodeName;\r
             // check if the node already exists in parentNodeMap\r
-            parentNode = parentNodeMap.get(parentNodeKey.toString());\r
+            parentNode = parentNodeMap.get(parentNodeKey);\r
             if (null == dataNode) {\r
               dataNode = parentNode;\r
             }\r
+            continue;\r
           } else if (nodeName.matches(INDEX_REGEX)) { // construct\r
                                                       // tabular and\r
                                                       // index nodes\r
 \r
             // get parent tabular node from parent MAP\r
-            parentNodeKey = parentNodeKey.append(".").append(nodeName);\r
-            Element node = parentNodeMap.get(parentNodeKey.toString());\r
+            parentNodeKey = parentNodeKey + "." + nodeName;\r
+            Element node = parentNodeMap.get(parentNodeKey);\r
 \r
             // create a tabular parent node if doesn't exit in MAP\r
             if (null == node) {\r
               node = doc.createElement(parentNodeName);\r
-              parentNodeMap.put(parentNodeKey.toString(), node);\r
+              parentNodeMap.put(parentNodeKey, node);\r
 \r
               // update current tabular parent node.\r
               if (null != parentNode)\r
                 parentNode.appendChild(node);\r
+              else\r
+                parentNode = node;\r
 \r
               // prepare and add index node to tabular parent node\r
               Element indexNode = doc.createElement(INDEX_STR);\r
@@ -145,23 +131,23 @@ public class NetConfSessionUtil {
                                                             // attribute\r
                                                             // is\r
                                                             // found\r
-            parentNodeKey = parentNodeKey.append(".").append(nodeName);\r
+            parentNodeKey = parentNodeKey + "." + nodeName;\r
             parentNodeName = nodeName;\r
           } else {\r
             // construct intermediate nodes\r
-            Element node = parentNodeMap.get(parentNodeKey.toString());\r
+            Element node = parentNodeMap.get(parentNodeKey);\r
             if (null == node) {\r
               if (null == dataNode) {\r
-                node = doc.createElementNS(NS_URI, parentNodeName);\r
+                node = doc.createElementNS(nameSpace, parentNodeName);\r
                 dataNode = node;\r
               } else {\r
                 node = doc.createElement(parentNodeName);\r
               }\r
-              parentNodeMap.put(parentNodeKey.toString(), node);\r
+              parentNodeMap.put(parentNodeKey, node);\r
               if (null != parentNode)\r
                 parentNode.appendChild(node);\r
             }\r
-            parentNodeKey = parentNodeKey.append(".").append(nodeName);\r
+            parentNodeKey = parentNodeKey + "." + nodeName;\r
             parentNodeName = nodeName;\r
             parentNode = node;\r
           }\r
@@ -169,8 +155,7 @@ public class NetConfSessionUtil {
         // construct leaf node\r
         Element leafNode = doc.createElement(parentNodeName);\r
         leafNode.setTextContent(paramValue);\r
-        if (null != parentNode)\r
-          parentNode.appendChild(leafNode);\r
+        parentNode.appendChild(leafNode);\r
       }\r
 \r
       if (null != dataNode) {\r
@@ -178,16 +163,21 @@ public class NetConfSessionUtil {
         final Element eventTime = doc.createElement(XmlNetconfConstants.EVENT_TIME);\r
         eventTime\r
             .setTextContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date()));\r
-\r
-        final Element evtTypeElement =\r
-            doc.createElementNS(NS_URI, notification.getInformTypeList().get(0).toString());\r
-        evtTypeElement.appendChild(dataNode);\r
         element.appendChild(element.getOwnerDocument().importNode(eventTime, true));\r
-        element.appendChild(element.getOwnerDocument().importNode(evtTypeElement, true));\r
+\r
+        if (netConNotifDTO.getNotificationType() != null) {\r
+          final Element evtTypeElement =\r
+              doc.createElementNS(nameSpace, netConNotifDTO.getNotificationType());\r
+          evtTypeElement.appendChild(dataNode);\r
+          element.appendChild(element.getOwnerDocument().importNode(evtTypeElement, true));\r
+        } else {\r
+          element.appendChild(element.getOwnerDocument().importNode(dataNode, true));\r
+        }\r
+\r
         result = convertDocumentToString(element);\r
       }\r
     } catch (ParserConfigurationException pce) {\r
-      LOG.error("Error while getNetconfResponseXML {}", pce.toString());\r
+      pce.printStackTrace();\r
     }\r
 \r
     return result;\r
@@ -197,17 +187,15 @@ public class NetConfSessionUtil {
     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
+      LOG.error("Error while converting Element to String" + e);\r
     }\r
-    LOG.debug("Converted XML is : {}", strxml);\r
+    LOG.debug("Converted XML is : " + strxml);\r
     return strxml;\r
   }\r
 \r