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.ErrorSeverity;
14 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
15 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
16 import org.opendaylight.netconf.api.xml.XmlElement;
17 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
20 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
26 public class DiscardChanges extends AbstractSingletonNetconfOperation {
28 private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
30 private static final String OPERATION_NAME = "discard-changes";
32 private final TransactionProvider transactionProvider;
34 public DiscardChanges(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
35 super(netconfSessionIdForReporting);
36 this.transactionProvider = transactionProvider;
40 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
41 throws DocumentedException {
44 transactionProvider.abortTransaction();
45 } catch (final IllegalStateException e) {
46 LOG.warn("Abort failed ", e);
47 final Map<String, String> errorInfo = new HashMap<>();
48 errorInfo.put(ErrorTag.OPERATION_FAILED.name(),
49 "Operation failed. Use 'get-config' or 'edit-config' before triggering "
50 + OPERATION_NAME + " operation");
51 throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
52 ErrorSeverity.ERROR, errorInfo);
54 return XmlUtil.createElement(document, XmlNetconfConstants.OK);
58 protected String getOperationName() {
59 return OPERATION_NAME;