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.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;
31 import com.google.common.base.Optional;
33 public class Commit extends AbstractConfigNetconfOperation {
35 private static final Logger logger = LoggerFactory.getLogger(Commit.class);
37 private final TransactionProvider transactionProvider;
39 public Commit(TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
40 String netconfSessionIdForReporting) {
41 super(configRegistryClient, netconfSessionIdForReporting);
42 this.transactionProvider = transactionProvider;
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);
51 protected String getOperationName() {
52 return XmlNetconfConstants.COMMIT;
56 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
77 logger.trace("Datastore {} committed successfully: {}", Datastore.candidate, status);
79 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());