netconf response preparing for 2sector
[oam/tr069-adapter.git] / mapper / src / main / java / org / commscope / tr069adapter / mapper / util / NetconfToTr069MapperUtil.java
index b7fa188..81245d0 100644 (file)
@@ -28,7 +28,6 @@ import java.util.HashMap;
 import java.util.List;\r
 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
@@ -37,7 +36,6 @@ import javax.xml.transform.Transformer;
 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.acs.common.DeviceRPCRequest;\r
 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;\r
 import org.commscope.tr069adapter.acs.common.OperationOptions;\r
@@ -63,7 +61,7 @@ public class NetconfToTr069MapperUtil {
 \r
   @Autowired\r
   MOMetaDataUtil metaDataUtil;\r
-  \r
+\r
   @Autowired\r
   MapperConfigProperties config;\r
 \r
@@ -154,7 +152,8 @@ public class NetconfToTr069MapperUtil {
     return deviceRPCRequest;\r
   }\r
 \r
-  public NetConfResponse getNetconfResponse(DeviceRPCResponse opResult, boolean isCustomparameter) {\r
+  public NetConfResponse getNetconfResponse(DeviceRPCResponse opResult, String swVersion,\r
+      String hwVersion, boolean isCustomparameter) {\r
     NetConfResponse netConfResponse = new NetConfResponse();\r
     ErrorCodeDetails errorCodeDetails = errorCodeUtil.getErrorCodeMetaData(opResult.getFaultKey());\r
     ErrorCodeDetails errorCode = new ErrorCodeDetails();\r
@@ -175,12 +174,14 @@ public class NetconfToTr069MapperUtil {
       netConfResponse.setErrorCode(errorCode);\r
       netConfResponse.setErrorMessage(opResult.getFaultString());\r
     }\r
-    netConfResponse.setNetconfResponseXml(getNetconfResponseXML(\r
-        opResult.getOperationResponse().getParameterDTOs(), isCustomparameter));\r
+    netConfResponse.setNetconfResponseXml(\r
+        getNetconfResponseXML(opResult.getOperationResponse().getParameterDTOs(), swVersion,\r
+            hwVersion, isCustomparameter));\r
     return netConfResponse;\r
   }\r
 \r
-  public NetConfResponse getNetconfResponseForSoftwareInventory(DeviceRPCResponse opResult) {\r
+  public NetConfResponse getNetconfResponseForSoftwareInventory(DeviceRPCResponse opResult,\r
+      String swVersion, String hwVersion) {\r
 \r
     NetConfResponse netConfResponse = new NetConfResponse();\r
     ErrorCodeDetails errorCodeDetails = errorCodeUtil.getErrorCodeMetaData(opResult.getFaultKey());\r
@@ -234,14 +235,15 @@ public class NetconfToTr069MapperUtil {
     paramDTOList.add(new ParameterDTO("software-inventory.software-slot.access", "READ_ONLY"));\r
     paramDTOList\r
         .add(new ParameterDTO("software-inventory.software-slot.product-code", productClass));\r
-    paramDTOList.add(new ParameterDTO("software-inventory.software-slot.vendor-code", config.getVendorName()));\r
+    paramDTOList.add(\r
+        new ParameterDTO("software-inventory.software-slot.vendor-code", config.getVendorName()));\r
     paramDTOList.add(new ParameterDTO("software-inventory.software-slot.build-id", buildId));\r
     paramDTOList.add(new ParameterDTO("software-inventory.software-slot.build-version",\r
         buildVersion.toString()));\r
     paramDTOList.add(new ParameterDTO("software-inventory.software-slot.files.name", "BC_ONE"));\r
     paramDTOList.add(new ParameterDTO("software-inventory.software-slot.files.integrity", "OK"));\r
 \r
-    String XmlStr = getNetconfResponseXML(paramDTOList, true);\r
+    String XmlStr = getNetconfResponseXML(paramDTOList, swVersion, hwVersion, true);\r
 \r
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
     DocumentBuilder builder;\r
@@ -296,9 +298,10 @@ public class NetconfToTr069MapperUtil {
     return netConfResponse;\r
   }\r
 \r
-  private String getNetconfResponseXML(List<ParameterDTO> parameters, boolean isCustomparameter) {\r
+  private String getNetconfResponseXML(List<ParameterDTO> parameters, String swVersion,\r
+      String hwVersion, boolean isCustomparameter) {\r
     if (null == parameters || parameters.isEmpty()) {\r
-\r
+      // LOG.debug("There are no parameters found in the response.");\r
       return null;\r
     }\r
     Collections.sort(parameters, new SortByParamterName());\r
@@ -306,8 +309,6 @@ public class NetconfToTr069MapperUtil {
     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
@@ -315,14 +316,13 @@ public class NetconfToTr069MapperUtil {
       Element dataNode = null; // root of all nodes\r
 \r
       for (ParameterDTO paramDto : parameters) {\r
-        String paramName =\r
-            metaDataUtil.getNetconfNameByTR69NameWithIndexes(paramDto.getParamName());\r
+        String paramName = metaDataUtil.getNetconfNameByTR69NameWithIndexes(paramDto.getParamName(),\r
+            swVersion, hwVersion);\r
         if (paramName == null && isCustomparameter) {\r
           paramName = paramDto.getParamName();\r
         }\r
         String paramValue = paramDto.getParamValue();\r
         if (paramValue == null || paramValue.trim().isEmpty()) {\r
-          logger.debug("Values is empty so skipping this parameter.");\r
           continue;\r
         }\r
         StringTokenizer tokenizer = new StringTokenizer(paramName, ".");\r
@@ -340,24 +340,24 @@ public class NetconfToTr069MapperUtil {
             if (null == dataNode) {\r
               dataNode = parentNode;\r
             }\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
-            StringBuilder bld = new StringBuilder(parentNodeKey);\r
-            bld.append(".");\r
-            bld.append(nodeName);\r
-            parentNodeKey = bld.toString();\r
-            Element node = parentNodeMap.computeIfPresent(parentNodeKey, (k, v) -> v);\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
-              if (metaDataUtil.getMetaDataByNetConfName(parentNodeKey + ".") != null\r
-                  && metaDataUtil.getMetaDataByNetConfName(parentNodeKey + ".").getURI() != null) {\r
-                node = doc.createElementNS(\r
-                    metaDataUtil.getMetaDataByNetConfName(parentNodeKey + ".").getURI(),\r
+              if (metaDataUtil.getMetaDataByNetConfName(parentNodeKey + ".", swVersion,\r
+                  hwVersion) != null\r
+                  && metaDataUtil\r
+                      .getMetaDataByNetConfName(parentNodeKey + ".", swVersion, hwVersion)\r
+                      .getURI() != null) {\r
+                node = doc.createElementNS(metaDataUtil\r
+                    .getMetaDataByNetConfName(parentNodeKey + ".", swVersion, hwVersion).getURI(),\r
                     parentNodeName);\r
               } else {\r
                 node = doc.createElement(parentNodeName);\r
@@ -383,19 +383,18 @@ public class NetconfToTr069MapperUtil {
                                                             // attribute\r
                                                             // is\r
                                                             // found\r
-            StringBuilder bld = new StringBuilder(parentNodeName);\r
-            bld.append(".");\r
-            bld.append(nodeName);\r
-            parentNodeKey = bld.toString();\r
+            parentNodeKey = parentNodeKey + "." + nodeName;\r
             parentNodeName = nodeName;\r
           } else {\r
             // construct intermediate nodes\r
             Element node = parentNodeMap.get(parentNodeKey);\r
             if (null == node) {\r
-              if (metaDataUtil.getMetaDataByNetConfName(parentNodeKey) != null\r
-                  && metaDataUtil.getMetaDataByNetConfName(parentNodeKey).getURI() != null) {\r
-                node = doc.createElementNS(\r
-                    metaDataUtil.getMetaDataByNetConfName(parentNodeKey).getURI(), parentNodeName);\r
+              if (metaDataUtil.getMetaDataByNetConfName(parentNodeKey, swVersion, hwVersion) != null\r
+                  && metaDataUtil.getMetaDataByNetConfName(parentNodeKey, swVersion, hwVersion)\r
+                      .getURI() != null) {\r
+                node = doc.createElementNS(metaDataUtil\r
+                    .getMetaDataByNetConfName(parentNodeKey, swVersion, hwVersion).getURI(),\r
+                    parentNodeName);\r
                 if (dataNode == null)\r
                   dataNode = node;\r
               } else {\r
@@ -406,10 +405,7 @@ public class NetconfToTr069MapperUtil {
                 parentNode.appendChild(node);\r
 \r
             }\r
-            StringBuilder bld = new StringBuilder(parentNodeKey);\r
-            bld.append(".");\r
-            bld.append(nodeName);\r
-            parentNodeKey = bld.toString();\r
+            parentNodeKey = parentNodeKey + "." + nodeName;\r
             parentNodeName = nodeName;\r
             parentNode = node;\r
           }\r
@@ -417,15 +413,15 @@ public class NetconfToTr069MapperUtil {
         // construct leaf node\r
         Element leafNode = doc.createElement(parentNodeName);\r
         leafNode.setTextContent(paramValue);\r
-        if (null != parentNode)\r
+        if (parentNode != null)\r
           parentNode.appendChild(leafNode);\r
       }\r
 \r
       if (null != dataNode) {\r
-        result = NetconfToTr069MapperUtil.convertDocumentToString(dataNode);\r
+        result = convertDocumentToString(dataNode);\r
       }\r
     } catch (ParserConfigurationException pce) {\r
-      logger.error("Exception : {}", pce.getMessage());\r
+      logger.error("Response xml formatting is failed : {} ", pce.toString());\r
     }\r
 \r
     return result;\r