Bug 714 - Fixed creating DOM Document's element with namespace
[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 java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
19 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
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 import com.google.common.base.Optional;
29
30 public class DiscardChanges extends AbstractConfigNetconfOperation {
31
32     public static final String DISCARD = "discard-changes";
33
34     private static final Logger logger = LoggerFactory.getLogger(DiscardChanges.class);
35
36     private final TransactionProvider transactionProvider;
37
38     public DiscardChanges(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
39             String netconfSessionIdForReporting) {
40         super(configRegistryClient, netconfSessionIdForReporting);
41         this.transactionProvider = transactionProvider;
42     }
43
44     private static void fromXml(XmlElement xml) {
45         xml.checkName(DISCARD);
46         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
47     }
48
49     @Override
50     protected String getOperationName() {
51         return DISCARD;
52     }
53
54     @Override
55     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
56         try {
57             fromXml(xml);
58         } catch (final IllegalArgumentException e) {
59             //FIXME where can IllegalStateException  be thrown?
60             logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
61             final Map<String, String> errorInfo = new HashMap<>();
62             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
63             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
64                     ErrorSeverity.error, errorInfo);
65         }
66
67         try {
68             this.transactionProvider.abortTransaction();
69         } catch (final IllegalStateException e) {
70             //FIXME where can IllegalStateException  be thrown?
71             logger.warn("Abort failed: ", e);
72             final Map<String, String> errorInfo = new HashMap<>();
73             errorInfo
74                     .put(ErrorTag.operation_failed.name(),
75                             "Operation failed. Use 'get-config' or 'edit-config' before triggering 'discard-changes' operation");
76             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
77                     ErrorSeverity.error, errorInfo);
78         }
79         logger.trace("Changes discarded successfully from datastore {}", Datastore.candidate);
80
81         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
82     }
83 }