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.base.Optional;
12 import org.opendaylight.controller.config.facade.xml.ConfigExecution;
13 import org.opendaylight.controller.config.facade.xml.Datastore;
14 import org.opendaylight.controller.config.facade.xml.TestOption;
15 import org.opendaylight.controller.config.facade.xml.mapping.config.Config;
16 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
17 import org.opendaylight.controller.config.util.xml.DocumentedException;
18 import org.opendaylight.controller.config.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class EditConfigXmlParser {
25 private static final Logger LOG = LoggerFactory.getLogger(EditConfigXmlParser.class);
27 public static final String EDIT_CONFIG = "edit-config";
28 public static final String DEFAULT_OPERATION_KEY = "default-operation";
29 static final String ERROR_OPTION_KEY = "error-option";
30 static final String DEFAULT_ERROR_OPTION = "stop-on-error";
31 static final String TARGET_KEY = "target";
32 static final String TEST_OPTION_KEY = "test-option";
34 public EditConfigXmlParser() {
37 ConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
38 throws DocumentedException {
40 //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
42 EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
44 xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
45 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
48 XmlElement targetElement = null;
49 XmlElement targetChildNode = null;
50 targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
51 targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
53 String datastoreValue = targetChildNode.getName();
54 Datastore targetDatastore = Datastore.valueOf(datastoreValue);
55 LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
58 if (targetDatastore != Datastore.candidate){
59 throw new DocumentedException(String.format(
60 "Only %s datastore supported for edit config but was: %s",
63 DocumentedException.ErrorType.application,
64 DocumentedException.ErrorTag.invalid_value,
65 DocumentedException.ErrorSeverity.error);
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 = TestOption.getFromXmlName(testOptionValue);
76 testOption = TestOption.getDefault();
78 LOG.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 (!errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)){
86 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
87 + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
92 Optional<XmlElement> defaultContent = xml
93 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
94 if (defaultContent.isPresent()) {
95 String mergeStrategyString = defaultContent.get().getTextContent();
96 LOG.trace("Setting merge strategy to {}", mergeStrategyString);
97 editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
100 XmlElement configElement = null;
101 configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
103 return new ConfigExecution(cfgMapping, configElement, testOption, editStrategyType);