Remove DocumentedException.ErrorType
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / DiscardChanges.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.mdsal.connector.ops;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
14 import org.opendaylight.netconf.api.xml.XmlElement;
15 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
17 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
18 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.ErrorType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 public class DiscardChanges extends AbstractSingletonNetconfOperation {
26     private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
27     private static final String OPERATION_NAME = "discard-changes";
28
29     private final TransactionProvider transactionProvider;
30
31     public DiscardChanges(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
32         super(netconfSessionIdForReporting);
33         this.transactionProvider = transactionProvider;
34     }
35
36     @Override
37     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
38             throws DocumentedException {
39
40         try {
41             transactionProvider.abortTransaction();
42         } catch (final IllegalStateException e) {
43             LOG.warn("Abort failed ", e);
44             final Map<String, String> errorInfo = new HashMap<>();
45             errorInfo.put(ErrorTag.OPERATION_FAILED.name(),
46                     "Operation failed. Use 'get-config' or 'edit-config' before triggering "
47                             + OPERATION_NAME + " operation");
48             throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
49                     ErrorSeverity.ERROR, errorInfo);
50         }
51         return document.createElement(XmlNetconfConstants.OK);
52     }
53
54     @Override
55     protected String getOperationName() {
56         return OPERATION_NAME;
57     }
58 }