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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=======================================================================
19 package org.commscope.tr069adapter.netconf.rpc;
21 import java.io.StringReader;
23 import javax.xml.XMLConstants;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
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.HandlingPriority;
38 import org.opendaylight.netconf.mapping.api.NetconfOperation;
39 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
45 public class DeleteObjectOperation implements NetconfOperation {
46 private static final Logger logger = LoggerFactory.getLogger(DeleteObjectOperation.class);
47 public static final String OP_NAMESPACE = "urn:tr069rpc:1.0";
48 public static final String OP_NAME = "delete-object";
50 private String deviceID;
51 private String swVersion;
52 private String hwVersion;
54 public DeleteObjectOperation(String deviceID, String swVersion, String hwVersion) {
55 this.deviceID = deviceID;
56 this.swVersion = swVersion;
57 this.hwVersion = hwVersion;
61 public HandlingPriority canHandle(final Document message) throws DocumentedException {
62 OperationNameAndNamespace operationNameAndNamespace = null;
63 operationNameAndNamespace = new OperationNameAndNamespace(message);
64 return canHandle(operationNameAndNamespace.getOperationName(),
65 operationNameAndNamespace.getNamespace());
69 public Document handle(Document requestMessage,
70 NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
71 logger.debug("DeleteObject rpc is received in netconfserver");
73 final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
75 final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
78 String requestXml = XmlUtility.convertDocumentToString(requestElement);
79 logger.debug("DeleteObject rpc requestXml={}", requestXml);
82 NetConfServerProperties config =
83 NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
85 final String baseUrl = config.getMapperPath() + "/deleteobject";
86 NetConfResponse restResponse =
87 XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
88 Document document = null;
90 ErrorCodeDetails errorCode = restResponse.getErrorCode();
91 if (errorCode != null && errorCode.getFaultCode() != null
92 && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
93 logger.error("Error recevied : {}", errorCode);
94 throw new DocumentedException(errorCode.getErrorMessage(),
95 ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),
96 ErrorSeverity.from(errorCode.getErrorSeverity()));
97 } else if (restResponse.getNetconfResponseXml() != null) {
98 logger.debug("deleteobject rpc response received from mapper {}",
99 restResponse.getNetconfResponseXml());
100 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
101 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
102 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
103 DocumentBuilder builder;
105 builder = factory.newDocumentBuilder();
107 builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));
108 document.getDocumentElement().setAttribute("xmlns:ns1", getOperationNamespace());
109 document.getDocumentElement().setAttribute("xmlns",
110 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
111 document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
112 } catch (Exception e) {
113 logger.error("while contruscting the response {}", e.getMessage());
114 throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
115 ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
122 protected HandlingPriority canHandle(final String operationName,
123 final String operationNamespace) {
124 return operationName.equals(getOperationName())
125 && operationNamespace.equals(getOperationNamespace())
126 ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)
127 : HandlingPriority.CANNOT_HANDLE;
130 public static final class OperationNameAndNamespace {
131 private final String operationName;
132 private final String namespace;
134 private final XmlElement operationElement;
136 public OperationNameAndNamespace(final Document message) throws DocumentedException {
137 XmlElement requestElement = null;
138 requestElement = getRequestElementWithCheck(message);
139 operationElement = requestElement.getOnlyChildElement();
140 operationName = operationElement.getName();
141 namespace = operationElement.getNamespace();
144 public String getOperationName() {
145 return operationName;
148 public String getNamespace() {
152 public XmlElement getOperationElement() {
153 return operationElement;
158 protected static XmlElement getRequestElementWithCheck(final Document message)
159 throws DocumentedException {
160 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
161 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
164 protected String getOperationNamespace() {
168 protected String getOperationName() {