Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / EditConfigXmlParser.java
index 99c0a2ff0d719865b7d39cc22143e563ce99c0fc..3995a0a5a4b364e5099185d7122ce87b225fcfb1 100644 (file)
@@ -8,23 +8,15 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig;
 
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
-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.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.config.facade.xml.ConfigExecution;
+import org.opendaylight.controller.config.facade.xml.Datastore;
+import org.opendaylight.controller.config.facade.xml.TestOption;
+import org.opendaylight.controller.config.facade.xml.mapping.config.Config;
+import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
+import org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.XmlElement;
 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.util.exception.MissingNameSpaceException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
-import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,8 +34,8 @@ public class EditConfigXmlParser {
     public EditConfigXmlParser() {
     }
 
-    EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
-            throws NetconfDocumentedException {
+    ConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
+            throws DocumentedException {
 
         //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
 
@@ -55,26 +47,22 @@ public class EditConfigXmlParser {
 
         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);
-        }
+        targetElement  = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
+        targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
+
         String datastoreValue = targetChildNode.getName();
         Datastore targetDatastore = Datastore.valueOf(datastoreValue);
         LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
 
         // check target
         if (targetDatastore != Datastore.candidate){
-            throw new NetconfDocumentedException(String.format(
+            throw new DocumentedException(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);
+                    DocumentedException.ErrorType.application,
+                    DocumentedException.ErrorTag.invalid_value,
+                    DocumentedException.ErrorSeverity.error);
         }
 
         // Test option
@@ -83,9 +71,9 @@ public class EditConfigXmlParser {
                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
         if (testOptionElementOpt.isPresent()) {
             String testOptionValue = testOptionElementOpt.get().getTextContent();
-            testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
+            testOption = TestOption.getFromXmlName(testOptionValue);
         } else {
-            testOption = EditConfigXmlParser.TestOption.getDefault();
+            testOption = TestOption.getDefault();
         }
         LOG.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
 
@@ -110,85 +98,8 @@ public class EditConfigXmlParser {
         }
 
         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);
-    }
-
-    @VisibleForTesting
-    static enum TestOption {
-        testOnly, set, testThenSet;
-
-        static TestOption getFromXmlName(String testOptionXmlName) {
-            switch (testOptionXmlName) {
-            case "test-only":
-                return testOnly;
-            case "test-then-set":
-                return testThenSet;
-            case "set":
-                return set;
-            default:
-                throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
-                        + Arrays.toString(TestOption.values()));
-            }
-        }
-
-        public static TestOption getDefault() {
-            return testThenSet;
-        }
-
-    }
-
-    @VisibleForTesting
-    static class EditConfigExecution {
-
-        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;
-        }
+        configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
 
-        boolean shouldTest() {
-            return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
-        }
-
-        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;
-        }
+        return new ConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
     }
 }