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