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.api.ServiceReferenceReadableRegistry;
16 import org.opendaylight.controller.config.util.ConfigRegistryClient;
17 import org.opendaylight.controller.config.util.ConfigTransactionClient;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
22 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
23 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import javax.management.ObjectName;
30 import java.util.Arrays;
33 public class EditConfigXmlParser {
35 private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
37 public static final String EDIT_CONFIG = "edit-config";
38 public static final String DEFAULT_OPERATION_KEY = "default-operation";
39 static final String ERROR_OPTION_KEY = "error-option";
40 static final String DEFAULT_ERROR_OPTION = "stop-on-error";
41 static final String TARGET_KEY = "target";
42 static final String TEST_OPTION_KEY = "test-option";
44 public EditConfigXmlParser() {
47 EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping,
48 TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient)
49 throws NetconfDocumentedException {
51 //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
53 EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
55 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
56 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
58 XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
59 XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
60 String datastoreValue = targetChildNode.getName();
61 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
62 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
65 Preconditions.checkArgument(targetDatastore == Datastore.candidate,
66 "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore);
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 = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
76 testOption = EditConfigXmlParser.TestOption.getDefault();
78 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
81 Optional<XmlElement> errorOptionElement = xml
82 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
83 if (errorOptionElement.isPresent()) {
84 String errorOptionParsed = errorOptionElement.get().getTextContent();
85 if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
86 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
87 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
91 Optional<XmlElement> defaultContent = xml
92 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
93 if (defaultContent.isPresent()) {
94 String mergeStrategyString = defaultContent.get().getTextContent();
95 logger.trace("Setting merge strategy to {}", mergeStrategyString);
96 editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
99 XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
101 ObjectName taON = transactionProvider.getOrCreateTransaction();
102 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(taON);
104 return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption,
105 ta, editStrategyType);
109 static enum TestOption {
110 testOnly, set, testThenSet;
112 static TestOption getFromXmlName(String testOptionXmlName) {
113 switch (testOptionXmlName) {
116 case "test-then-set":
121 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
122 + Arrays.toString(TestOption.values()));
126 public static TestOption getDefault() {
133 static class EditConfigExecution {
135 private final Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
136 private final TestOption testOption;
137 private final EditStrategyType defaultEditStrategyType;
138 private final Services services;
140 EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, ServiceReferenceReadableRegistry ta, EditStrategyType defaultStrategy) {
141 Config.ConfigElementResolved configElementResolved = configResolver.fromXml(configElement, defaultStrategy, ta);
142 this.resolvedXmlElements = configElementResolved.getResolvedModules();
143 this.services = configElementResolved.getServices();
144 this.testOption = testOption;
145 this.defaultEditStrategyType = defaultStrategy;
148 boolean shouldTest() {
149 return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
152 boolean shouldSet() {
153 return testOption == TestOption.set || testOption == TestOption.testThenSet;
156 Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements() {
157 return resolvedXmlElements;
160 EditStrategyType getDefaultStrategy() {
161 return defaultEditStrategyType;
164 Services getServices() {