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.config.util.ConfigRegistryClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
19 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
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.XmlNetconfConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import javax.management.ObjectName;
27 import java.util.Arrays;
28 import java.util.Collections;
32 public class EditConfigXmlParser {
34 private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
36 public static final String EDIT_CONFIG = "edit-config";
37 public static final String DEFAULT_OPERATION_KEY = "default-operation";
38 static final String ERROR_OPTION_KEY = "error-option";
39 static final String DEFAULT_ERROR_OPTION = "stop-on-error";
40 static final String TARGET_KEY = "target";
41 static final String TEST_OPTION_KEY = "test-option";
43 public EditConfigXmlParser() {
46 EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping,
47 TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient)
48 throws NetconfDocumentedException {
50 EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
52 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
53 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
55 XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
56 XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
57 String datastoreValue = targetChildNode.getName();
58 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
59 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
62 Preconditions.checkArgument(targetDatastore == Datastore.candidate,
63 "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore);
66 TestOption testOption;
67 Optional<XmlElement> testOptionElementOpt = xml
68 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
69 if (testOptionElementOpt.isPresent()) {
70 String testOptionValue = testOptionElementOpt.get().getTextContent();
71 testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
73 testOption = EditConfigXmlParser.TestOption.getDefault();
75 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
78 Optional<XmlElement> errorOptionElement = xml
79 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
80 if (errorOptionElement.isPresent()) {
81 String errorOptionParsed = errorOptionElement.get().getTextContent();
82 if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
83 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
84 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
88 Optional<XmlElement> defaultContent = xml
89 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
90 if (defaultContent.isPresent()) {
91 String mergeStrategyString = defaultContent.get().getTextContent();
92 logger.trace("Setting merge strategy to {}", mergeStrategyString);
93 editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
95 Set<ObjectName> instancesForFillingServiceRefMapping = Collections.emptySet();
96 if (editStrategyType == EditStrategyType.merge) {
97 instancesForFillingServiceRefMapping = Datastore.getInstanceQueryStrategy(targetDatastore, transactionProvider)
98 .queryInstances(configRegistryClient);
99 logger.trace("Pre-filling services from following instances: {}", instancesForFillingServiceRefMapping);
102 XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
104 return new EditConfigXmlParser.EditConfigExecution(xml, cfgMapping, configElement, testOption,
105 instancesForFillingServiceRefMapping, editStrategyType);
108 private void removeMountpointsFromConfig(XmlElement configElement, XmlElement mountpointsElement) {
109 configElement.getDomElement().removeChild(mountpointsElement.getDomElement());
113 static enum TestOption {
114 testOnly, set, testThenSet;
116 static TestOption getFromXmlName(String testOptionXmlName) {
117 switch (testOptionXmlName) {
120 case "test-then-set":
125 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
126 + Arrays.toString(TestOption.values()));
130 public static TestOption getDefault() {
137 static class EditConfigExecution {
138 private final XmlElement editConfigXml;
139 private final Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
140 private final TestOption testOption;
141 private final EditStrategyType defaultEditStrategyType;
143 EditConfigExecution(XmlElement xml, Config configResolver, XmlElement configElement, TestOption testOption, Set<ObjectName> instancesForFillingServiceRefMapping,
144 EditStrategyType defaultStrategy) {
145 this.editConfigXml = xml;
146 this.resolvedXmlElements = configResolver.fromXml(configElement, instancesForFillingServiceRefMapping, defaultStrategy);
147 this.testOption = testOption;
148 this.defaultEditStrategyType = defaultStrategy;
151 boolean shouldTest() {
152 return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
155 boolean shouldSet() {
156 return testOption == TestOption.set || testOption == TestOption.testThenSet;
159 Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements() {
160 return resolvedXmlElements;
163 EditStrategyType getDefaultStrategy() {
164 return defaultEditStrategyType;