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