BUG 2743 - Added support for runtime RPC's to netconf mdsal northbound.
[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.AbstractSingletonNetconfOperation;
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 AbstractSingletonNetconfOperation {
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
44         try {
45             transactionProvider.abortTransaction();
46         } catch (IllegalStateException e) {
47             LOG.warn("Abort failed ", e);
48             final Map<String, String> errorInfo = new HashMap<>();
49             errorInfo
50                     .put(ErrorTag.operation_failed.name(),
51                             "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation");
52             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
53                     ErrorSeverity.error, errorInfo);
54         }
55         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
56     }
57
58     @Override
59     protected String getOperationName() {
60         return OPERATION_NAME;
61     }
62
63 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.