f5b1454b3ac7a5b833659eea3e3640eaa7674cf3
[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   private String swVersion;\r
50   private String hwVersion;\r
51 \r
52   public SoftwareDownloadOperation(String deviceID, String swVersion, String hwVersion) {\r
53     this.deviceID = deviceID;\r
54     this.swVersion = swVersion;\r
55     this.hwVersion = hwVersion;\r
56   }\r
57 \r
58   @Override\r
59   public HandlingPriority canHandle(final Document message) throws DocumentedException {\r
60     OperationNameAndNamespace operationNameAndNamespace = null;\r
61     operationNameAndNamespace = new OperationNameAndNamespace(message);\r
62     return canHandle(operationNameAndNamespace.getOperationName(),\r
63         operationNameAndNamespace.getNamespace());\r
64   }\r
65 \r
66   @Override\r
67   public Document handle(Document requestMessage,\r
68       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {\r
69 \r
70     logger.debug("soft-ware download rpc is received in netconfserver");\r
71 \r
72     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);\r
73     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);\r
74 \r
75     String requestXml = XmlUtility.convertDocumentToString(requestElement);\r
76     logger.debug("soft-ware download rpc requestXml=" + requestXml);\r
77 \r
78     NetConfServerProperties config =\r
79         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
80 \r
81     final String baseUrl = config.getMapperPath() + "/softwareDowload";\r
82     NetConfResponse restResponse =\r
83         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
84 \r
85     Document document = null;\r
86 \r
87     ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
88     if (errorCode != null && errorCode.getFaultCode() != null\r
89         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
90       logger.error("Error recevied : " + errorCode);\r
91       throw new DocumentedException(errorCode.getErrorMessage(),\r
92           ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
93           ErrorSeverity.from(errorCode.getErrorSeverity()));\r
94     } else if (restResponse != null && restResponse.getNetconfResponseXml() != null) {\r
95       logger.debug("soft-ware download rpc response received from mapper "\r
96           + restResponse.getNetconfResponseXml());\r
97       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r
98       DocumentBuilder builder;\r
99       try {\r
100         builder = factory.newDocumentBuilder();\r
101         document =\r
102             builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));\r
103         document.getDocumentElement().setAttribute("xmlns:ns1", getOperationNamespace());\r
104         document.getDocumentElement().setAttribute("xmlns",\r
105             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
106         document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);\r
107       } catch (Exception e) {\r
108         logger.error("while contruscting the response; ", e.toString());\r
109       }\r
110     }\r
111 \r
112     return document;\r
113   }\r
114 \r
115   protected HandlingPriority canHandle(final String operationName,\r
116       final String operationNamespace) {\r
117     return operationName.equals("software-download")\r
118         && operationNamespace.equals(SOFT_MGMT_NAMESPACE)\r
119             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)\r
120             : HandlingPriority.CANNOT_HANDLE;\r
121   }\r
122 \r
123   public static final class OperationNameAndNamespace {\r
124     private final String operationName;\r
125     private final String namespace;\r
126 \r
127     private final XmlElement operationElement;\r
128 \r
129     public OperationNameAndNamespace(final Document message) throws DocumentedException {\r
130       XmlElement requestElement = null;\r
131       requestElement = getRequestElementWithCheck(message);\r
132       operationElement = requestElement.getOnlyChildElement();\r
133       operationName = operationElement.getName();\r
134       namespace = operationElement.getNamespace();\r
135     }\r
136 \r
137     public String getOperationName() {\r
138       return operationName;\r
139     }\r
140 \r
141     public String getNamespace() {\r
142       return namespace;\r
143     }\r
144 \r
145     public XmlElement getOperationElement() {\r
146       return operationElement;\r
147     }\r
148 \r
149   }\r
150 \r
151   protected static XmlElement getRequestElementWithCheck(final Document message)\r
152       throws DocumentedException {\r
153     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),\r
154         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
155   }\r
156 \r
157   protected String getOperationNamespace() {\r
158     return "urn:o-ran:software-management:1.0";\r
159   }\r
160 \r
161   protected String getOperationName() {\r
162     return "software-download";\r
163   }\r
164 }\r