Merge "Startup arch - remove artifactId prefix from dir names."
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops;
10
11 import com.google.common.base.Optional;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
18 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 public class DiscardChanges extends AbstractLastNetconfOperation{
29
30     private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
31
32     private static final String OPERATION_NAME = "discard-changes";
33
34     private final TransactionProvider transactionProvider;
35
36     public DiscardChanges(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
37         super(netconfSessionIdForReporting);
38         this.transactionProvider = transactionProvider;
39     }
40
41     @Override
42     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
43         operationElement.getOnlyChildElement(OPERATION_NAME);
44
45         try {
46             transactionProvider.abortTransaction();
47         } catch (IllegalStateException e) {
48             LOG.warn("Abort failed ", e);
49             final Map<String, String> errorInfo = new HashMap<>();
50             errorInfo
51                     .put(ErrorTag.operation_failed.name(),
52                             "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation");
53             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
54                     ErrorSeverity.error, errorInfo);
55         }
56         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
57     }
58
59     @Override
60     protected String getOperationName() {
61         return OPERATION_NAME;
62     }
63 }