Merge "Bug-835:Reserve ports should be logical ports"
[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
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
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.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 public class Validate extends AbstractConfigNetconfOperation {
32
33     public static final String VALIDATE = "validate";
34
35     private static final Logger logger = LoggerFactory.getLogger(Validate.class);
36
37     private final TransactionProvider transactionProvider;
38
39     public Validate(final TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
40             String netconfSessionIdForReporting) {
41         super(configRegistryClient, netconfSessionIdForReporting);
42         this.transactionProvider = transactionProvider;
43     }
44
45     private void checkXml(XmlElement xml) throws NetconfDocumentedException {
46         xml.checkName(VALIDATE);
47         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
48
49         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
50                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
51         XmlElement sourceChildNode = sourceElement.getOnlyChildElement();
52
53         sourceChildNode.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
54         String datastoreValue = sourceChildNode.getName();
55         Datastore sourceDatastore = Datastore.valueOf(datastoreValue);
56
57         if (sourceDatastore != Datastore.candidate){
58             throw new NetconfDocumentedException( "Only " + Datastore.candidate
59                     + " is supported as source for " + VALIDATE + " but was " + datastoreValue,ErrorType.application,ErrorTag.data_missing,ErrorSeverity.error);
60         }
61     }
62
63     @Override
64     protected String getOperationName() {
65         return VALIDATE;
66     }
67
68     @Override
69     protected Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
70         checkXml(xml);
71         try {
72             transactionProvider.validateTransaction();
73         } catch (ValidationException e) {
74             logger.warn("Validation failed", e);
75             throw NetconfDocumentedException.wrap(e);
76         } catch (IllegalStateException e) {
77             logger.warn("Validation failed", e);
78             final Map<String, String> errorInfo = new HashMap<>();
79             errorInfo
80                     .put(ErrorTag.operation_failed.name(),
81                             "Datastore is not present. Use 'get-config' or 'edit-config' before triggering 'operations' operation");
82             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application, ErrorTag.operation_failed,
83                     ErrorSeverity.error, errorInfo);
84
85         }
86
87         logger.trace("Datastore {} validated successfully", Datastore.candidate);
88
89         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
90     }
91 }