Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.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.controller.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, final String netconfSessionIdForReporting) {
36         super(configSubsystemFacade, netconfSessionIdForReporting);
37     }
38
39     private static void fromXml(XmlElement xml) throws DocumentedException {
40         xml.checkName(DISCARD);
41         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
42     }
43
44     @Override
45     protected String getOperationName() {
46         return DISCARD;
47     }
48
49     @Override
50     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws DocumentedException {
51         fromXml(xml);
52         try {
53             getConfigSubsystemFacade().abortConfiguration();
54         } catch (final RuntimeException e) {
55             LOG.warn("Abort failed: ", e);
56             final Map<String, String> errorInfo = new HashMap<>();
57             errorInfo
58                     .put(ErrorTag.operation_failed.name(),
59                             "Abort failed.");
60             throw new DocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
61                     ErrorSeverity.error, errorInfo);
62         }
63         LOG.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
64
65
66         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
67     }
68 }