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