Initial source code
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / GetConfigOperation.java
diff --git a/netconf-server/src/main/java/org/commscope/tr069adapter/netconf/rpc/GetConfigOperation.java b/netconf-server/src/main/java/org/commscope/tr069adapter/netconf/rpc/GetConfigOperation.java
new file mode 100644 (file)
index 0000000..edbd799
--- /dev/null
@@ -0,0 +1,103 @@
+/*\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.File;\r
+import java.io.IOException;\r
+import java.util.Optional;\r
+\r
+import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;\r
+import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
+import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
+import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
+import org.opendaylight.netconf.api.DocumentedException;\r
+import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
+import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
+import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
+import org.opendaylight.netconf.api.xml.XmlElement;\r
+import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
+import org.opendaylight.netconf.api.xml.XmlUtil;\r
+import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.w3c.dom.Document;\r
+import org.w3c.dom.Element;\r
+import org.xml.sax.SAXException;\r
+\r
+public class GetConfigOperation extends AbstractLastNetconfOperation {\r
+  private static final Logger logger = LoggerFactory.getLogger(GetConfigOperation.class);\r
+\r
+  private String deviceID;\r
+\r
+  public GetConfigOperation(final String netconfSessionIdForReporting,\r
+      final Optional<File> initialConfigXMLFile, String deviceID) {\r
+    super(netconfSessionIdForReporting);\r
+    this.deviceID = deviceID;\r
+    if (initialConfigXMLFile.isPresent()) {\r
+      logger.info("File is present: {}", initialConfigXMLFile.get().getName());\r
+    }\r
+  }\r
+\r
+  @Override\r
+  protected Element handleWithNoSubsequentOperations(final Document document,\r
+      final XmlElement operationElement) throws DocumentedException {\r
+    final Element element = document.createElement(XmlNetconfConstants.DATA_KEY);\r
+\r
+    String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
+    logger.debug("netconf request recevied : {}", requestXml);\r
+    NetConfServerProperties config =\r
+        NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
+\r
+    final String baseUrl = config.getMapperPath() + "/getConfig";\r
+    NetConfResponse restResponse = XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
+\r
+    if (restResponse != null) {\r
+      ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
+      if (errorCode != null && errorCode.getFaultCode() != null\r
+          && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
+        logger.error("Error received : {} ", errorCode);\r
+        throw new DocumentedException(errorCode.getErrorMessage(),\r
+            ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
+            ErrorSeverity.from(errorCode.getErrorSeverity()));\r
+      } else if (restResponse.getNetconfResponseXml() != null) {\r
+        Element element1 = null;\r
+        try {\r
+          logger.debug("Response received from mapper :{}", restResponse.getNetconfResponseXml());\r
+          element1 = XmlUtil.readXmlToElement(restResponse.getNetconfResponseXml());\r
+          XmlElement xmlElement = XmlElement.fromDomElement(element1);\r
+          Element domElement = xmlElement.getDomElement();\r
+          element.appendChild(element.getOwnerDocument().importNode(domElement, true));\r
+        } catch (SAXException | IOException e1) {\r
+          logger.error("Error while constructing the reponse {}", e1.toString());\r
+        }\r
+      }\r
+    } else {\r
+      logger.error("received the null response from mapper ");\r
+      throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
+          ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
+    }\r
+\r
+    return element;\r
+  }\r
+\r
+  @Override\r
+  protected String getOperationName() {\r
+    return XmlNetconfConstants.GET_CONFIG;\r
+  }\r
+}\r