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