X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fconfignetconfconnector%2Foperations%2Feditconfig%2FEditConfigXmlParser.java;h=99c0a2ff0d719865b7d39cc22143e563ce99c0fc;hb=250542ef463f05ba5c871c128afeb33c7af93ea8;hp=e481bbe57f68bf4d043a8365362ce2c90ab46893;hpb=515e60d60ac7dd08aa2440468cd8dab42892e7d0;p=controller.git diff --git a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/EditConfigXmlParser.java b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/EditConfigXmlParser.java index e481bbe57f..99c0a2ff0d 100644 --- a/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/EditConfigXmlParser.java +++ b/opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/EditConfigXmlParser.java @@ -10,29 +10,27 @@ package org.opendaylight.controller.netconf.confignetconfconnector.operations.ed import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; -import com.google.common.base.Preconditions; import com.google.common.collect.Multimap; +import java.util.Arrays; +import java.util.Map; import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry; -import org.opendaylight.controller.config.util.ConfigRegistryClient; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementDefinition; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper; import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services; import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore; -import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider; +import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException; +import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException; import org.opendaylight.controller.netconf.util.xml.XmlElement; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Arrays; -import java.util.Map; - public class EditConfigXmlParser { - private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class); + private static final Logger LOG = LoggerFactory.getLogger(EditConfigXmlParser.class); public static final String EDIT_CONFIG = "edit-config"; public static final String DEFAULT_OPERATION_KEY = "default-operation"; @@ -44,8 +42,7 @@ public class EditConfigXmlParser { public EditConfigXmlParser() { } - EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping, - TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient) + EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping) throws NetconfDocumentedException { //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store @@ -55,15 +52,30 @@ public class EditConfigXmlParser { xml.checkName(EditConfigXmlParser.EDIT_CONFIG); xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); - XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY); - XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace(); + + XmlElement targetElement = null; + XmlElement targetChildNode = null; + try { + targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY); + targetChildNode = targetElement.getOnlyChildElementWithSameNamespace(); + } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) { + LOG.trace("Can't get only child element with same namespace", e); + throw NetconfDocumentedException.wrap(e); + } String datastoreValue = targetChildNode.getName(); Datastore targetDatastore = Datastore.valueOf(datastoreValue); - logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore); + LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore); // check target - Preconditions.checkArgument(targetDatastore == Datastore.candidate, - "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore); + if (targetDatastore != Datastore.candidate){ + throw new NetconfDocumentedException(String.format( + "Only %s datastore supported for edit config but was: %s", + Datastore.candidate, + targetDatastore), + NetconfDocumentedException.ErrorType.application, + NetconfDocumentedException.ErrorTag.invalid_value, + NetconfDocumentedException.ErrorSeverity.error); + } // Test option TestOption testOption; @@ -75,16 +87,17 @@ public class EditConfigXmlParser { } else { testOption = EditConfigXmlParser.TestOption.getDefault(); } - logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption); + LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption); // Error option Optional errorOptionElement = xml .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY); if (errorOptionElement.isPresent()) { String errorOptionParsed = errorOptionElement.get().getTextContent(); - if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)) + if (!errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION)){ throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed); + } } // Default op @@ -92,11 +105,17 @@ public class EditConfigXmlParser { .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY); if (defaultContent.isPresent()) { String mergeStrategyString = defaultContent.get().getTextContent(); - logger.trace("Setting merge strategy to {}", mergeStrategyString); + LOG.trace("Setting merge strategy to {}", mergeStrategyString); editStrategyType = EditStrategyType.valueOf(mergeStrategyString); } - XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY); + XmlElement configElement = null; + try { + configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY); + } catch (MissingNameSpaceException e) { + LOG.trace("Can't get only child element with same namespace due to ",e); + throw NetconfDocumentedException.wrap(e); + } return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType); } @@ -134,7 +153,7 @@ public class EditConfigXmlParser { private final Config configResolver; private final XmlElement configElement; - EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) { + EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException { Config.checkUnrecognisedChildren(configElement); this.configResolver = configResolver; this.configElement = configElement; @@ -151,7 +170,7 @@ public class EditConfigXmlParser { return testOption == TestOption.set || testOption == TestOption.testThenSet; } - Map> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) { + Map> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException { return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry)); } @@ -160,7 +179,7 @@ public class EditConfigXmlParser { return new ServiceRegistryWrapper(serviceRegistry); } - Map> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) { + Map> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException { return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry)); }