2 * Copyright (c) 2018 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
8 package org.opendaylight.netconf.mdsal.connector.ops;
10 import com.google.common.collect.ImmutableMap;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
14 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
15 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
16 import org.opendaylight.netconf.api.xml.XmlElement;
17 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.NodeList;
26 public final class Validate extends AbstractConfigOperation {
27 private static final Logger LOG = LoggerFactory.getLogger(Validate.class);
28 private static final String OPERATION_NAME = "validate";
29 private static final String SOURCE_KEY = "source";
31 private final TransactionProvider transactionProvider;
33 public Validate(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
34 super(netconfSessionIdForReporting);
35 this.transactionProvider = transactionProvider;
39 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
40 throws DocumentedException {
41 final Datastore targetDatastore = extractSourceParameter(operationElement, OPERATION_NAME);
42 if (targetDatastore != Datastore.candidate) {
43 throw new DocumentedException("<validate> is only supported on candidate datastore",
44 DocumentedException.ErrorType.PROTOCOL,
45 ErrorTag.OPERATION_NOT_SUPPORTED,
49 transactionProvider.validateTransaction();
50 LOG.trace("<validate> request completed successfully on session {}", getNetconfSessionIdForReporting());
51 return XmlUtil.createElement(document, XmlNetconfConstants.OK);
54 protected static Datastore extractSourceParameter(final XmlElement operationElement, final String operationName)
55 throws DocumentedException {
56 final NodeList elementsByTagName = getElementsByTagName(operationElement, SOURCE_KEY);
57 // Direct lookup instead of using XmlElement class due to performance
58 if (elementsByTagName.getLength() == 0) {
59 final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", SOURCE_KEY, "bad-element",
61 throw new DocumentedException("Missing source element", ErrorType.PROTOCOL, ErrorTag.MISSING_ELEMENT,
62 ErrorSeverity.ERROR, errorInfo);
63 } else if (elementsByTagName.getLength() > 1) {
64 throw new DocumentedException("Multiple source elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
67 final XmlElement sourceChildNode =
68 XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
69 return Datastore.valueOf(sourceChildNode.getName());
74 protected String getOperationName() {
75 return OPERATION_NAME;