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.collect.Multimap;
15 import java.util.Arrays;
18 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementDefinition;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
26 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
27 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
28 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
29 import org.opendaylight.controller.netconf.util.xml.XmlElement;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
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 throws NetconfDocumentedException {
50 //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
52 EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
54 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
55 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
58 XmlElement targetElement = null;
59 XmlElement targetChildNode = null;
61 targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
62 targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
63 } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
64 logger.trace("Can't get only child element with same namespace due to {}",e);
65 throw NetconfDocumentedException.wrap(e);
67 String datastoreValue = targetChildNode.getName();
68 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
69 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
72 if (targetDatastore != Datastore.candidate){
73 throw new NetconfDocumentedException(String.format(
74 "Only %s datastore supported for edit config but was: %s",
77 NetconfDocumentedException.ErrorType.application,
78 NetconfDocumentedException.ErrorTag.invalid_value,
79 NetconfDocumentedException.ErrorSeverity.error);
83 TestOption testOption;
84 Optional<XmlElement> testOptionElementOpt = xml
85 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
86 if (testOptionElementOpt.isPresent()) {
87 String testOptionValue = testOptionElementOpt.get().getTextContent();
88 testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
90 testOption = EditConfigXmlParser.TestOption.getDefault();
92 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
95 Optional<XmlElement> errorOptionElement = xml
96 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
97 if (errorOptionElement.isPresent()) {
98 String errorOptionParsed = errorOptionElement.get().getTextContent();
99 if (!errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)){
100 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
101 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
106 Optional<XmlElement> defaultContent = xml
107 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
108 if (defaultContent.isPresent()) {
109 String mergeStrategyString = defaultContent.get().getTextContent();
110 logger.trace("Setting merge strategy to {}", mergeStrategyString);
111 editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
114 XmlElement configElement = null;
116 configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
117 } catch (MissingNameSpaceException e) {
118 logger.trace("Can't get only child element with same namespace due to {}",e);
119 throw NetconfDocumentedException.wrap(e);
122 return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
126 static enum TestOption {
127 testOnly, set, testThenSet;
129 static TestOption getFromXmlName(String testOptionXmlName) {
130 switch (testOptionXmlName) {
133 case "test-then-set":
138 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
139 + Arrays.toString(TestOption.values()));
143 public static TestOption getDefault() {
150 static class EditConfigExecution {
152 private final TestOption testOption;
153 private final EditStrategyType defaultEditStrategyType;
154 private final Services services;
155 private final Config configResolver;
156 private final XmlElement configElement;
158 EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
159 Config.checkUnrecognisedChildren(configElement);
160 this.configResolver = configResolver;
161 this.configElement = configElement;
162 this.services = configResolver.fromXmlServices(configElement);
163 this.testOption = testOption;
164 this.defaultEditStrategyType = defaultStrategy;
167 boolean shouldTest() {
168 return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
171 boolean shouldSet() {
172 return testOption == TestOption.set || testOption == TestOption.testThenSet;
175 Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
176 return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
179 ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
180 // TODO cache service registry
181 return new ServiceRegistryWrapper(serviceRegistry);
184 Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
185 return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
188 EditStrategyType getDefaultStrategy() {
189 return defaultEditStrategyType;
192 Services getServices() {