Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / SoftwareDownloadOperation.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 java.io.StringReader;
22
23 import javax.xml.XMLConstants;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26
27 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;
28 import org.commscope.tr069adapter.mapper.model.NetConfResponse;
29 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
30 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
31 import org.opendaylight.netconf.api.DocumentedException;
32 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
33 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
34 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
35 import org.opendaylight.netconf.api.xml.XmlElement;
36 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
37 import org.opendaylight.netconf.mapping.api.HandlingPriority;
38 import org.opendaylight.netconf.mapping.api.NetconfOperation;
39 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
44
45 public class SoftwareDownloadOperation implements NetconfOperation {
46   private static final Logger logger = LoggerFactory.getLogger(SoftwareDownloadOperation.class);
47   public static final String SOFT_MGMT_NAMESPACE = "urn:o-ran:software-management:1.0";
48   public static final String OP_NAME = "software-download";
49
50   private String deviceID;
51   private String swVersion;
52   private String hwVersion;
53
54   public SoftwareDownloadOperation(String deviceID, String swVersion, String hwVersion) {
55     this.deviceID = deviceID;
56     this.swVersion = swVersion;
57     this.hwVersion = hwVersion;
58   }
59
60   @Override
61   public HandlingPriority canHandle(final Document message) throws DocumentedException {
62     OperationNameAndNamespace operationNameAndNamespace = null;
63     operationNameAndNamespace = new OperationNameAndNamespace(message);
64     return canHandle(operationNameAndNamespace.getOperationName(),
65         operationNameAndNamespace.getNamespace());
66   }
67
68   @Override
69   public Document handle(Document requestMessage,
70       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
71
72     logger.debug("soft-ware download rpc is received in netconfserver");
73
74     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
75     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
76
77     String requestXml = XmlUtility.convertDocumentToString(requestElement);
78     logger.debug("soft-ware download rpc requestXml= {}", requestXml);
79
80     NetConfServerProperties config =
81         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
82
83     final String baseUrl = config.getMapperPath() + "/softwareDowload";
84     NetConfResponse restResponse =
85         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
86
87     Document document = null;
88
89     ErrorCodeDetails errorCode = restResponse.getErrorCode();
90     if (errorCode != null && errorCode.getFaultCode() != null
91         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
92       logger.error("Error recevied : {}", errorCode);
93       throw new DocumentedException(errorCode.getErrorMessage(),
94           ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),
95           ErrorSeverity.from(errorCode.getErrorSeverity()));
96     } else if (restResponse.getNetconfResponseXml() != null) {
97       logger.debug("soft-ware download rpc response received from mapper {}",
98           restResponse.getNetconfResponseXml());
99       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
100       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
101       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
102       DocumentBuilder builder;
103       try {
104         builder = factory.newDocumentBuilder();
105         document =
106             builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));
107         document.getDocumentElement().setAttribute("xmlns:ns1", getOperationNamespace());
108         document.getDocumentElement().setAttribute("xmlns",
109             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
110         document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
111       } catch (Exception e) {
112         logger.error("while contruscting the response; {} ", e.toString());
113       }
114     }
115
116     return document;
117   }
118
119   protected HandlingPriority canHandle(final String operationName,
120       final String operationNamespace) {
121     return operationName.equals(OP_NAME) && operationNamespace.equals(SOFT_MGMT_NAMESPACE)
122         ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)
123         : HandlingPriority.CANNOT_HANDLE;
124   }
125
126   public static final class OperationNameAndNamespace {
127     private final String operationName;
128     private final String namespace;
129
130     private final XmlElement operationElement;
131
132     public OperationNameAndNamespace(final Document message) throws DocumentedException {
133       XmlElement requestElement = null;
134       requestElement = getRequestElementWithCheck(message);
135       operationElement = requestElement.getOnlyChildElement();
136       operationName = operationElement.getName();
137       namespace = operationElement.getNamespace();
138     }
139
140     public String getOperationName() {
141       return operationName;
142     }
143
144     public String getNamespace() {
145       return namespace;
146     }
147
148     public XmlElement getOperationElement() {
149       return operationElement;
150     }
151
152   }
153
154   protected static XmlElement getRequestElementWithCheck(final Document message)
155       throws DocumentedException {
156     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
157         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
158   }
159
160   protected String getOperationNamespace() {
161     return SOFT_MGMT_NAMESPACE;
162   }
163
164   protected String getOperationName() {
165     return OP_NAME;
166   }
167 }