2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations;
11 import java.util.HashMap;
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.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
30 public class Commit extends AbstractConfigNetconfOperation {
32 private static final Logger logger = LoggerFactory.getLogger(Commit.class);
34 private final TransactionProvider transactionProvider;
36 public Commit(TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
37 String netconfSessionIdForReporting) {
38 super(configRegistryClient, netconfSessionIdForReporting);
39 this.transactionProvider = transactionProvider;
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);
48 protected String getOperationName() {
49 return XmlNetconfConstants.COMMIT;
53 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
74 logger.trace("Datastore {} committed successfully: {}", Datastore.candidate, status);
76 return document.createElement(XmlNetconfConstants.OK);