VES Heartbeat and Software Management Feature
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / SoftwareDownloadOperation.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.netconf.rpc;\r
20 \r
21 import java.io.StringReader;\r
22 \r
23 import javax.xml.parsers.DocumentBuilder;\r
24 import javax.xml.parsers.DocumentBuilderFactory;\r
25 \r
26 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;\r
27 import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
28 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
29 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
30 import org.opendaylight.netconf.api.DocumentedException;\r
31 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
32 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
33 import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
34 import org.opendaylight.netconf.api.xml.XmlElement;\r
35 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
36 import org.opendaylight.netconf.mapping.api.HandlingPriority;\r
37 import org.opendaylight.netconf.mapping.api.NetconfOperation;\r
38 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;\r
39 import org.slf4j.Logger;\r
40 import org.slf4j.LoggerFactory;\r
41 import org.w3c.dom.Document;\r
42 import org.xml.sax.InputSource;\r
43 \r
44 public class SoftwareDownloadOperation implements NetconfOperation {\r
45   private static final Logger logger = LoggerFactory.getLogger(SoftwareDownloadOperation.class);\r
46   public static final String SOFT_MGMT_NAMESPACE = "urn:o-ran:software-management:1.0";\r
47 \r
48   private String deviceID;\r
49 \r
50   public SoftwareDownloadOperation(String deviceID) {\r
51     this.deviceID = deviceID;\r
52   }\r
53 \r
54   @Override\r
55   public HandlingPriority canHandle(final Document message) throws DocumentedException {\r
56     OperationNameAndNamespace operationNameAndNamespace = null;\r
57     operationNameAndNamespace = new OperationNameAndNamespace(message);\r
58     return canHandle(operationNameAndNamespace.getOperationName(),\r
59         operationNameAndNamespace.getNamespace());\r
60   }\r
61 \r
62   @Override\r
63   public Document handle(Document requestMessage,\r
64       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {\r
65 \r
66     logger.debug("soft-ware download rpc is received in netconfserver");\r
67 \r
68     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);\r
69     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);\r
70 \r
71     String requestXml = XmlUtility.convertDocumentToString(requestElement);\r
72     logger.debug("soft-ware download rpc requestXml=" + requestXml);\r
73 \r
74     NetConfServerProperties config =\r
75         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
76 \r
77     final String baseUrl = config.getMapperPath() + "/softwareDowload";\r
78     NetConfResponse restResponse = XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
79 \r
80     Document document = null;\r
81 \r
82     ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
83     if (errorCode != null && errorCode.getFaultCode() != null\r
84         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
85       logger.error("Error recevied : " + errorCode);\r
86       throw new DocumentedException(errorCode.getErrorMessage(),\r
87           ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
88           ErrorSeverity.from(errorCode.getErrorSeverity()));\r
89     } else if (restResponse != null && restResponse.getNetconfResponseXml() != null) {\r
90       logger.debug("soft-ware download rpc response received from mapper "\r
91           + restResponse.getNetconfResponseXml());\r
92       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
93       DocumentBuilder builder;\r
94       try {\r
95         builder = factory.newDocumentBuilder();\r
96         document =\r
97             builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));\r
98         document.getDocumentElement().setAttribute("xmlns:ns1", getOperationNamespace());\r
99         document.getDocumentElement().setAttribute("xmlns",\r
100             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
101         document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);\r
102       } catch (Exception e) {\r
103         logger.error("while contruscting the response; ", e.toString());\r
104       }\r
105     }\r
106 \r
107     return document;\r
108   }\r
109 \r
110   protected HandlingPriority canHandle(final String operationName,\r
111       final String operationNamespace) {\r
112     return operationName.equals("software-download")\r
113         && operationNamespace.equals(SOFT_MGMT_NAMESPACE)\r
114             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)\r
115             : HandlingPriority.CANNOT_HANDLE;\r
116   }\r
117 \r
118   public static final class OperationNameAndNamespace {\r
119     private final String operationName;\r
120     private final String namespace;\r
121 \r
122     private final XmlElement operationElement;\r
123 \r
124     public OperationNameAndNamespace(final Document message) throws DocumentedException {\r
125       XmlElement requestElement = null;\r
126       requestElement = getRequestElementWithCheck(message);\r
127       operationElement = requestElement.getOnlyChildElement();\r
128       operationName = operationElement.getName();\r
129       namespace = operationElement.getNamespace();\r
130     }\r
131 \r
132     public String getOperationName() {\r
133       return operationName;\r
134     }\r
135 \r
136     public String getNamespace() {\r
137       return namespace;\r
138     }\r
139 \r
140     public XmlElement getOperationElement() {\r
141       return operationElement;\r
142     }\r
143 \r
144   }\r
145 \r
146   protected static XmlElement getRequestElementWithCheck(final Document message)\r
147       throws DocumentedException {\r
148     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),\r
149         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
150   }\r
151 \r
152   protected String getOperationNamespace() {\r
153     return "urn:o-ran:software-management:1.0";\r
154   }\r
155 \r
156   protected String getOperationName() {\r
157     return "software-download";\r
158   }\r
159 }\r