a78e5faa4977f6637e41748d9d772b618ab6f1f2
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / SoftwareActivateOperation.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.netconf.rpc;
20
21 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
22 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
23 import org.opendaylight.netconf.api.DocumentedException;
24 import org.opendaylight.netconf.api.xml.XmlElement;
25 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
26 import org.opendaylight.netconf.mapping.api.HandlingPriority;
27 import org.opendaylight.netconf.mapping.api.NetconfOperation;
28 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32
33 public class SoftwareActivateOperation implements NetconfOperation {
34   private static final Logger logger = LoggerFactory.getLogger(SoftwareActivateOperation.class);
35   public static final String SOFT_MGMT_NAMESPACE = "urn:o-ran:software-management:1.0";
36
37   private String deviceID;
38   private String swVersion;
39   private String hwVersion;
40
41   public SoftwareActivateOperation(String deviceID, String swVersion, String hwVersion) {
42     this.deviceID = deviceID;
43     this.swVersion = swVersion;
44     this.hwVersion = hwVersion;
45   }
46
47   @Override
48   public HandlingPriority canHandle(final Document message) throws DocumentedException {
49     OperationNameAndNamespace operationNameAndNamespace = null;
50     operationNameAndNamespace = new OperationNameAndNamespace(message);
51     return canHandle(operationNameAndNamespace.getOperationName(),
52         operationNameAndNamespace.getNamespace());
53   }
54
55   @Override
56   public Document handle(Document requestMessage,
57       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
58
59     logger.debug("sw-activate rpc recevied in netconf server");
60     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
61
62     String requestXml = XmlUtility.convertDocumentToString(requestElement);
63     logger.debug("sw-activate rpc recevied requestXml = {}", requestXml);
64     NetConfServerProperties config =
65         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
66
67     final String baseUrl = config.getMapperPath() + "/softwareActivate";
68     XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
69     return null;
70   }
71
72   protected HandlingPriority canHandle(final String operationName,
73       final String operationNamespace) {
74     return operationName.equals("software-activate")
75         && operationNamespace.equals(SOFT_MGMT_NAMESPACE)
76             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)
77             : HandlingPriority.CANNOT_HANDLE;
78   }
79
80   public static final class OperationNameAndNamespace {
81     private final String operationName;
82     private final String namespace;
83
84     private final XmlElement operationElement;
85
86     public OperationNameAndNamespace(final Document message) throws DocumentedException {
87       XmlElement requestElement = null;
88       requestElement = getRequestElementWithCheck(message);
89       operationElement = requestElement.getOnlyChildElement();
90       operationName = operationElement.getName();
91       namespace = operationElement.getNamespace();
92     }
93
94     public String getOperationName() {
95       return operationName;
96     }
97
98     public String getNamespace() {
99       return namespace;
100     }
101
102     public XmlElement getOperationElement() {
103       return operationElement;
104     }
105   }
106
107   protected static XmlElement getRequestElementWithCheck(final Document message)
108       throws DocumentedException {
109     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
110         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
111   }
112 }