Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / Validate.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.ValidationException;
13 import org.opendaylight.controller.config.util.ConfigRegistryClient;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
18 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 public class Validate extends AbstractConfigNetconfOperation {
31
32     public static final String VALIDATE = "validate";
33
34     private static final Logger logger = LoggerFactory.getLogger(Validate.class);
35
36     private final TransactionProvider transactionProvider;
37
38     public Validate(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
39             String netconfSessionIdForReporting) {
40         super(configRegistryClient, netconfSessionIdForReporting);
41         this.transactionProvider = transactionProvider;
42     }
43
44     private void checkXml(XmlElement xml) throws NetconfDocumentedException {
45         xml.checkName(VALIDATE);
46         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
47
48         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
49                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
50         XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
51
52         sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
53         String datastoreValue = sourceChildNode.getName();
54         Datastore sourceDatastore = Datastore.valueOf(datastoreValue);
55
56         if (sourceDatastore != Datastore.candidate){
57             throw new NetconfDocumentedException( "Only " + Datastore.candidate
58                     + " is supported as source for " + VALIDATE + " but was " + datastoreValue,ErrorType.application,ErrorTag.data_missing,ErrorSeverity.error);
59         }
60     }
61
62     @Override
63     protected String getOperationName() {
64         return VALIDATE;
65     }
66
67     @Override
68     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
69         checkXml(xml);
70         try {
71             transactionProvider.validateTransaction();
72         } catch (ValidationException e) {
73             logger.warn("Validation failed", e);
74             throw NetconfDocumentedException.wrap(e);
75         } catch (IllegalStateException e) {
76             logger.warn("Validation failed", e);
77             final Map<String, String> errorInfo = new HashMap<>();
78             errorInfo
79                     .put(ErrorTag.operation_failed.name(),
80                             "Datastore is not present. Use 'get-config' or 'edit-config' before triggering 'operations' operation");
81             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
82                     ErrorSeverity.error, errorInfo);
83
84         }
85
86         logger.trace("Datastore {} validated successfully", Datastore.candidate);
87
88         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
89     }
90 }