1 package org.commscope.tr069adapter.netconf.rpc;
3 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;
4 import org.commscope.tr069adapter.mapper.model.NetConfResponse;
5 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
6 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
7 import org.opendaylight.netconf.api.DocumentedException;
8 import org.opendaylight.netconf.api.xml.XmlElement;
9 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
10 import org.opendaylight.netconf.mapping.api.HandlingPriority;
11 import org.opendaylight.netconf.mapping.api.NetconfOperation;
12 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17 import org.w3c.dom.Node;
18 import org.xml.sax.InputSource;
20 import javax.xml.XMLConstants;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import java.io.StringReader;
26 public abstract class GenericOperation implements NetconfOperation {
27 private static final Logger logger = LoggerFactory.getLogger(GenericOperation.class);
29 private String opNamespace;
30 private String opName;
31 private String opString;
33 protected String deviceID;
34 protected String swVersion;
35 protected String hwVersion;
37 public HandlingPriority canHandle(final Document message) throws DocumentedException {
38 OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
39 return canHandle(operationNameAndNamespace.getOperationName(),
40 operationNameAndNamespace.getNamespace());
43 protected HandlingPriority canHandle(final String operationName,
44 final String operationNamespace) {
45 return operationName.equals(getOpName())
46 && operationNamespace.equals(getOpNamespace())
47 ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)
48 : HandlingPriority.CANNOT_HANDLE;
51 public Document handle(Document requestMessage,
52 NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
53 logger.debug("rpc is received in netconfserver");
55 final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
57 final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
58 final Element element =
59 requestMessage.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
60 XmlNetconfConstants.RPC_REPLY_KEY);
61 element.setAttribute("xmlns:ns1", getOpNamespace());
62 element.setAttribute("message-id", msgId);
64 String requestXml = XmlUtility.convertDocumentToString(requestElement);
65 logger.debug("rpc requestXml= {}", requestXml);
67 NetConfServerProperties config =
68 NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
70 final String baseUrl = config.getMapperPath() + "/" + getOpString();
71 NetConfResponse restResponse =
72 XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
73 Document document = null;
75 ErrorCodeDetails errorCode = restResponse.getErrorCode();
76 if (errorCode != null && errorCode.getFaultCode() != null
77 && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
78 logger.error("Error recevied : {}", errorCode);
79 throw new DocumentedException(errorCode.getErrorMessage(),
80 DocumentedException.ErrorType.from(errorCode.getErrorType()), DocumentedException.ErrorTag
81 .from(errorCode.getErrorTag()),
82 DocumentedException.ErrorSeverity.from(errorCode.getErrorSeverity()));
83 } else if (restResponse.getNetconfResponseXml() != null) {
84 logger.debug("rpc response received from mapper: {}",
85 restResponse.getNetconfResponseXml());
86 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
87 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
88 factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
90 Node child = requestMessage.createElement(XmlNetconfConstants.OK);
91 element.appendChild(child);
92 String xmlStr = XmlUtility.convertDocumentToString(element);
93 document = getDocumentDetails(factory, xmlStr);
94 } catch (Exception e) {
95 logger.error("Error while contruscting the response: {}", e.getMessage());
96 throw new DocumentedException("Operation Aborted", DocumentedException.ErrorType.from("application"),
97 DocumentedException.ErrorTag.from("operation-failed"), DocumentedException.ErrorSeverity
104 protected Document getDocumentDetails(DocumentBuilderFactory factory, String xmlStr)
105 throws DocumentedException {
106 DocumentBuilder builder;
109 builder = factory.newDocumentBuilder();
110 document = builder.parse(new InputSource(new StringReader(xmlStr)));
111 } catch (Exception e) {
112 logger.error("Error while converting String to element: {}", e.getMessage());
113 throw new DocumentedException("Operation Aborted", DocumentedException.ErrorType.from("application"),
114 DocumentedException.ErrorTag.from("operation-failed"), DocumentedException.ErrorSeverity.from("ERROR"));
119 public static final class OperationNameAndNamespace {
120 private final String operationName;
121 private final String namespace;
123 private final XmlElement operationElement;
125 public OperationNameAndNamespace(final Document message) throws DocumentedException {
126 XmlElement requestElement = null;
127 requestElement = getRequestElementWithCheck(message);
128 operationElement = requestElement.getOnlyChildElement();
129 operationName = operationElement.getName();
130 namespace = operationElement.getNamespace();
133 public String getOperationName() {
134 return operationName;
137 public String getNamespace() {
141 public XmlElement getOperationElement() {
142 return operationElement;
147 protected static XmlElement getRequestElementWithCheck(final Document message)
148 throws DocumentedException {
149 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
150 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
153 public String getOpNamespace() {
157 public void setOpNamespace(String opNamespace) {
158 this.opNamespace = opNamespace;
161 public String getOpName() {
165 public void setOpName(String opName) {
166 this.opName = opName;
169 public String getOpString() {
173 public void setOpString(String opString) {
174 this.opString = opString;