47cc0ea5235d721a39dabbca56c45a32618fddaf
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / GetOperation.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.IOException;\r
22 \r
23 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;\r
24 import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
25 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
26 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
27 import org.opendaylight.netconf.api.DocumentedException;\r
28 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
29 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
30 import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
31 import org.opendaylight.netconf.api.xml.XmlElement;\r
32 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
33 import org.opendaylight.netconf.api.xml.XmlUtil;\r
34 import org.opendaylight.netconf.test.tool.rpc.DataList;\r
35 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;\r
36 import org.slf4j.Logger;\r
37 import org.slf4j.LoggerFactory;\r
38 import org.w3c.dom.Document;\r
39 import org.w3c.dom.Element;\r
40 import org.xml.sax.SAXException;\r
41 \r
42 public class GetOperation extends AbstractLastNetconfOperation {\r
43   private static final Logger logger = LoggerFactory.getLogger(GetOperation.class);\r
44 \r
45   private final DataList storage;\r
46   private String deviceID;\r
47   private String swVersion;\r
48   private String hwVersion;\r
49 \r
50   public GetOperation(final String netconfSessionIdForReporting, final DataList storage,\r
51       String deviceID, String swVersion, String hwVersion) {\r
52     super(netconfSessionIdForReporting);\r
53     this.deviceID = deviceID;\r
54     this.storage = storage;\r
55     this.swVersion = swVersion;\r
56     this.hwVersion = hwVersion;\r
57   }\r
58 \r
59   @Override\r
60   protected Element handleWithNoSubsequentOperations(final Document document,\r
61       final XmlElement operationElement) throws DocumentedException {\r
62     final Element element = document.createElement(XmlNetconfConstants.DATA_KEY);\r
63 \r
64     for (final XmlElement e : storage.getConfigList()) {\r
65       final Element domElement = e.getDomElement();\r
66       element.appendChild(element.getOwnerDocument().importNode(domElement, true));\r
67     }\r
68 \r
69     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
70     logger.debug("netconf request recevied : {}", requestXml);\r
71     NetConfServerProperties config =\r
72         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
73 \r
74     final String baseUrl = config.getMapperPath() + "/get";\r
75     NetConfResponse restResponse =\r
76         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
77 \r
78     if (restResponse != null) {\r
79       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
80       if (errorCode != null && errorCode.getFaultCode() != null\r
81           && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
82         throw new DocumentedException(errorCode.getErrorMessage(),\r
83             ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
84             ErrorSeverity.from(errorCode.getErrorSeverity()));\r
85       } else if (restResponse.getNetconfResponseXml() != null) {\r
86         Element element1 = null;\r
87         try {\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     return element;\r
102   }\r
103 \r
104   @Override\r
105   protected String getOperationName() {\r
106     return XmlNetconfConstants.GET;\r
107   }\r
108 }\r