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