sonar code issues addressed
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / GenericOperation.java
1 package org.commscope.tr069adapter.netconf.rpc;
2
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;
19
20 import javax.xml.XMLConstants;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import java.io.StringReader;
24
25
26 public abstract class GenericOperation implements NetconfOperation {
27     private static final Logger logger = LoggerFactory.getLogger(GenericOperation.class);
28
29     private String opNamespace;
30     private String opName;
31     private String opString;
32
33     protected String deviceID;
34     protected String swVersion;
35     protected String hwVersion;
36
37     public HandlingPriority canHandle(final Document message) throws DocumentedException {
38         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
39         return canHandle(operationNameAndNamespace.getOperationName(),
40             operationNameAndNamespace.getNamespace());
41     }
42
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;
49     }
50
51     public Document handle(Document requestMessage,
52         NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
53         logger.debug("rpc is received in netconfserver");
54
55         final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
56
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);
63
64         String requestXml = XmlUtility.convertDocumentToString(requestElement);
65         logger.debug("rpc requestXml= {}", requestXml);
66
67         NetConfServerProperties config =
68             NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
69
70         final String baseUrl = config.getMapperPath() + "/" + getOpString();
71         NetConfResponse restResponse =
72             XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
73         Document document = null;
74
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, "");
89             try {
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
98                     .from("ERROR"));
99             }
100         }
101         return document;
102     }
103
104     protected Document getDocumentDetails(DocumentBuilderFactory factory, String xmlStr)
105         throws DocumentedException {
106         DocumentBuilder builder;
107         Document document;
108         try {
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"));
115         }
116         return document;
117     }
118
119     public static final class OperationNameAndNamespace {
120         private final String operationName;
121         private final String namespace;
122
123         private final XmlElement operationElement;
124
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();
131         }
132
133         public String getOperationName() {
134             return operationName;
135         }
136
137         public String getNamespace() {
138             return namespace;
139         }
140
141         public XmlElement getOperationElement() {
142             return operationElement;
143         }
144
145     }
146
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);
151     }
152
153     public String getOpNamespace() {
154         return opNamespace;
155     }
156
157     public void setOpNamespace(String opNamespace) {
158         this.opNamespace = opNamespace;
159     }
160
161     public String getOpName() {
162         return opName;
163     }
164
165     public void setOpName(String opName) {
166         this.opName = opName;
167     }
168
169     public String getOpString() {
170         return opString;
171     }
172
173     public void setOpString(String opString) {
174         this.opString = opString;
175     }
176 }