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 java.util.Arrays;
16 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementDefinition;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
24 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
25 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
26 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
27 import org.opendaylight.controller.netconf.util.xml.XmlElement;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class EditConfigXmlParser {
33 private static final Logger LOG = LoggerFactory.getLogger(EditConfigXmlParser.class);
35 public static final String EDIT_CONFIG = "edit-config";
36 public static final String DEFAULT_OPERATION_KEY = "default-operation";
37 static final String ERROR_OPTION_KEY = "error-option";
38 static final String DEFAULT_ERROR_OPTION = "stop-on-error";
39 static final String TARGET_KEY = "target";
40 static final String TEST_OPTION_KEY = "test-option";
42 public EditConfigXmlParser() {
45 EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
46 throws NetconfDocumentedException {
48 //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
50 EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
52 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
53 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
56 XmlElement targetElement = null;
57 XmlElement targetChildNode = null;
59 targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
60 targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
61 } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
62 LOG.trace("Can't get only child element with same namespace", e);
63 throw NetconfDocumentedException.wrap(e);
65 String datastoreValue = targetChildNode.getName();
66 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
67 LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
70 if (targetDatastore != Datastore.candidate){
71 throw new NetconfDocumentedException(String.format(
72 "Only %s datastore supported for edit config but was: %s",
75 NetconfDocumentedException.ErrorType.application,
76 NetconfDocumentedException.ErrorTag.invalid_value,
77 NetconfDocumentedException.ErrorSeverity.error);
81 TestOption testOption;
82 Optional<XmlElement> testOptionElementOpt = xml
83 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
84 if (testOptionElementOpt.isPresent()) {
85 String testOptionValue = testOptionElementOpt.get().getTextContent();
86 testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
88 testOption = EditConfigXmlParser.TestOption.getDefault();
90 LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
93 Optional<XmlElement> errorOptionElement = xml
94 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
95 if (errorOptionElement.isPresent()) {
96 String errorOptionParsed = errorOptionElement.get().getTextContent();
97 if (!errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)){
98 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
99 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
104 Optional<XmlElement> defaultContent = xml
105 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
106 if (defaultContent.isPresent()) {
107 String mergeStrategyString = defaultContent.get().getTextContent();
108 LOG.trace("Setting merge strategy to {}", mergeStrategyString);
109 editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
112 XmlElement configElement = null;
114 configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
115 } catch (MissingNameSpaceException e) {
116 LOG.trace("Can't get only child element with same namespace due to ",e);
117 throw NetconfDocumentedException.wrap(e);
120 return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
124 static enum TestOption {
125 testOnly, set, testThenSet;
127 static TestOption getFromXmlName(String testOptionXmlName) {
128 switch (testOptionXmlName) {
131 case "test-then-set":
136 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
137 + Arrays.toString(TestOption.values()));
141 public static TestOption getDefault() {
148 static class EditConfigExecution {
150 private final TestOption testOption;
151 private final EditStrategyType defaultEditStrategyType;
152 private final Services services;
153 private final Config configResolver;
154 private final XmlElement configElement;
156 EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
157 Config.checkUnrecognisedChildren(configElement);
158 this.configResolver = configResolver;
159 this.configElement = configElement;
160 this.services = configResolver.fromXmlServices(configElement);
161 this.testOption = testOption;
162 this.defaultEditStrategyType = defaultStrategy;
165 boolean shouldTest() {
166 return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
169 boolean shouldSet() {
170 return testOption == TestOption.set || testOption == TestOption.testThenSet;
173 Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
174 return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
177 ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
178 // TODO cache service registry
179 return new ServiceRegistryWrapper(serviceRegistry);
182 Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
183 return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
186 EditStrategyType getDefaultStrategy() {
187 return defaultEditStrategyType;
190 Services getServices() {