Refactor yang-jmx-generator and -plugin to support list of dependencies.
[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 com.google.common.base.Preconditions;
12 import org.opendaylight.controller.config.api.ValidationException;
13 import org.opendaylight.controller.config.util.ConfigRegistryClient;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
18 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 public class Validate extends AbstractConfigNetconfOperation {
30
31     public static final String VALIDATE = "validate";
32
33     private static final Logger logger = LoggerFactory.getLogger(Validate.class);
34
35     private final TransactionProvider transactionProvider;
36
37     public Validate(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
38             String netconfSessionIdForReporting) {
39         super(configRegistryClient, netconfSessionIdForReporting);
40         this.transactionProvider = transactionProvider;
41     }
42
43     private void checkXml(XmlElement xml) {
44         xml.checkName(VALIDATE);
45         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
46
47         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
48                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
49         XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
50
51         sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
52         String datastoreValue = sourceChildNode.getName();
53         Datastore sourceDatastore = Datastore.valueOf(datastoreValue);
54
55         Preconditions.checkState(sourceDatastore == Datastore.candidate, "Only " + Datastore.candidate
56                 + " is supported as source for " + VALIDATE + " but was " + datastoreValue);
57     }
58
59     @Override
60     protected String getOperationName() {
61         return VALIDATE;
62     }
63
64     @Override
65     protected Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
66         try {
67             checkXml(xml);
68         } catch (IllegalStateException e) {
69             logger.warn("Rpc error: {}", ErrorTag.missing_attribute, e);
70             final Map<String, String> errorInfo = new HashMap<>();
71             errorInfo.put(ErrorTag.missing_attribute.name(), "Missing value of datastore attribute");
72             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.missing_attribute,
73                     ErrorSeverity.error, errorInfo);
74         } catch (final IllegalArgumentException e) {
75             logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
76             final Map<String, String> errorInfo = new HashMap<>();
77             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
78             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
79                     ErrorSeverity.error, errorInfo);
80         }
81
82         try {
83             transactionProvider.validateTransaction();
84         } catch (ValidationException e) {
85             logger.warn("Validation failed", e);
86             final Map<String, String> errorInfo = new HashMap<>();
87             errorInfo.put(ErrorTag.operation_failed.name(), "Validation failed");
88             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
89                     ErrorSeverity.error, errorInfo);
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.info("Datastore {} validated successfully", Datastore.candidate);
102
103         return document.createElement(XmlNetconfConstants.OK);
104     }
105 }