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 com.google.common.base.Optional;
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;
27 import java.util.HashMap;
31 public class DiscardChanges extends AbstractConfigNetconfOperation {
33 public static final String DISCARD = "discard-changes";
35 private static final Logger logger = LoggerFactory.getLogger(DiscardChanges.class);
37 private final TransactionProvider transactionProvider;
39 public DiscardChanges(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
40 String netconfSessionIdForReporting) {
41 super(configRegistryClient, netconfSessionIdForReporting);
42 this.transactionProvider = transactionProvider;
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);
51 protected String getOperationName() {
56 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
59 this.transactionProvider.abortTransaction();
60 } catch (final IllegalStateException e) {
61 logger.warn("Abort failed: ", e);
62 final Map<String, String> errorInfo = new HashMap<>();
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);
69 logger.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
72 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());