6439498a5ff9f1b904f622c07b4dd849fc2efd51
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / SoftwareActivateOperation.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 SoftwareActivateOperation implements NetconfOperation {\r
34   private static final Logger logger = LoggerFactory.getLogger(SoftwareActivateOperation.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   private String swVersion;\r
39   private String hwVersion;\r
40 \r
41   public SoftwareActivateOperation(String deviceID, String swVersion, String hwVersion) {\r
42     this.deviceID = deviceID;\r
43     this.swVersion = swVersion;\r
44     this.hwVersion = hwVersion;\r
45   }\r
46 \r
47   @Override\r
48   public HandlingPriority canHandle(final Document message) throws DocumentedException {\r
49     OperationNameAndNamespace operationNameAndNamespace = null;\r
50     operationNameAndNamespace = new OperationNameAndNamespace(message);\r
51     return canHandle(operationNameAndNamespace.getOperationName(),\r
52         operationNameAndNamespace.getNamespace());\r
53   }\r
54 \r
55   @Override\r
56   public Document handle(Document requestMessage,\r
57       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {\r
58 \r
59     logger.debug("sw-activate rpc recevied in netconf server");\r
60     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);\r
61 \r
62     String requestXml = XmlUtility.convertDocumentToString(requestElement);\r
63     logger.debug("sw-activate rpc recevied requestXml = {}", requestXml);\r
64     NetConfServerProperties config =\r
65         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
66 \r
67     final String baseUrl = config.getMapperPath() + "/softwareActivate";\r
68     XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
69     return null;\r
70   }\r
71 \r
72   protected HandlingPriority canHandle(final String operationName,\r
73       final String operationNamespace) {\r
74     return operationName.equals("software-activate")\r
75         && operationNamespace.equals(SOFT_MGMT_NAMESPACE)\r
76             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)\r
77             : HandlingPriority.CANNOT_HANDLE;\r
78   }\r
79 \r
80   public static final class OperationNameAndNamespace {\r
81     private final String operationName;\r
82     private final String namespace;\r
83 \r
84     private final XmlElement operationElement;\r
85 \r
86     public OperationNameAndNamespace(final Document message) throws DocumentedException {\r
87       XmlElement requestElement = null;\r
88       requestElement = getRequestElementWithCheck(message);\r
89       operationElement = requestElement.getOnlyChildElement();\r
90       operationName = operationElement.getName();\r
91       namespace = operationElement.getNamespace();\r
92     }\r
93 \r
94     public String getOperationName() {\r
95       return operationName;\r
96     }\r
97 \r
98     public String getNamespace() {\r
99       return namespace;\r
100     }\r
101 \r
102     public XmlElement getOperationElement() {\r
103       return operationElement;\r
104     }\r
105   }\r
106 \r
107   protected static XmlElement getRequestElementWithCheck(final Document message)\r
108       throws DocumentedException {\r
109     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),\r
110         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
111   }\r
112 }\r