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