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