4fd08e08f1b2fb037e8c624aacc212cb532393f1
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / GetConfigOperation.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.netconf.rpc;\r
20 \r
21 import java.io.File;\r
22 import java.io.IOException;\r
23 import java.util.Optional;\r
24 \r
25 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;\r
26 import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
27 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
28 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
29 import org.opendaylight.netconf.api.DocumentedException;\r
30 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
31 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
32 import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
33 import org.opendaylight.netconf.api.xml.XmlElement;\r
34 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
35 import org.opendaylight.netconf.api.xml.XmlUtil;\r
36 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;\r
37 import org.slf4j.Logger;\r
38 import org.slf4j.LoggerFactory;\r
39 import org.w3c.dom.Document;\r
40 import org.w3c.dom.Element;\r
41 import org.xml.sax.SAXException;\r
42 \r
43 public class GetConfigOperation extends AbstractLastNetconfOperation {\r
44   private static final Logger logger = LoggerFactory.getLogger(GetConfigOperation.class);\r
45 \r
46   private String deviceID;\r
47   private String swVersion;\r
48   private String hwVersion;\r
49 \r
50   public GetConfigOperation(final String netconfSessionIdForReporting,\r
51       final Optional<File> initialConfigXMLFile, String deviceID, String swVersion,\r
52       String hwVersion) {\r
53     super(netconfSessionIdForReporting);\r
54     this.deviceID = deviceID;\r
55     this.swVersion = swVersion;\r
56     this.hwVersion = hwVersion;\r
57     if (initialConfigXMLFile.isPresent()) {\r
58       logger.info("File is present: {}", initialConfigXMLFile.get().getName());\r
59     }\r
60   }\r
61 \r
62   @Override\r
63   protected Element handleWithNoSubsequentOperations(final Document document,\r
64       final XmlElement operationElement) throws DocumentedException {\r
65     final Element element = document.createElement(XmlNetconfConstants.DATA_KEY);\r
66 \r
67     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
68     logger.debug("netconf request recevied : {}", requestXml);\r
69     NetConfServerProperties config =\r
70         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
71 \r
72     final String baseUrl = config.getMapperPath() + "/getConfig";\r
73     NetConfResponse restResponse =\r
74         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
75 \r
76     if (restResponse != null) {\r
77       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
78       if (errorCode != null && errorCode.getFaultCode() != null\r
79           && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
80         logger.error("Error received : {} ", errorCode);\r
81         throw new DocumentedException(errorCode.getErrorMessage(),\r
82             ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
83             ErrorSeverity.from(errorCode.getErrorSeverity()));\r
84       } else if (restResponse.getNetconfResponseXml() != null) {\r
85         Element element1 = null;\r
86         try {\r
87           logger.debug("Response received from mapper :{}", restResponse.getNetconfResponseXml());\r
88           element1 = XmlUtil.readXmlToElement(restResponse.getNetconfResponseXml());\r
89           XmlElement xmlElement = XmlElement.fromDomElement(element1);\r
90           Element domElement = xmlElement.getDomElement();\r
91           element.appendChild(element.getOwnerDocument().importNode(domElement, true));\r
92         } catch (SAXException | IOException e1) {\r
93           logger.error("Error while constructing the reponse {}", e1.toString());\r
94         }\r
95       }\r
96     } else {\r
97       logger.error("received the null response from mapper ");\r
98       throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
99           ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
100     }\r
101 \r
102     return element;\r
103   }\r
104 \r
105   @Override\r
106   protected String getOperationName() {\r
107     return XmlNetconfConstants.GET_CONFIG;\r
108   }\r
109 }\r