Merge "BUG-2243 Fixing invalid hello message handling"
[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
13 import org.opendaylight.controller.config.util.ConfigRegistryClient;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
18 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30
31 public class DiscardChanges extends AbstractConfigNetconfOperation {
32
33     public static final String DISCARD = "discard-changes";
34
35     private static final Logger logger = LoggerFactory.getLogger(DiscardChanges.class);
36
37     private final TransactionProvider transactionProvider;
38
39     public DiscardChanges(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
40             String netconfSessionIdForReporting) {
41         super(configRegistryClient, netconfSessionIdForReporting);
42         this.transactionProvider = transactionProvider;
43     }
44
45     private static void fromXml(XmlElement xml) throws NetconfDocumentedException {
46         xml.checkName(DISCARD);
47         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
48     }
49
50     @Override
51     protected String getOperationName() {
52         return DISCARD;
53     }
54
55     @Override
56     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
57         fromXml(xml);
58         try {
59             this.transactionProvider.abortTransaction();
60         } catch (final IllegalStateException e) {
61             logger.warn("Abort failed: ", e);
62             final Map<String, String> errorInfo = new HashMap<>();
63             errorInfo
64                     .put(ErrorTag.operation_failed.name(),
65                             "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation");
66             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
67                     ErrorSeverity.error, errorInfo);
68         }
69         logger.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
70
71
72         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
73     }
74 }