Modify config-api exceptions, bump config and netconf to 0.2.5-SNAPSHOT.
[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 org.opendaylight.controller.config.util.ConfigRegistryClient;
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
16 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 import java.util.HashMap;
25 import java.util.Map;
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             //FIXME where can IllegalStateException  be thrown?
57             logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
58             final Map<String, String> errorInfo = new HashMap<>();
59             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
60             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
61                     ErrorSeverity.error, errorInfo);
62         }
63
64         try {
65             this.transactionProvider.abortTransaction();
66         } catch (final IllegalStateException e) {
67             //FIXME where can IllegalStateException  be thrown?
68             logger.warn("Abort failed: ", e);
69             final Map<String, String> errorInfo = new HashMap<>();
70             errorInfo
71                     .put(ErrorTag.operation_failed.name(),
72                             "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation");
73             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
74                     ErrorSeverity.error, errorInfo);
75         }
76         logger.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
77
78         return document.createElement(XmlNetconfConstants.OK);
79     }
80 }