Merge "Bug 8153: Enforce check-style rules for netconf - netconf-tcp"
[netconf.git] / netconf / config-netconf-connector / src / main / java / org / opendaylight / 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.netconf.confignetconfconnector.operations;
10
11 import com.google.common.base.Optional;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.controller.config.api.ValidationException;
15 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
16 import org.opendaylight.controller.config.facade.xml.Datastore;
17 import org.opendaylight.controller.config.util.xml.DocumentedException;
18 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
19 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
20 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
21 import org.opendaylight.controller.config.util.xml.XmlElement;
22 import org.opendaylight.controller.config.util.xml.XmlUtil;
23 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28
29 public class Validate extends AbstractConfigNetconfOperation {
30
31     public static final String VALIDATE = "validate";
32
33     private static final Logger LOG = LoggerFactory.getLogger(Validate.class);
34
35     public Validate(final ConfigSubsystemFacade configSubsystemFacade, final String netconfSessionIdForReporting) {
36         super(configSubsystemFacade, netconfSessionIdForReporting);
37     }
38
39     private void checkXml(final XmlElement xml) throws DocumentedException {
40         xml.checkName(VALIDATE);
41         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
42
43         final XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
44                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
45         final XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
46
47         sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
48         final String datastoreValue = sourceChildNode.getName();
49         final Datastore sourceDatastore = Datastore.valueOf(datastoreValue);
50
51         if (sourceDatastore != Datastore.candidate) {
52             throw new DocumentedException("Only " + Datastore.candidate
53                     + " is supported as source for " + VALIDATE + " but was " + datastoreValue, ErrorType.APPLICATION,
54                     ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
55         }
56     }
57
58     @Override
59     protected String getOperationName() {
60         return VALIDATE;
61     }
62
63     @Override
64     protected Element handleWithNoSubsequentOperations(final Document document,
65                                                        final XmlElement xml) throws DocumentedException {
66         checkXml(xml);
67         try {
68             getConfigSubsystemFacade().validateConfiguration();
69         } catch (final ValidationException e) {
70             LOG.warn("Validation failed", e);
71             throw DocumentedException.wrap(e);
72         } catch (final IllegalStateException e) {
73             LOG.warn("Validation failed", e);
74             final Map<String, String> errorInfo = new HashMap<>();
75             errorInfo
76                     .put(ErrorTag.OPERATION_FAILED.name(),
77                             "Datastore is not present. "
78                                     + "Use 'get-config' or 'edit-config' before triggering 'operations' operation");
79             throw new DocumentedException(e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
80                     ErrorSeverity.ERROR, errorInfo);
81
82         }
83
84         LOG.trace("Datastore {} validated successfully", Datastore.candidate);
85
86         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
87     }
88 }