sonar code issues addressed
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / DeleteObjectOperation.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 DeleteObjectOperation extends GenericOperation {
44   private static final Logger logger = LoggerFactory.getLogger(DeleteObjectOperation.class);
45
46   public DeleteObjectOperation(String deviceID, String swVersion, String hwVersion) {
47     this.deviceID = deviceID;
48     this.swVersion = swVersion;
49     this.hwVersion = hwVersion;
50     setOpString("deleteobject");
51     setOpNamespace("urn:tr069rpc:1.0");
52     setOpName("delete-object");
53   }
54
55   @Override
56   public Document handle(Document requestMessage,
57       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
58     logger.debug("DeleteObject rpc is received in netconfserver");
59
60     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
61
62     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
63
64
65     String requestXml = XmlUtility.convertDocumentToString(requestElement);
66     logger.debug("DeleteObject rpc requestXml={}", requestXml);
67
68
69     NetConfServerProperties config =
70         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
71
72     final String baseUrl = config.getMapperPath() + "/" + getOpString();
73     NetConfResponse restResponse =
74         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
75     Document document = null;
76
77     ErrorCodeDetails errorCode = restResponse.getErrorCode();
78     if (errorCode != null && errorCode.getFaultCode() != null
79         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
80       logger.error("Error recevied : {}", 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       logger.debug("deleteobject rpc response received from mapper {}",
86           restResponse.getNetconfResponseXml());
87       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
88       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
89       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
90       DocumentBuilder builder;
91       try {
92         builder = factory.newDocumentBuilder();
93         document =
94             builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));
95         document.getDocumentElement().setAttribute("xmlns:ns1", getOpNamespace());
96         document.getDocumentElement().setAttribute("xmlns",
97             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
98         document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
99       } catch (Exception e) {
100         logger.error("while contruscting the response {}", e.getMessage());
101         throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
102             ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
103       }
104     }
105
106     return document;
107   }
108 }