Merge "Removing { } from NormalizedNodeJsonBodyWriter"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / DiscardChanges.java
index 639d4dac726d4d156659739bb5addaf54578d0ce..1f70a1e52df63b62118e8c97af6b4107c9f068b8 100644 (file)
@@ -8,30 +8,29 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.operations;
 
+import com.google.common.base.Optional;
 import java.util.HashMap;
 import java.util.Map;
-
 import org.opendaylight.controller.config.util.ConfigRegistryClient;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
-import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-import com.google.common.base.Optional;
 
 public class DiscardChanges extends AbstractConfigNetconfOperation {
 
     public static final String DISCARD = "discard-changes";
 
-    private static final Logger logger = LoggerFactory.getLogger(DiscardChanges.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
 
     private final TransactionProvider transactionProvider;
 
@@ -41,7 +40,7 @@ public class DiscardChanges extends AbstractConfigNetconfOperation {
         this.transactionProvider = transactionProvider;
     }
 
-    private static void fromXml(XmlElement xml) {
+    private static void fromXml(XmlElement xml) throws NetconfDocumentedException {
         xml.checkName(DISCARD);
         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
     }
@@ -53,30 +52,22 @@ public class DiscardChanges extends AbstractConfigNetconfOperation {
 
     @Override
     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
+        fromXml(xml);
         try {
-            fromXml(xml);
-        } catch (final IllegalArgumentException e) {
-            //FIXME where can IllegalStateException  be thrown?
-            logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
-            final Map<String, String> errorInfo = new HashMap<>();
-            errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
-            throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
-                    ErrorSeverity.error, errorInfo);
-        }
-
-        try {
-            this.transactionProvider.abortTransaction();
-        } catch (final IllegalStateException e) {
-            //FIXME where can IllegalStateException  be thrown?
-            logger.warn("Abort failed: ", e);
+            if (transactionProvider.getTransaction().isPresent()) {
+                this.transactionProvider.abortTransaction();
+            }
+        } catch (final RuntimeException e) {
+            LOG.warn("Abort failed: ", e);
             final Map<String, String> errorInfo = new HashMap<>();
             errorInfo
                     .put(ErrorTag.operation_failed.name(),
-                            "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation");
+                            "Abort failed.");
             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
                     ErrorSeverity.error, errorInfo);
         }
-        logger.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
+        LOG.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
+
 
         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
     }