Get rid of netconf operation filters
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / Validate.java
1 /*
2  * Copyright (c) 2013 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.controller.netconf.confignetconfconnector.operations;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.controller.config.api.ValidationException;
15 import org.opendaylight.controller.config.util.ConfigRegistryClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
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.XmlNetconfConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 import com.google.common.base.Preconditions;
29
30 public class Validate extends AbstractConfigNetconfOperation {
31
32     public static final String VALIDATE = "validate";
33
34     private static final Logger logger = LoggerFactory.getLogger(Validate.class);
35
36     private final TransactionProvider transactionProvider;
37
38     public Validate(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
39             String netconfSessionIdForReporting) {
40         super(configRegistryClient, netconfSessionIdForReporting);
41         this.transactionProvider = transactionProvider;
42     }
43
44     private void checkXml(XmlElement xml) {
45         xml.checkName(VALIDATE);
46         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
47
48         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
49                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
50         XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
51
52         sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
53         String datastoreValue = sourceChildNode.getName();
54         Datastore sourceDatastore = Datastore.valueOf(datastoreValue);
55
56         Preconditions.checkState(sourceDatastore == Datastore.candidate, "Only " + Datastore.candidate
57                 + " is supported as source for " + VALIDATE + " but was " + datastoreValue);
58     }
59
60     @Override
61     protected String getOperationName() {
62         return VALIDATE;
63     }
64
65     @Override
66     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
67         try {
68             checkXml(xml);
69         } catch (IllegalStateException e) {
70             //FIXME where can IllegalStateException  be thrown? I see precondition that guards for programming bugs..
71             logger.warn("Rpc error: {}", ErrorTag.missing_attribute, e);
72             final Map<String, String> errorInfo = new HashMap<>();
73             errorInfo.put(ErrorTag.missing_attribute.name(), "Missing value of datastore attribute");
74             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.missing_attribute,
75                     ErrorSeverity.error, errorInfo);
76         } catch (final IllegalArgumentException e) {
77             // FIXME use checked exception if it has domain meaning
78             logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
79             final Map<String, String> errorInfo = new HashMap<>();
80             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
81             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
82                     ErrorSeverity.error, errorInfo);
83         }
84
85         try {
86             transactionProvider.validateTransaction();
87         } catch (ValidationException e) {
88             logger.warn("Validation failed", e);
89             throw NetconfDocumentedException.wrap(e);
90         } catch (IllegalStateException e) {
91             logger.warn("Validation failed", e);
92             final Map<String, String> errorInfo = new HashMap<>();
93             errorInfo
94                     .put(ErrorTag.operation_failed.name(),
95                             "Datastore is not present. Use 'get-config' or 'edit-config' before triggering 'operations' operation");
96             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
97                     ErrorSeverity.error, errorInfo);
98
99         }
100
101         logger.trace("Datastore {} validated successfully", Datastore.candidate);
102
103         return document.createElement(XmlNetconfConstants.OK);
104     }
105 }