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;
12 import java.util.HashMap;
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.api.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
29 public class DiscardChanges extends AbstractConfigNetconfOperation {
31 public static final String DISCARD = "discard-changes";
33 private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
35 private final TransactionProvider transactionProvider;
37 public DiscardChanges(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
38 String netconfSessionIdForReporting) {
39 super(configRegistryClient, netconfSessionIdForReporting);
40 this.transactionProvider = transactionProvider;
43 private static void fromXml(XmlElement xml) throws NetconfDocumentedException {
44 xml.checkName(DISCARD);
45 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
49 protected String getOperationName() {
54 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
57 if (transactionProvider.getTransaction().isPresent()) {
58 this.transactionProvider.abortTransaction();
60 } catch (final RuntimeException e) {
61 LOG.warn("Abort failed: ", e);
62 final Map<String, String> errorInfo = new HashMap<>();
64 .put(ErrorTag.operation_failed.name(),
66 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
67 ErrorSeverity.error, errorInfo);
69 LOG.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
72 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());