Merge "removed config-util from pax-exam minimum bundles as this is not a bundle"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / Config.java
index ca849c71cf8bcc8ca50b543d9b97a0ac79af2e27..f33a32271fd189dd39a61b7403069d765e4d1615 100644 (file)
@@ -15,6 +15,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
+import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
@@ -51,21 +52,24 @@ public class Config {
         this.moduleNamesToConfigs = Collections.unmodifiableMap(moduleNamesToConfigs);
     }
 
-    private Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(Set<ObjectName> instancesToMap,
-            Services serviceTracker) {
+    public static Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(Set<ObjectName> instancesToMap,
+                                                                                Services serviceTracker, Map<String, Map<String, ModuleConfig>> configs) {
         Multimap<String, ObjectName> moduleToInstances = mapInstancesToModules(instancesToMap);
 
         Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
 
-        for (String namespace : moduleConfigs.keySet()) {
+        for (String namespace : configs.keySet()) {
 
             Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
 
-            for (Entry<String, ModuleConfig> mbeEntry : moduleConfigs.get(namespace).entrySet()) {
+            for (Entry<String, ModuleConfig> mbeEntry : configs.get(namespace).entrySet()) {
 
                 String moduleName = mbeEntry.getKey();
                 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
 
+                // TODO, this code does not support same module names from different namespaces
+                // Namespace should be present in ObjectName
+
                 if (instances == null)
                     continue;
 
@@ -85,11 +89,11 @@ public class Config {
         return retVal;
     }
 
-    private void addServices(Services serviceTracker, Collection<ObjectName> instances,
-            Collection<String> providedServices) {
+    private static void addServices(Services serviceTracker, Collection<ObjectName> instances,
+            Multimap<String, String> providedServices) {
         for (ObjectName instanceOn : instances) {
-            for (String serviceName : providedServices) {
-                serviceTracker.addServiceEntry(serviceName, instanceOn);
+            for (Entry<String, String> serviceName : providedServices.entries()) {
+                serviceTracker.addServiceEntry(serviceName.getKey(), serviceName.getValue(), instanceOn);
             }
         }
     }
@@ -114,7 +118,7 @@ public class Config {
         Services serviceTracker = new Services();
 
         Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
-                serviceTracker);
+                serviceTracker, moduleConfigs);
 
         Element root = dataElement;
         if (maybeNamespace.isPresent()) {
@@ -148,6 +152,7 @@ public class Config {
         return root;
     }
 
+    // TODO remove commented modules from output
     private void addEmptyModulesCommented(Document document, Element root, String moduleNamespace,
             Entry<String, Collection<ObjectName>> moduleMappingEntry) {
         Element emptyModule = document.createElement(XmlNetconfConstants.MODULE_KEY);
@@ -162,7 +167,8 @@ public class Config {
     // TODO refactor, replace string representing namespace with namespace class
     // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
     // class
-    public Map<String, Multimap<String, ModuleElementResolved>> fromXml(XmlElement xml, Set<ObjectName> instancesForFillingServiceRefMapping) {
+    public Map<String, Multimap<String, ModuleElementResolved>> fromXml(XmlElement xml, Set<ObjectName> instancesForFillingServiceRefMapping,
+                                                                        EditStrategyType defaultEditStrategyType) {
         Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
 
         List<XmlElement> recognisedChildren = Lists.newArrayList();
@@ -173,7 +179,7 @@ public class Config {
         xml.checkUnrecognisedElements(recognisedChildren);
 
         for (XmlElement moduleElement : moduleElements) {
-            resolveModule(retVal, serviceTracker, moduleElement);
+            resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType);
         }
 
         return retVal;
@@ -194,7 +200,7 @@ public class Config {
     }
 
     private void resolveModule(Map<String, Multimap<String, ModuleElementResolved>> retVal, Services serviceTracker,
-            XmlElement moduleElement) {
+            XmlElement moduleElement, EditStrategyType defaultStrategy) {
         XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
         Entry<String, String> prefixToNamespace = typeElement.findNamespaceOfTextContent();
         String moduleNamespace = prefixToNamespace.getValue();
@@ -213,7 +219,7 @@ public class Config {
         }
 
         ModuleElementResolved moduleElementResolved = moduleMapping.fromXml(moduleElement, serviceTracker,
-                instanceName, moduleNamespace);
+                instanceName, moduleNamespace, defaultStrategy);
 
         innerMap.put(factoryName, moduleElementResolved);
     }
@@ -222,7 +228,7 @@ public class Config {
         Optional<XmlElement> servicesElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SERVICES_KEY,
                 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
 
-        Map<String, Map<String, String>> mappedServices;
+        Map<String, Map<String, Map<String, String>>> mappedServices;
         if (servicesElement.isPresent()) {
             mappedServices = Services.fromXml(servicesElement.get());
             recognisedChildren.add(servicesElement.get());
@@ -240,8 +246,9 @@ public class Config {
 
             checkState(moduleConfig != null, "Cannot find ModuleConfig with name " + factoryName + " in " + moduleNamesToConfigs);
             // Set<String> services = ;
-            for (String serviceName : moduleConfig.getProvidedServices()) {
-                services.addServiceEntry(serviceName, existingON);
+            for (Entry<String, String> serviceName : moduleConfig.getProvidedServices().entries()) {
+
+                services.addServiceEntry(serviceName.getKey(), serviceName.getValue(), existingON);
             }
         }