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