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