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 com.google.common.base.Optional;
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
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.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
28 import java.util.HashMap;
31 public class Validate extends AbstractConfigNetconfOperation {
33 public static final String VALIDATE = "validate";
35 private static final Logger logger = LoggerFactory.getLogger(Validate.class);
37 private final TransactionProvider transactionProvider;
39 public Validate(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
40 String netconfSessionIdForReporting) {
41 super(configRegistryClient, netconfSessionIdForReporting);
42 this.transactionProvider = transactionProvider;
45 private void checkXml(XmlElement xml) throws NetconfDocumentedException {
46 xml.checkName(VALIDATE);
47 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
49 XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
50 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
51 XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
53 sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
54 String datastoreValue = sourceChildNode.getName();
55 Datastore sourceDatastore = Datastore.valueOf(datastoreValue);
57 if (sourceDatastore != Datastore.candidate){
58 throw new NetconfDocumentedException( "Only " + Datastore.candidate
59 + " is supported as source for " + VALIDATE + " but was " + datastoreValue,ErrorType.application,ErrorTag.data_missing,ErrorSeverity.error);
64 protected String getOperationName() {
69 protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
72 transactionProvider.validateTransaction();
73 } catch (ValidationException e) {
74 logger.warn("Validation failed", e);
75 throw NetconfDocumentedException.wrap(e);
76 } catch (IllegalStateException e) {
77 logger.warn("Validation failed", e);
78 final Map<String, String> errorInfo = new HashMap<>();
80 .put(ErrorTag.operation_failed.name(),
81 "Datastore is not present. Use 'get-config' or 'edit-config' before triggering 'operations' operation");
82 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
83 ErrorSeverity.error, errorInfo);
87 logger.trace("Datastore {} validated successfully", Datastore.candidate);
89 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());