sonar code issues addressed
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / GPAObjectOperation.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.StringReader;
22
23 import javax.xml.XMLConstants;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26
27 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;
28 import org.commscope.tr069adapter.mapper.model.NetConfResponse;
29 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
30 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
31 import org.opendaylight.netconf.api.DocumentedException;
32 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
33 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
34 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
35 import org.opendaylight.netconf.api.xml.XmlElement;
36 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
37 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Document;
41 import org.xml.sax.InputSource;
42
43 public class GPAObjectOperation extends GenericOperation {
44   private static final Logger logger = LoggerFactory.getLogger(GPAObjectOperation.class);
45
46   public GPAObjectOperation(String deviceID, String swVersion, String hwVersion) {
47     this.deviceID = deviceID;
48     this.swVersion = swVersion;
49     this.hwVersion = hwVersion;
50     setOpString("gpaobject");
51     setOpNamespace("urn:tr069rpc:1.0");
52     setOpName("get-parameter-attributes");
53   }
54
55   @Override
56   public Document handle(Document requestMessage,
57       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
58     logger.debug("gpaObject rpc is received in netconfserver");
59
60     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
61     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
62
63     String requestXml = XmlUtility.convertDocumentToString(requestElement);
64     logger.debug("gpaObject rpc requestXml= {}", requestXml);
65
66     NetConfServerProperties config =
67         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
68
69     final String baseUrl = config.getMapperPath() + "/" + getOpString();
70     NetConfResponse restResponse =
71         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
72     Document document = null;
73
74     ErrorCodeDetails errorCode = restResponse.getErrorCode();
75     if (errorCode != null && errorCode.getFaultCode() != null
76         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
77       logger.error("Error recevied : {}", errorCode);
78       throw new DocumentedException(errorCode.getErrorMessage(),
79           ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),
80           ErrorSeverity.from(errorCode.getErrorSeverity()));
81     } else if (restResponse.getNetconfResponseXml() != null) {
82       logger.debug("gpaobject rpc response received from mapper {}",
83           restResponse.getNetconfResponseXml());
84       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
86       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
87       DocumentBuilder builder;
88       try {
89         builder = factory.newDocumentBuilder();
90         document =
91             builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));
92         document.getDocumentElement().setAttribute("xmlns:ns1", getOpNamespace());
93         document.getDocumentElement().setAttribute("xmlns",
94             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
95         document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
96       } catch (Exception e) {
97         logger.error("Error while contruscting the response: {}", e.getMessage());
98         throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
99             ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
100       }
101     }
102
103     return document;
104   }
105
106 }