1230144a037db3be5bdae3a306ccab0c4416d3ef
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / DeleteConfigOperation.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.mapper.model.ErrorCodeDetails;\r
22 import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
23 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
24 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
25 import org.opendaylight.netconf.api.DocumentedException;\r
26 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
27 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
28 import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
29 import org.opendaylight.netconf.api.xml.XmlElement;\r
30 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
31 import org.opendaylight.netconf.test.tool.rpc.DataList;\r
32 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;\r
33 import org.slf4j.Logger;\r
34 import org.slf4j.LoggerFactory;\r
35 import org.w3c.dom.Attr;\r
36 import org.w3c.dom.Document;\r
37 import org.w3c.dom.Element;\r
38 \r
39 public class DeleteConfigOperation extends AbstractLastNetconfOperation {\r
40 \r
41   private static final Logger logger = LoggerFactory.getLogger(DeleteConfigOperation.class);\r
42 \r
43   private static final String DELETE_EDIT_CONFIG = "delete";\r
44   private static final String OPERATION = "operation";\r
45   private static final String REMOVE_EDIT_CONFIG = "remove";\r
46   private final DataList storage;\r
47   private String deviceID;\r
48   private String swVersion;\r
49   private String hwVersion;\r
50 \r
51   public DeleteConfigOperation(final String netconfSessionIdForReporting, final DataList storage,\r
52       String deviceID, String swVersion, String hwVersion) {\r
53     super(netconfSessionIdForReporting);\r
54     this.storage = storage;\r
55     this.deviceID = deviceID;\r
56     this.swVersion = swVersion;\r
57     this.hwVersion = hwVersion;\r
58   }\r
59 \r
60   @Override\r
61   protected Element handleWithNoSubsequentOperations(final Document document,\r
62       final XmlElement operationElement) throws DocumentedException {\r
63     final XmlElement configElementData =\r
64         operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);\r
65     containsDelete(configElementData);\r
66     if (containsDelete(configElementData)) {\r
67       storage.resetConfigList();\r
68     } else {\r
69       storage.setConfigList(configElementData.getChildElements());\r
70     }\r
71 \r
72     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
73     logger.debug("netconf request recevied : {}", requestXml);\r
74     NetConfServerProperties config =\r
75         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
76 \r
77     final String baseUrl = config.getMapperPath() + "/delConfig";\r
78     NetConfResponse restResponse =\r
79         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
80 \r
81     if (restResponse != null) {\r
82       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
83 \r
84       if (errorCode != null && errorCode.getFaultCode() != null\r
85           && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
86         throw new DocumentedException(errorCode.getErrorMessage(),\r
87             ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
88             ErrorSeverity.from(errorCode.getErrorSeverity()));\r
89       } else {\r
90         return document.createElement(XmlNetconfConstants.OK);\r
91       }\r
92     } else {\r
93       logger.error("received the null response from mapper ");\r
94       throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
95           ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
96     }\r
97 \r
98   }\r
99 \r
100   @Override\r
101   protected String getOperationName() {\r
102     return "delete-config";\r
103   }\r
104 \r
105   private boolean containsDelete(final XmlElement element) {\r
106     for (final Attr o : element.getAttributes().values()) {\r
107       if (o.getLocalName().equals(OPERATION)\r
108           && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue().equals(REMOVE_EDIT_CONFIG))) {\r
109         return true;\r
110       }\r
111     }\r
112     for (final XmlElement xmlElement : element.getChildElements()) {\r
113       if (containsDelete(xmlElement)) {\r
114         return true;\r
115       }\r
116     }\r
117     return false;\r
118   }\r
119 }\r