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.NetconfOperationChainedExecution;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Document;
41 import org.xml.sax.InputSource;
43 public class AddObjectOperation extends GenericOperation {
44 private static final Logger logger = LoggerFactory.getLogger(AddObjectOperation.class);
46 public AddObjectOperation(String deviceID, String swVersion, String hwVersion) {
47 this.deviceID = deviceID;
48 this.swVersion = swVersion;
49 this.hwVersion = hwVersion;
50 setOpString("addobject");
51 setOpNamespace("urn:tr069rpc:1.0");
52 setOpName("add-object");
56 public Document handle(Document requestMessage,
57 NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
58 logger.debug("AddObject rpc is received in netconfserver");
60 final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
62 final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
65 String requestXml = XmlUtility.convertDocumentToString(requestElement);
66 logger.debug("AddObject rpc requestXml={}", requestXml);
69 NetConfServerProperties config =
70 NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
72 final String baseUrl = config.getMapperPath() + "/" + getOpString();
73 NetConfResponse restResponse =
74 XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
75 Document document = null;
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("addobject 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;
92 builder = factory.newDocumentBuilder();
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"));