2 * Copyright (c) 2015 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
8 package org.opendaylight.netconf.mdsal.connector.ops;
10 import java.util.HashMap;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
14 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
15 import org.opendaylight.netconf.api.xml.XmlElement;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
18 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
19 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
25 public class DiscardChanges extends AbstractSingletonNetconfOperation {
27 private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
29 private static final String OPERATION_NAME = "discard-changes";
31 private final TransactionProvider transactionProvider;
33 public DiscardChanges(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
34 super(netconfSessionIdForReporting);
35 this.transactionProvider = transactionProvider;
39 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
40 throws DocumentedException {
43 transactionProvider.abortTransaction();
44 } catch (final IllegalStateException e) {
45 LOG.warn("Abort failed ", e);
46 final Map<String, String> errorInfo = new HashMap<>();
47 errorInfo.put(ErrorTag.OPERATION_FAILED.name(),
48 "Operation failed. Use 'get-config' or 'edit-config' before triggering "
49 + OPERATION_NAME + " operation");
50 throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
51 ErrorSeverity.ERROR, errorInfo);
53 return document.createElement(XmlNetconfConstants.OK);
57 protected String getOperationName() {
58 return OPERATION_NAME;