Decouple config and netconf subsystems.
[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 com.google.common.base.Optional;
12 import org.opendaylight.controller.config.api.ConflictingVersionException;
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.api.jmx.CommitStatus;
15 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
16 import org.opendaylight.controller.config.facade.xml.Datastore;
17 import org.opendaylight.controller.config.util.xml.DocumentedException;
18 import org.opendaylight.controller.config.util.xml.XmlElement;
19 import org.opendaylight.controller.config.util.xml.XmlUtil;
20 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25
26 public class Commit extends AbstractConfigNetconfOperation {
27
28     private static final Logger LOG = LoggerFactory.getLogger(Commit.class);
29
30     public  Commit(final ConfigSubsystemFacade configSubsystemFacade, final String netconfSessionIdForReporting) {
31         super(configSubsystemFacade, netconfSessionIdForReporting);
32     }
33
34     private static void checkXml(XmlElement xml) throws DocumentedException {
35         xml.checkName(XmlNetconfConstants.COMMIT);
36         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
37     }
38
39     @Override
40     protected String getOperationName() {
41         return XmlNetconfConstants.COMMIT;
42     }
43
44     @Override
45     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws DocumentedException {
46
47         checkXml(xml);
48         CommitStatus status;
49         try {
50             status = getConfigSubsystemFacade().commitTransaction();
51             LOG.trace("Datastore {} committed successfully: {}", Datastore.candidate, status);
52         } catch (ConflictingVersionException | ValidationException e) {
53             throw DocumentedException.wrap(e);
54         }
55         LOG.trace("Datastore {} committed successfully: {}", Datastore.candidate, status);
56
57         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
58     }
59
60 }