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 org.opendaylight.controller.config.util.ConfigRegistryClient;
+import java.util.Arrays;
+import java.util.Map;
+import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
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 javax.management.ObjectName;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-
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";
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 {
- EditStrategyType.resetDefaultStrategy();
+ //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
+
+ EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
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;
} 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<XmlElement> 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
.getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
if (defaultContent.isPresent()) {
String mergeStrategyString = defaultContent.get().getTextContent();
- logger.trace("Setting merge strategy to {}", mergeStrategyString);
- EditStrategyType editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
- // FIXME: thread safety, remove global state
- EditStrategyType.setDefaultStrategy(editStrategyType);
+ LOG.trace("Setting merge strategy to {}", mergeStrategyString);
+ editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
}
- // FIXME: thread safety, remove global state
- Set<ObjectName> instancesForFillingServiceRefMapping = Collections.emptySet();
- if (EditStrategyType.defaultStrategy() == EditStrategyType.merge) {
- instancesForFillingServiceRefMapping = Datastore.getInstanceQueryStrategy(targetDatastore, transactionProvider)
- .queryInstances(configRegistryClient);
- logger.trace("Pre-filling services from following instances: {}", instancesForFillingServiceRefMapping);
- }
-
- XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
- return new EditConfigXmlParser.EditConfigExecution(xml, cfgMapping, configElement, testOption, instancesForFillingServiceRefMapping);
- }
+ 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);
+ }
- private void removeMountpointsFromConfig(XmlElement configElement, XmlElement mountpointsElement) {
- configElement.getDomElement().removeChild(mountpointsElement.getDomElement());
+ return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
}
@VisibleForTesting
@VisibleForTesting
static class EditConfigExecution {
- XmlElement editConfigXml;
- Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
- TestOption testOption;
- EditConfigExecution(XmlElement xml, Config configResolver, XmlElement configElement, TestOption testOption, Set<ObjectName> instancesForFillingServiceRefMapping) {
- this.editConfigXml = xml;
- this.resolvedXmlElements = configResolver.fromXml(configElement, instancesForFillingServiceRefMapping);
+ private final TestOption testOption;
+ private final EditStrategyType defaultEditStrategyType;
+ private final Services services;
+ private final Config configResolver;
+ private final XmlElement configElement;
+
+ EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
+ Config.checkUnrecognisedChildren(configElement);
+ this.configResolver = configResolver;
+ this.configElement = configElement;
+ this.services = configResolver.fromXmlServices(configElement);
this.testOption = testOption;
+ this.defaultEditStrategyType = defaultStrategy;
}
boolean shouldTest() {
boolean shouldSet() {
return testOption == TestOption.set || testOption == TestOption.testThenSet;
}
+
+ Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
+ return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
+ }
+
+ ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
+ // TODO cache service registry
+ return new ServiceRegistryWrapper(serviceRegistry);
+ }
+
+ Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
+ return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
+ }
+
+ EditStrategyType getDefaultStrategy() {
+ return defaultEditStrategyType;
+ }
+
+ Services getServices() {
+ return services;
+ }
}
}