Merge "Add reverse() method in Edge and Path classes"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / Commit.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 org.opendaylight.controller.config.api.ConflictingVersionException;
12 import org.opendaylight.controller.config.api.ValidationException;
13 import org.opendaylight.controller.config.api.jmx.CommitStatus;
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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 public class Commit extends AbstractConfigNetconfOperation {
31
32     private static final Logger logger = LoggerFactory.getLogger(Commit.class);
33
34     private final TransactionProvider transactionProvider;
35
36     public Commit(TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
37             String netconfSessionIdForReporting) {
38         super(configRegistryClient, netconfSessionIdForReporting);
39         this.transactionProvider = transactionProvider;
40     }
41
42     private static void checkXml(XmlElement xml) {
43         xml.checkName(XmlNetconfConstants.COMMIT);
44         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
45     }
46
47     @Override
48     protected String getOperationName() {
49         return XmlNetconfConstants.COMMIT;
50     }
51
52     @Override
53     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
54         checkXml(xml);
55
56         CommitStatus status;
57         try {
58             status = this.transactionProvider.commitTransaction();
59         } catch (final IllegalStateException e) {
60             // TODO Illegal state thrown when no transaction yet for user
61             // Throw other exception type, or create transaction automatically
62             logger.warn("Commit failed: ", e);
63             final Map<String, String> errorInfo = new HashMap<>();
64             errorInfo.put(ErrorTag.operation_failed.name(),
65                     "Operation failed. Use 'get-config' or 'edit-config' before triggering 'commit' operation");
66             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
67                     ErrorSeverity.error, errorInfo);
68         } catch (ValidationException e) {
69             throw NetconfDocumentedException.wrap(e);
70         } catch (ConflictingVersionException e) {
71             throw NetconfDocumentedException.wrap(e);
72
73         }
74         logger.trace("Datastore {} committed successfully: {}", Datastore.candidate, status);
75
76         return document.createElement(XmlNetconfConstants.OK);
77     }
78
79 }