Initial source code
[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 \r
39   public SoftwareActivateOperation(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-activate 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-activate rpc recevied requestXml = {}", requestXml);\r
60     NetConfServerProperties config =\r
61         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
62 \r
63     final String baseUrl = config.getMapperPath() + "/softwareActivate";\r
64     XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
65     return null;\r
66   }\r
67 \r
68   protected HandlingPriority canHandle(final String operationName,\r
69       final String operationNamespace) {\r
70     return operationName.equals("software-activate")\r
71         && operationNamespace.equals(SOFT_MGMT_NAMESPACE)\r
72             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)\r
73             : HandlingPriority.CANNOT_HANDLE;\r
74   }\r
75 \r
76   public static final class OperationNameAndNamespace {\r
77     private final String operationName;\r
78     private final String namespace;\r
79 \r
80     private final XmlElement operationElement;\r
81 \r
82     public OperationNameAndNamespace(final Document message) throws DocumentedException {\r
83       XmlElement requestElement = null;\r
84       requestElement = getRequestElementWithCheck(message);\r
85       operationElement = requestElement.getOnlyChildElement();\r
86       operationName = operationElement.getName();\r
87       namespace = operationElement.getNamespace();\r
88     }\r
89 \r
90     public String getOperationName() {\r
91       return operationName;\r
92     }\r
93 \r
94     public String getNamespace() {\r
95       return namespace;\r
96     }\r
97 \r
98     public XmlElement getOperationElement() {\r
99       return operationElement;\r
100     }\r
101   }\r
102 \r
103   protected static XmlElement getRequestElementWithCheck(final Document message)\r
104       throws DocumentedException {\r
105     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),\r
106         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);\r
107   }\r
108 }\r