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
index 1230144..ff64e96 100644 (file)
-/*\r
- * ============LICENSE_START========================================================================\r
- * ONAP : tr-069-adapter\r
- * =================================================================================================\r
- * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
- * =================================================================================================\r
- * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
- * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
- * may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
- * either express or implied. See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ===============LICENSE_END=======================================================================\r
- */\r
-\r
-package org.commscope.tr069adapter.netconf.rpc;\r
-\r
-import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;\r
-import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
-import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
-import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
-import org.opendaylight.netconf.api.DocumentedException;\r
-import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
-import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
-import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
-import org.opendaylight.netconf.api.xml.XmlElement;\r
-import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
-import org.opendaylight.netconf.test.tool.rpc.DataList;\r
-import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-import org.w3c.dom.Attr;\r
-import org.w3c.dom.Document;\r
-import org.w3c.dom.Element;\r
-\r
-public class DeleteConfigOperation extends AbstractLastNetconfOperation {\r
-\r
-  private static final Logger logger = LoggerFactory.getLogger(DeleteConfigOperation.class);\r
-\r
-  private static final String DELETE_EDIT_CONFIG = "delete";\r
-  private static final String OPERATION = "operation";\r
-  private static final String REMOVE_EDIT_CONFIG = "remove";\r
-  private final DataList storage;\r
-  private String deviceID;\r
-  private String swVersion;\r
-  private String hwVersion;\r
-\r
-  public DeleteConfigOperation(final String netconfSessionIdForReporting, final DataList storage,\r
-      String deviceID, String swVersion, String hwVersion) {\r
-    super(netconfSessionIdForReporting);\r
-    this.storage = storage;\r
-    this.deviceID = deviceID;\r
-    this.swVersion = swVersion;\r
-    this.hwVersion = hwVersion;\r
-  }\r
-\r
-  @Override\r
-  protected Element handleWithNoSubsequentOperations(final Document document,\r
-      final XmlElement operationElement) throws DocumentedException {\r
-    final XmlElement configElementData =\r
-        operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);\r
-    containsDelete(configElementData);\r
-    if (containsDelete(configElementData)) {\r
-      storage.resetConfigList();\r
-    } else {\r
-      storage.setConfigList(configElementData.getChildElements());\r
-    }\r
-\r
-    String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
-    logger.debug("netconf request recevied : {}", requestXml);\r
-    NetConfServerProperties config =\r
-        NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
-\r
-    final String baseUrl = config.getMapperPath() + "/delConfig";\r
-    NetConfResponse restResponse =\r
-        XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
-\r
-    if (restResponse != null) {\r
-      ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
-\r
-      if (errorCode != null && errorCode.getFaultCode() != null\r
-          && !errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
-        throw new DocumentedException(errorCode.getErrorMessage(),\r
-            ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
-            ErrorSeverity.from(errorCode.getErrorSeverity()));\r
-      } else {\r
-        return document.createElement(XmlNetconfConstants.OK);\r
-      }\r
-    } else {\r
-      logger.error("received the null response from mapper ");\r
-      throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
-          ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
-    }\r
-\r
-  }\r
-\r
-  @Override\r
-  protected String getOperationName() {\r
-    return "delete-config";\r
-  }\r
-\r
-  private boolean containsDelete(final XmlElement element) {\r
-    for (final Attr o : element.getAttributes().values()) {\r
-      if (o.getLocalName().equals(OPERATION)\r
-          && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue().equals(REMOVE_EDIT_CONFIG))) {\r
-        return true;\r
-      }\r
-    }\r
-    for (final XmlElement xmlElement : element.getChildElements()) {\r
-      if (containsDelete(xmlElement)) {\r
-        return true;\r
-      }\r
-    }\r
-    return false;\r
-  }\r
-}\r
+/*
+ * ============LICENSE_START========================================================================
+ * ONAP : tr-069-adapter
+ * =================================================================================================
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.
+ * =================================================================================================
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
+ * may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ===============LICENSE_END=======================================================================
+ */
+
+package org.commscope.tr069adapter.netconf.rpc;
+
+import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;
+import org.commscope.tr069adapter.mapper.model.NetConfResponse;
+import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
+import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
+import org.opendaylight.netconf.api.DocumentedException;
+import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
+import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
+import org.opendaylight.netconf.api.DocumentedException.ErrorType;
+import org.opendaylight.netconf.api.xml.XmlElement;
+import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.netconf.test.tool.rpc.DataList;
+import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class DeleteConfigOperation extends AbstractLastNetconfOperation {
+
+  private static final Logger logger = LoggerFactory.getLogger(DeleteConfigOperation.class);
+
+  private static final String DELETE_EDIT_CONFIG = "delete";
+  private static final String OPERATION = "operation";
+  private static final String REMOVE_EDIT_CONFIG = "remove";
+  private final DataList storage;
+  private String deviceID;
+  private String swVersion;
+  private String hwVersion;
+
+  public DeleteConfigOperation(final String netconfSessionIdForReporting, final DataList storage,
+      String deviceID, String swVersion, String hwVersion) {
+    super(netconfSessionIdForReporting);
+    this.storage = storage;
+    this.deviceID = deviceID;
+    this.swVersion = swVersion;
+    this.hwVersion = hwVersion;
+  }
+
+  @Override
+  protected Element handleWithNoSubsequentOperations(final Document document,
+      final XmlElement operationElement) throws DocumentedException {
+    final XmlElement configElementData =
+        operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);
+    containsDelete(configElementData);
+    if (containsDelete(configElementData)) {
+      storage.resetConfigList();
+    } else {
+      storage.setConfigList(configElementData.getChildElements());
+    }
+
+    String requestXml = XmlUtility.convertDocumentToString(operationElement);
+    logger.debug("netconf request recevied : {}", requestXml);
+    NetConfServerProperties config =
+        NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
+
+    final String baseUrl = config.getMapperPath() + "/delConfig";
+    NetConfResponse restResponse =
+        XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
+
+    if (restResponse != null) {
+      ErrorCodeDetails errorCode = restResponse.getErrorCode();
+
+      if (errorCode != null && errorCode.getFaultCode() != null
+          && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
+        throw new DocumentedException(errorCode.getErrorMessage(),
+            ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),
+            ErrorSeverity.from(errorCode.getErrorSeverity()));
+      } else {
+        return document.createElement(XmlNetconfConstants.OK);
+      }
+    } else {
+      logger.error("received the null response from mapper ");
+      throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),
+          ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
+    }
+
+  }
+
+  @Override
+  protected String getOperationName() {
+    return "delete-config";
+  }
+
+  private boolean containsDelete(final XmlElement element) {
+    for (final Attr o : element.getAttributes().values()) {
+      if (o.getLocalName().equals(OPERATION)
+          && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue().equals(REMOVE_EDIT_CONFIG))) {
+        return true;
+      }
+    }
+    for (final XmlElement xmlElement : element.getChildElements()) {
+      if (containsDelete(xmlElement)) {
+        return true;
+      }
+    }
+    return false;
+  }
+}