d5e8066330a996749be8623031d0c48277650386
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / RebootOperation.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.w3c.dom.Element;
44 import org.w3c.dom.Node;
45 import org.xml.sax.InputSource;
46
47 public class RebootOperation implements NetconfOperation {
48   private static final Logger logger = LoggerFactory.getLogger(RebootOperation.class);
49   public static final String OP_NAMESPACE = "urn:tr069rpc:1.0";
50   public static final String OP_NAME = "reboot";
51
52   private String deviceID;
53   private String swVersion;
54   private String hwVersion;
55
56   public RebootOperation(String deviceID, String swVersion, String hwVersion) {
57     this.deviceID = deviceID;
58     this.swVersion = swVersion;
59     this.hwVersion = hwVersion;
60   }
61
62   @Override
63   public HandlingPriority canHandle(final Document message) throws DocumentedException {
64     OperationNameAndNamespace operationNameAndNamespace = null;
65     operationNameAndNamespace = new OperationNameAndNamespace(message);
66     return canHandle(operationNameAndNamespace.getOperationName(),
67         operationNameAndNamespace.getNamespace());
68   }
69
70   @Override
71   public Document handle(Document requestMessage,
72       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
73     logger.debug("Reboot rpc is received in netconfserver");
74
75     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
76
77     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
78     final Element element =
79         requestMessage.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
80             XmlNetconfConstants.RPC_REPLY_KEY);
81     element.setAttribute("xmlns:ns1", getOperationNamespace());
82     element.setAttribute("message-id", msgId);
83
84     String requestXml = XmlUtility.convertDocumentToString(requestElement);
85     logger.debug("reboot rpc requestXml= {}", requestXml);
86
87
88     NetConfServerProperties config =
89         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
90
91     final String baseUrl = config.getMapperPath() + "/reboot";
92     NetConfResponse restResponse =
93         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
94     Document respDoc = null;
95
96     ErrorCodeDetails errorCode = restResponse.getErrorCode();
97     if (errorCode != null && errorCode.getFaultCode() != null
98         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
99       logger.error("Error recevied : {}", errorCode);
100       throw new DocumentedException(errorCode.getErrorMessage(),
101           ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),
102           ErrorSeverity.from(errorCode.getErrorSeverity()));
103     } else {
104       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
105       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
106       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
107       DocumentBuilder builder;
108       try {
109         Node child = requestMessage.createElement(XmlNetconfConstants.OK);
110         element.appendChild(child);
111         String xmlStr = XmlUtility.convertDocumentToString(element);
112         try {
113           builder = factory.newDocumentBuilder();
114           respDoc = builder.parse(new InputSource(new StringReader(xmlStr)));
115         } catch (Exception e) {
116           logger.error("Error while converting String to element: {}", e.getMessage());
117           throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
118               ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
119         }
120       } catch (Exception e) {
121         logger.error("Error while contruscting the response: {}", e.getMessage());
122         throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
123             ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
124       }
125     }
126
127     return respDoc;
128   }
129
130   protected HandlingPriority canHandle(final String operationName,
131       final String operationNamespace) {
132     return operationName.equals(getOperationName())
133         && operationNamespace.equals(getOperationNamespace())
134             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)
135             : HandlingPriority.CANNOT_HANDLE;
136   }
137
138   public static final class OperationNameAndNamespace {
139     private final String operationName;
140     private final String namespace;
141
142     private final XmlElement operationElement;
143
144     public OperationNameAndNamespace(final Document message) throws DocumentedException {
145       XmlElement requestElement = null;
146       requestElement = getRequestElementWithCheck(message);
147       operationElement = requestElement.getOnlyChildElement();
148       operationName = operationElement.getName();
149       namespace = operationElement.getNamespace();
150     }
151
152     public String getOperationName() {
153       return operationName;
154     }
155
156     public String getNamespace() {
157       return namespace;
158     }
159
160     public XmlElement getOperationElement() {
161       return operationElement;
162     }
163
164   }
165
166   protected static XmlElement getRequestElementWithCheck(final Document message)
167       throws DocumentedException {
168     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
169         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
170   }
171
172   protected String getOperationNamespace() {
173     return OP_NAMESPACE;
174   }
175
176   protected String getOperationName() {
177     return OP_NAME;
178   }
179
180 }