Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / ConnectionStatus.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.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;
44
45 public class ConnectionStatus implements NetconfOperation {
46   private static final Logger logger = LoggerFactory.getLogger(ConnectionStatus.class);
47   public static final String OP_NAMESPACE = "urn:tr069rpc:1.0";
48   public static final String OP_NAME = "connection-status";
49
50   private String deviceID;
51   private String swVersion;
52   private String hwVersion;
53
54   public ConnectionStatus(String deviceID, String swVersion, String hwVersion) {
55     this.deviceID = deviceID;
56     this.swVersion = swVersion;
57     this.hwVersion = hwVersion;
58   }
59
60   @Override
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());
66   }
67
68   @Override
69   public Document handle(Document requestMessage,
70       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
71     logger.debug("ConnectionStatus rpc is received in netconfserver");
72
73     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
74
75     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
76
77
78     String requestXml = XmlUtility.convertDocumentToString(requestElement);
79     logger.debug("ConnectionStatus rpc requestXml= {}", requestXml);
80
81
82     NetConfServerProperties config =
83         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
84
85     final String baseUrl = config.getMapperPath() + "/connectionstatus";
86     NetConfResponse restResponse =
87         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
88     Document document = null;
89
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("ConnectionStatus 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;
104       try {
105         builder = factory.newDocumentBuilder();
106         document =
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("Error while contruscting the response: {}", e.getMessage());
114         throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
115             ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
116       }
117     }
118
119     return document;
120   }
121
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;
128   }
129
130   public static final class OperationNameAndNamespace {
131     private final String operationName;
132     private final String namespace;
133
134     private final XmlElement operationElement;
135
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();
142     }
143
144     public String getOperationName() {
145       return operationName;
146     }
147
148     public String getNamespace() {
149       return namespace;
150     }
151
152     public XmlElement getOperationElement() {
153       return operationElement;
154     }
155
156   }
157
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);
162   }
163
164   protected String getOperationNamespace() {
165     return OP_NAMESPACE;
166   }
167
168   protected String getOperationName() {
169     return OP_NAME;
170   }
171
172 }