Bug 2731: Discard changes only when transaction exist.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / DiscardChanges.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations;
10
11 import com.google.common.base.Optional;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28
29 public class DiscardChanges extends AbstractConfigNetconfOperation {
30
31     public static final String DISCARD = "discard-changes";
32
33     private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
34
35     private final TransactionProvider transactionProvider;
36
37     public DiscardChanges(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
38             String netconfSessionIdForReporting) {
39         super(configRegistryClient, netconfSessionIdForReporting);
40         this.transactionProvider = transactionProvider;
41     }
42
43     private static void fromXml(XmlElement xml) throws NetconfDocumentedException {
44         xml.checkName(DISCARD);
45         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
46     }
47
48     @Override
49     protected String getOperationName() {
50         return DISCARD;
51     }
52
53     @Override
54     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
55         fromXml(xml);
56         try {
57             if (transactionProvider.getTransaction().isPresent()) {
58                 this.transactionProvider.abortTransaction();
59             }
60         } catch (final RuntimeException e) {
61             LOG.warn("Abort failed: ", e);
62             final Map<String, String> errorInfo = new HashMap<>();
63             errorInfo
64                     .put(ErrorTag.operation_failed.name(),
65                             "Abort failed.");
66             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
67                     ErrorSeverity.error, errorInfo);
68         }
69         LOG.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
70
71
72         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
73     }
74 }