Change target to ${project.build.target} in a bunch of pom file.
[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.jmx.CommitStatus;
15 import org.opendaylight.controller.config.util.ConfigRegistryClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
20 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 public class Commit extends AbstractConfigNetconfOperation {
29
30     private static final Logger logger = LoggerFactory.getLogger(Commit.class);
31
32     private final TransactionProvider transactionProvider;
33
34     public Commit(TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
35             String netconfSessionIdForReporting) {
36         super(configRegistryClient, netconfSessionIdForReporting);
37         this.transactionProvider = transactionProvider;
38     }
39
40     private static void checkXml(XmlElement xml) {
41         xml.checkName(XmlNetconfConstants.COMMIT);
42         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
43     }
44
45     @Override
46     protected String getOperationName() {
47         return XmlNetconfConstants.COMMIT;
48     }
49
50     @Override
51     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
52         checkXml(xml);
53
54         CommitStatus status;
55         try {
56             status = this.transactionProvider.commitTransaction();
57         } catch (final IllegalStateException e) {
58             logger.warn("Commit failed: ", e);
59             final Map<String, String> errorInfo = new HashMap<>();
60             errorInfo.put(ErrorTag.operation_failed.name(),
61                     "Operation failed. Use 'get-config' or 'edit-config' before triggering 'commit' operation");
62             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
63                     ErrorSeverity.error, errorInfo);
64         } catch (final NetconfDocumentedException e) {
65             throw new NetconfDocumentedException(
66                     "Unable to retrieve config snapshot after commit for persister, details: " + e.getMessage(),
67                     ErrorType.application, ErrorTag.operation_failed, ErrorSeverity.error, e.getErrorInfo());
68         }
69         logger.info("Datastore {} committed successfully: {}", Datastore.candidate, status);
70
71         return document.createElement(XmlNetconfConstants.OK);
72     }
73
74 }