f3cc24494cbb98284cb06e9557ad5fb2fec6a838
[netconf.git] / netconf / config-netconf-connector / src / main / java / org / opendaylight / netconf / confignetconfconnector / operations / DiscardChanges.java
1 /*
2  * Copyright (c) 2013 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.netconf.confignetconfconnector.operations;
10
11 import com.google.common.base.Optional;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
15 import org.opendaylight.controller.config.facade.xml.Datastore;
16 import org.opendaylight.controller.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
19 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
20 import org.opendaylight.controller.config.util.xml.XmlElement;
21 import org.opendaylight.controller.config.util.xml.XmlUtil;
22 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28
29 public class DiscardChanges extends AbstractConfigNetconfOperation {
30
31     public static final String DISCARD = "discard-changes";
32
33     private static final Logger LOG = LoggerFactory.getLogger(DiscardChanges.class);
34
35     public DiscardChanges(final ConfigSubsystemFacade configSubsystemFacade,
36                           final String netconfSessionIdForReporting) {
37         super(configSubsystemFacade, netconfSessionIdForReporting);
38     }
39
40     private static void fromXml(final XmlElement xml) throws DocumentedException {
41         xml.checkName(DISCARD);
42         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
43     }
44
45     @Override
46     protected String getOperationName() {
47         return DISCARD;
48     }
49
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     @Override
52     protected Element handleWithNoSubsequentOperations(final Document document,
53                                                        final XmlElement xml) throws DocumentedException {
54         fromXml(xml);
55         try {
56             getConfigSubsystemFacade().abortConfiguration();
57         } catch (final RuntimeException e) {
58             LOG.warn("Abort failed: ", e);
59             final Map<String, String> errorInfo = new HashMap<>();
60             errorInfo
61                     .put(ErrorTag.OPERATION_FAILED.name(),
62                             "Abort failed.");
63             throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
64                     ErrorSeverity.ERROR, errorInfo);
65         }
66         LOG.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
67
68
69         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
70     }
71 }