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.controller.netconf.confignetconfconnector.operations.editconfig;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Multimap;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
18 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import java.util.Arrays;
27 public class EditConfigXmlParser {
29 private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
31 public static final String EDIT_CONFIG = "edit-config";
32 public static final String DEFAULT_OPERATION_KEY = "default-operation";
33 static final String ERROR_OPTION_KEY = "error-option";
34 static final String DEFAULT_ERROR_OPTION = "stop-on-error";
35 static final String TARGET_KEY = "target";
36 static final String TEST_OPTION_KEY = "test-option";
38 public EditConfigXmlParser() {
41 EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
42 throws NetconfDocumentedException {
44 EditStrategyType.resetDefaultStrategy();
46 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
47 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
49 XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
50 XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
51 String datastoreValue = targetChildNode.getName();
52 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
53 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
56 Preconditions.checkArgument(targetDatastore == Datastore.candidate,
57 "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore);
60 TestOption testOption;
61 Optional<XmlElement> testOptionElementOpt = xml
62 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
63 if (testOptionElementOpt.isPresent()) {
64 String testOptionValue = testOptionElementOpt.get().getTextContent();
65 testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
67 testOption = EditConfigXmlParser.TestOption.getDefault();
69 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
72 Optional<XmlElement> errorOptionElement = xml
73 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
74 if (errorOptionElement.isPresent()) {
75 String errorOptionParsed = errorOptionElement.get().getTextContent();
76 if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
77 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
78 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
82 Optional<XmlElement> defaultContent = xml
83 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
84 if (defaultContent.isPresent())
85 EditStrategyType.setDefaultStrategy(EditStrategyType.valueOf(defaultContent.get().getTextContent()));
87 XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
89 return new EditConfigXmlParser.EditConfigExecution(xml, cfgMapping, configElement, testOption);
92 private void removeMountpointsFromConfig(XmlElement configElement, XmlElement mountpointsElement) {
93 configElement.getDomElement().removeChild(mountpointsElement.getDomElement());
97 static enum TestOption {
98 testOnly, set, testThenSet;
100 static TestOption getFromXmlName(String testOptionXmlName) {
101 switch (testOptionXmlName) {
104 case "test-then-set":
109 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
110 + Arrays.toString(TestOption.values()));
114 public static TestOption getDefault() {
121 static class EditConfigExecution {
122 XmlElement editConfigXml;
123 Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
124 TestOption testOption;
126 EditConfigExecution(XmlElement xml, Config configResolver, XmlElement configElement, TestOption testOption) {
127 this.editConfigXml = xml;
128 this.resolvedXmlElements = configResolver.fromXml(configElement);
129 this.testOption = testOption;
132 boolean shouldTest() {
133 return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
136 boolean shouldSet() {
137 return testOption == TestOption.set || testOption == TestOption.testThenSet;