Migrate netconf-console to Karaf 4 way
[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 java.util.Map;
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;
25
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";
30
31     private final TransactionProvider transactionProvider;
32
33     public Validate(final String netconfSessionIdForReporting, final TransactionProvider transactionProvider) {
34         super(netconfSessionIdForReporting);
35         this.transactionProvider = transactionProvider;
36     }
37
38     @Override
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,
46                 ErrorSeverity.ERROR);
47         }
48
49         transactionProvider.validateTransaction();
50         LOG.trace("<validate> request completed successfully on session {}", getNetconfSessionIdForReporting());
51         return XmlUtil.createElement(document, XmlNetconfConstants.OK);
52     }
53
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",
60                 operationName);
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,
65                 ErrorSeverity.ERROR);
66         } else {
67             final XmlElement sourceChildNode =
68                 XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
69             return Datastore.valueOf(sourceChildNode.getName());
70         }
71     }
72
73     @Override
74     protected String getOperationName() {
75         return OPERATION_NAME;
76     }
77 }