2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.confignetconfconnector.operations;
11 import com.google.common.base.Optional;
12 import java.util.HashMap;
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;
29 public class Validate extends AbstractConfigNetconfOperation {
31 public static final String VALIDATE = "validate";
33 private static final Logger LOG = LoggerFactory.getLogger(Validate.class);
35 public Validate(final ConfigSubsystemFacade configSubsystemFacade, final String netconfSessionIdForReporting) {
36 super(configSubsystemFacade, netconfSessionIdForReporting);
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);
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();
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);
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);
58 protected String getOperationName() {
63 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement xml) throws DocumentedException {
66 getConfigSubsystemFacade().validateConfiguration();
67 } catch (final ValidationException e) {
68 LOG.warn("Validation failed", e);
69 throw DocumentedException.wrap(e);
70 } catch (final IllegalStateException e) {
71 LOG.warn("Validation failed", e);
72 final Map<String, String> errorInfo = new HashMap<>();
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);
81 LOG.trace("Datastore {} validated successfully", Datastore.candidate);
83 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());