Initial source code
[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 org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
22 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
23 import org.opendaylight.netconf.api.DocumentedException;\r
24 import org.opendaylight.netconf.api.xml.XmlElement;\r
25 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
26 import org.opendaylight.netconf.mapping.api.HandlingPriority;\r
27 import org.opendaylight.netconf.mapping.api.NetconfOperation;\r
28 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 import org.w3c.dom.Document;\r
32 \r
33 public class SoftwareDownloadOperation implements NetconfOperation {\r
34   private static final Logger logger = LoggerFactory.getLogger(SoftwareDownloadOperation.class);\r
35   public static final String SOFT_MGMT_NAMESPACE = "urn:o-ran:software-management:1.0";\r
36 \r
37   private String deviceID;\r
38 \r
39   public SoftwareDownloadOperation(String deviceID) {\r
40     this.deviceID = deviceID;\r
41   }\r
42 \r
43   @Override\r
44   public HandlingPriority canHandle(final Document message) throws DocumentedException {\r
45     OperationNameAndNamespace operationNameAndNamespace = null;\r
46     operationNameAndNamespace = new OperationNameAndNamespace(message);\r
47     return canHandle(operationNameAndNamespace.getOperationName(),\r
48         operationNameAndNamespace.getNamespace());\r
49   }\r
50 \r
51   @Override\r
52   public Document handle(Document requestMessage,\r
53       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {\r
54 \r
55     logger.debug("sw-download rpc recevied in netconf server");\r
56     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);\r
57 \r
58     String requestXml = XmlUtility.convertDocumentToString(requestElement);\r
59     logger.debug("sw-download rpc recevied requestXml = {}", requestXml);\r
60     NetConfServerProperties config =\r
61         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
62 \r
63     final String baseUrl = config.getMapperPath() + "/softwareDowload";\r
64     XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
65 \r
66     return null;\r
67   }\r
68 \r
69   protected HandlingPriority canHandle(final String operationName,\r
70       final String operationNamespace) {\r
71     return operationName.equals("software-download")\r
72         && operationNamespace.equals(SOFT_MGMT_NAMESPACE)\r
73             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)\r
74             : HandlingPriority.CANNOT_HANDLE;\r
75   }\r
76 \r
77   public static final class OperationNameAndNamespace {\r
78     private final String operationName;\r
79     private final String namespace;\r
80 \r
81     private final XmlElement operationElement;\r
82 \r
83     public OperationNameAndNamespace(final Document message) throws DocumentedException {\r
84       XmlElement requestElement = null;\r
85       requestElement = getRequestElementWithCheck(message);\r
86       operationElement = requestElement.getOnlyChildElement();\r
87       operationName = operationElement.getName();\r
88       namespace = operationElement.getNamespace();\r
89     }\r
90 \r
91     public String getOperationName() {\r
92       return operationName;\r
93     }\r
94 \r
95     public String getNamespace() {\r
96       return namespace;\r
97     }\r
98 \r
99     public XmlElement getOperationElement() {\r
100       return operationElement;\r
101     }\r
102 \r
103   }\r
104 \r
105   protected static XmlElement getRequestElementWithCheck(final Document message)\r
106       throws DocumentedException {\r
107     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),\r
108         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
109   }\r
110 }\r