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