6135c14b3b3561e0cb36e183190fa3633113ecf1
[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 \r
49   public DeleteConfigOperation(final String netconfSessionIdForReporting, final DataList storage,\r
50       String deviceID) {\r
51     super(netconfSessionIdForReporting);\r
52     this.storage = storage;\r
53     this.deviceID = deviceID;\r
54   }\r
55 \r
56   @Override\r
57   protected Element handleWithNoSubsequentOperations(final Document document,\r
58       final XmlElement operationElement) throws DocumentedException {\r
59     final XmlElement configElementData =\r
60         operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);\r
61     containsDelete(configElementData);\r
62     if (containsDelete(configElementData)) {\r
63       storage.resetConfigList();\r
64     } else {\r
65       storage.setConfigList(configElementData.getChildElements());\r
66     }\r
67 \r
68     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
69     logger.debug("netconf request recevied : {}", requestXml);\r
70     NetConfServerProperties config =\r
71         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
72 \r
73     final String baseUrl = config.getMapperPath() + "/delConfig";\r
74     NetConfResponse restResponse = XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
75 \r
76     if (restResponse != null) {\r
77       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
78 \r
79       if (errorCode != null && errorCode.getFaultCode() != null\r
80           && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
81         throw new DocumentedException(errorCode.getErrorMessage(),\r
82             ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
83             ErrorSeverity.from(errorCode.getErrorSeverity()));\r
84       } else {\r
85         return document.createElement(XmlNetconfConstants.OK);\r
86       }\r
87     } else {\r
88       logger.error("received the null response from mapper ");\r
89       throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
90           ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
91     }\r
92 \r
93   }\r
94 \r
95   @Override\r
96   protected String getOperationName() {\r
97     return "delete-config";\r
98   }\r
99 \r
100   private boolean containsDelete(final XmlElement element) {\r
101     for (final Attr o : element.getAttributes().values()) {\r
102       if (o.getLocalName().equals(OPERATION)\r
103           && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue().equals(REMOVE_EDIT_CONFIG))) {\r
104         return true;\r
105       }\r
106     }\r
107     for (final XmlElement xmlElement : element.getChildElements()) {\r
108       if (containsDelete(xmlElement)) {\r
109         return true;\r
110       }\r
111     }\r
112     return false;\r
113   }\r
114 }\r