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