Remove netconf from commons/opendaylight pom
[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.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.controller.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(XmlElement xml) throws DocumentedException {
40         xml.checkName(VALIDATE);
41         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
42
43         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
44                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
45         XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
46
47         sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
48         String datastoreValue = sourceChildNode.getName();
49         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, ErrorTag.data_missing, ErrorSeverity.error);
54         }
55     }
56
57     @Override
58     protected String getOperationName() {
59         return VALIDATE;
60     }
61
62     @Override
63     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws DocumentedException {
64         checkXml(xml);
65         try {
66             getConfigSubsystemFacade().validateConfiguration();
67         } catch (ValidationException e) {
68             LOG.warn("Validation failed", e);
69             throw DocumentedException.wrap(e);
70         } catch (IllegalStateException e) {
71             LOG.warn("Validation failed", e);
72             final Map<String, String> errorInfo = new HashMap<>();
73             errorInfo
74                     .put(ErrorTag.operation_failed.name(),
75                             "Datastore is not present. Use 'get-config' or 'edit-config' before triggering 'operations' operation");
76             throw new DocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
77                     ErrorSeverity.error, errorInfo);
78
79         }
80
81         LOG.trace("Datastore {} validated successfully", Datastore.candidate);
82
83         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
84     }
85 }