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;
14 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
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.ModuleElementDefinition;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
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.exception.MissingNameSpaceException;
25 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
26 import org.opendaylight.controller.netconf.util.xml.XmlElement;
27 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.util.Arrays;
34 public class EditConfigXmlParser {
36 private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
38 public static final String EDIT_CONFIG = "edit-config";
39 public static final String DEFAULT_OPERATION_KEY = "default-operation";
40 static final String ERROR_OPTION_KEY = "error-option";
41 static final String DEFAULT_ERROR_OPTION = "stop-on-error";
42 static final String TARGET_KEY = "target";
43 static final String TEST_OPTION_KEY = "test-option";
45 public EditConfigXmlParser() {
48 EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping,
49 TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient)
50 throws NetconfDocumentedException {
52 //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
54 EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
56 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
57 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
60 XmlElement targetElement = null;
61 XmlElement targetChildNode = null;
63 targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
64 targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
65 } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
66 logger.trace("Can't get only child element with same namespace due to {}",e);
67 throw NetconfDocumentedException.wrap(e);
69 String datastoreValue = targetChildNode.getName();
70 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
71 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
74 if (targetDatastore != Datastore.candidate){
75 throw new NetconfDocumentedException(String.format(
76 "Only %s datastore supported for edit config but was: %s",
79 NetconfDocumentedException.ErrorType.application,
80 NetconfDocumentedException.ErrorTag.invalid_value,
81 NetconfDocumentedException.ErrorSeverity.error);
85 TestOption testOption;
86 Optional<XmlElement> testOptionElementOpt = xml
87 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
88 if (testOptionElementOpt.isPresent()) {
89 String testOptionValue = testOptionElementOpt.get().getTextContent();
90 testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
92 testOption = EditConfigXmlParser.TestOption.getDefault();
94 logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
97 Optional<XmlElement> errorOptionElement = xml
98 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
99 if (errorOptionElement.isPresent()) {
100 String errorOptionParsed = errorOptionElement.get().getTextContent();
101 if (!errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)){
102 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
103 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
108 Optional<XmlElement> defaultContent = xml
109 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
110 if (defaultContent.isPresent()) {
111 String mergeStrategyString = defaultContent.get().getTextContent();
112 logger.trace("Setting merge strategy to {}", mergeStrategyString);
113 editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
116 XmlElement configElement = null;
118 configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
119 } catch (MissingNameSpaceException e) {
120 logger.trace("Can't get only child element with same namespace due to {}",e);
121 throw NetconfDocumentedException.wrap(e);
124 return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
128 static enum TestOption {
129 testOnly, set, testThenSet;
131 static TestOption getFromXmlName(String testOptionXmlName) {
132 switch (testOptionXmlName) {
135 case "test-then-set":
140 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
141 + Arrays.toString(TestOption.values()));
145 public static TestOption getDefault() {
152 static class EditConfigExecution {
154 private final TestOption testOption;
155 private final EditStrategyType defaultEditStrategyType;
156 private final Services services;
157 private final Config configResolver;
158 private final XmlElement configElement;
160 EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
161 Config.checkUnrecognisedChildren(configElement);
162 this.configResolver = configResolver;
163 this.configElement = configElement;
164 this.services = configResolver.fromXmlServices(configElement);
165 this.testOption = testOption;
166 this.defaultEditStrategyType = defaultStrategy;
169 boolean shouldTest() {
170 return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
173 boolean shouldSet() {
174 return testOption == TestOption.set || testOption == TestOption.testThenSet;
177 Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
178 return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
181 ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
182 // TODO cache service registry
183 return new ServiceRegistryWrapper(serviceRegistry);
186 Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
187 return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
190 EditStrategyType getDefaultStrategy() {
191 return defaultEditStrategyType;
194 Services getServices() {