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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=======================================================================
19 package org.commscope.tr069adapter.netconf.rpc;
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;
39 public class DeleteConfigOperation extends AbstractLastNetconfOperation {
41 private static final Logger logger = LoggerFactory.getLogger(DeleteConfigOperation.class);
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;
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;
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();
69 storage.setConfigList(configElementData.getChildElements());
72 String requestXml = XmlUtility.convertDocumentToString(operationElement);
73 logger.debug("netconf request recevied : {}", requestXml);
74 NetConfServerProperties config =
75 NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
77 final String baseUrl = config.getMapperPath() + "/delConfig";
78 NetConfResponse restResponse =
79 XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
81 if (restResponse != null) {
82 ErrorCodeDetails errorCode = restResponse.getErrorCode();
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()));
90 return document.createElement(XmlNetconfConstants.OK);
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"));
101 protected String getOperationName() {
102 return "delete-config";
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))) {
112 for (final XmlElement xmlElement : element.getChildElements()) {
113 if (containsDelete(xmlElement)) {