edbd7998929d180011e228b9554e574321b08bdb
[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 \r
48   public GetConfigOperation(final String netconfSessionIdForReporting,\r
49       final Optional<File> initialConfigXMLFile, String deviceID) {\r
50     super(netconfSessionIdForReporting);\r
51     this.deviceID = deviceID;\r
52     if (initialConfigXMLFile.isPresent()) {\r
53       logger.info("File is present: {}", initialConfigXMLFile.get().getName());\r
54     }\r
55   }\r
56 \r
57   @Override\r
58   protected Element handleWithNoSubsequentOperations(final Document document,\r
59       final XmlElement operationElement) throws DocumentedException {\r
60     final Element element = document.createElement(XmlNetconfConstants.DATA_KEY);\r
61 \r
62     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
63     logger.debug("netconf request recevied : {}", requestXml);\r
64     NetConfServerProperties config =\r
65         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
66 \r
67     final String baseUrl = config.getMapperPath() + "/getConfig";\r
68     NetConfResponse restResponse = XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
69 \r
70     if (restResponse != null) {\r
71       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
72       if (errorCode != null && errorCode.getFaultCode() != null\r
73           && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
74         logger.error("Error received : {} ", errorCode);\r
75         throw new DocumentedException(errorCode.getErrorMessage(),\r
76             ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
77             ErrorSeverity.from(errorCode.getErrorSeverity()));\r
78       } else if (restResponse.getNetconfResponseXml() != null) {\r
79         Element element1 = null;\r
80         try {\r
81           logger.debug("Response received from mapper :{}", restResponse.getNetconfResponseXml());\r
82           element1 = XmlUtil.readXmlToElement(restResponse.getNetconfResponseXml());\r
83           XmlElement xmlElement = XmlElement.fromDomElement(element1);\r
84           Element domElement = xmlElement.getDomElement();\r
85           element.appendChild(element.getOwnerDocument().importNode(domElement, true));\r
86         } catch (SAXException | IOException e1) {\r
87           logger.error("Error while constructing the reponse {}", e1.toString());\r
88         }\r
89       }\r
90     } else {\r
91       logger.error("received the null response from mapper ");\r
92       throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
93           ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
94     }\r
95 \r
96     return element;\r
97   }\r
98 \r
99   @Override\r
100   protected String getOperationName() {\r
101     return XmlNetconfConstants.GET_CONFIG;\r
102   }\r
103 }\r