Migrate netconf-console to Karaf 4 way
[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.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;
25
26 public class DiscardChanges extends AbstractSingletonNetconfOperation {
27
28     private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
29
30     private static final String OPERATION_NAME = "discard-changes";
31
32     private final TransactionProvider transactionProvider;
33
34     public DiscardChanges(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
35         super(netconfSessionIdForReporting);
36         this.transactionProvider = transactionProvider;
37     }
38
39     @Override
40     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
41             throws DocumentedException {
42
43         try {
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);
53         }
54         return XmlUtil.createElement(document, XmlNetconfConstants.OK);
55     }
56
57     @Override
58     protected String getOperationName() {
59         return OPERATION_NAME;
60     }
61
62 }