Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / EditConfigXmlParser.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.editconfig;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.config.facade.xml.ConfigExecution;
13 import org.opendaylight.controller.config.facade.xml.Datastore;
14 import org.opendaylight.controller.config.facade.xml.TestOption;
15 import org.opendaylight.controller.config.facade.xml.mapping.config.Config;
16 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
17 import org.opendaylight.controller.config.util.xml.DocumentedException;
18 import org.opendaylight.controller.config.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class EditConfigXmlParser {
24
25     private static final Logger LOG = LoggerFactory.getLogger(EditConfigXmlParser.class);
26
27     public static final String EDIT_CONFIG = "edit-config";
28     public static final String DEFAULT_OPERATION_KEY = "default-operation";
29     static final String ERROR_OPTION_KEY = "error-option";
30     static final String DEFAULT_ERROR_OPTION = "stop-on-error";
31     static final String TARGET_KEY = "target";
32     static final String TEST_OPTION_KEY = "test-option";
33
34     public EditConfigXmlParser() {
35     }
36
37     ConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
38             throws DocumentedException {
39
40         //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
41
42         EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
43
44         xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
45         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
46
47
48         XmlElement targetElement = null;
49         XmlElement targetChildNode = null;
50         targetElement  = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
51         targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
52
53         String datastoreValue = targetChildNode.getName();
54         Datastore targetDatastore = Datastore.valueOf(datastoreValue);
55         LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
56
57         // check target
58         if (targetDatastore != Datastore.candidate){
59             throw new DocumentedException(String.format(
60                     "Only %s datastore supported for edit config but was: %s",
61                     Datastore.candidate,
62                     targetDatastore),
63                     DocumentedException.ErrorType.application,
64                     DocumentedException.ErrorTag.invalid_value,
65                     DocumentedException.ErrorSeverity.error);
66         }
67
68         // Test option
69         TestOption testOption;
70         Optional<XmlElement> testOptionElementOpt = xml
71                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
72         if (testOptionElementOpt.isPresent()) {
73             String testOptionValue = testOptionElementOpt.get().getTextContent();
74             testOption = TestOption.getFromXmlName(testOptionValue);
75         } else {
76             testOption = TestOption.getDefault();
77         }
78         LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
79
80         // Error option
81         Optional<XmlElement> errorOptionElement = xml
82                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
83         if (errorOptionElement.isPresent()) {
84             String errorOptionParsed = errorOptionElement.get().getTextContent();
85             if (!errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)){
86                 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
87                         + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
88             }
89         }
90
91         // Default op
92         Optional<XmlElement> defaultContent = xml
93                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
94         if (defaultContent.isPresent()) {
95             String mergeStrategyString = defaultContent.get().getTextContent();
96             LOG.trace("Setting merge strategy to {}", mergeStrategyString);
97             editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
98         }
99
100         XmlElement configElement = null;
101         configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
102
103         return new ConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
104     }
105 }