2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations;
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;
24 import java.util.HashMap;
27 public class DiscardChanges extends AbstractConfigNetconfOperation {
29 public static final String DISCARD = "discard-changes";
31 private static final Logger logger = LoggerFactory.getLogger(DiscardChanges.class);
33 private final TransactionProvider transactionProvider;
35 public DiscardChanges(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
36 String netconfSessionIdForReporting) {
37 super(configRegistryClient, netconfSessionIdForReporting);
38 this.transactionProvider = transactionProvider;
41 private static void fromXml(XmlElement xml) {
42 xml.checkName(DISCARD);
43 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
47 protected String getOperationName() {
52 protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
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<>();
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);
76 logger.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
78 return document.createElement(XmlNetconfConstants.OK);