Merge "bug 1888 - FRM Flow Listener registration fail"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / Config.java
index bb5a67cb9ce9051570b6ea736cc83d5e89cd1977..773e4ee933c147d6206394b8da51fd4f8094358f 100644 (file)
@@ -9,12 +9,17 @@
 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
 
 import static com.google.common.base.Preconditions.checkState;
-import static java.lang.String.format;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
 
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -24,29 +29,19 @@ import javax.management.ObjectName;
 
 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
 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;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Multimap;
 
 public class Config {
-    private final Logger logger = LoggerFactory.getLogger(Config.class);
 
     private final Map<String/* Namespace from yang file */,
             Map<String /* Name of module entry from yang file */, ModuleConfig>> moduleConfigs;
-    private final Map<String, ModuleConfig> moduleNamesToConfigs;
 
     private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
 
@@ -56,11 +51,6 @@ public class Config {
 
     public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
         this.moduleConfigs = moduleConfigs;
-        Map<String, ModuleConfig> moduleNamesToConfigs = new HashMap<>();
-        for (Entry<String, Map<String, ModuleConfig>> entry : moduleConfigs.entrySet()) {
-            moduleNamesToConfigs.putAll(entry.getValue());
-        }
-        this.moduleNamesToConfigs = Collections.unmodifiableMap(moduleNamesToConfigs);
         this.identityMap = identityMap;
     }
 
@@ -70,11 +60,11 @@ public class Config {
 
         Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
 
-        for (String namespace : configs.keySet()) {
+        for (Entry<String, Map<String, ModuleConfig>> namespaceToModuleToConfigEntry : configs.entrySet()) {
 
             Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
 
-            for (Entry<String, ModuleConfig> mbeEntry : configs.get(namespace).entrySet()) {
+            for (Entry<String, ModuleConfig> mbeEntry : namespaceToModuleToConfigEntry.getValue().entrySet()) {
 
                 String moduleName = mbeEntry.getKey();
                 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
@@ -82,14 +72,15 @@ public class Config {
                 // TODO, this code does not support same module names from different namespaces
                 // Namespace should be present in ObjectName
 
-                if (instances == null)
+                if (instances == null){
                     continue;
+                }
 
                 innerRetVal.put(moduleName, instances);
 
             }
 
-            retVal.put(namespace, innerRetVal);
+            retVal.put(namespaceToModuleToConfigEntry.getKey(), innerRetVal);
         }
         return retVal;
     }
@@ -104,44 +95,38 @@ public class Config {
         return retVal;
     }
 
-    // public Element toXml(Set<ObjectName> instancesToMap, String namespace,
-    // Document document) {
-    // return toXml(instancesToMap, Optional.of(namespace), document);
-    // }
-
     public Element toXml(Set<ObjectName> instancesToMap, Optional<String> maybeNamespace, Document document,
             Element dataElement, ServiceRegistryWrapper serviceTracker) {
 
         Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
                 moduleConfigs);
 
-        Element root = dataElement;
         if (maybeNamespace.isPresent()) {
-            root.setAttributeNS(maybeNamespace.get(), dataElement.getNodeName(), "xmlns");
+            dataElement.setAttributeNS(maybeNamespace.get(), dataElement.getNodeName(), "xmlns");
         }
 
         Element modulesElement = XmlUtil.createElement(document, XmlNetconfConstants.MODULES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
-        root.appendChild(modulesElement);
-        for (String moduleNamespace : moduleToInstances.keySet()) {
-            for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstances.get(moduleNamespace)
+        dataElement.appendChild(modulesElement);
+        for (Entry<String, Map<String, Collection<ObjectName>>> moduleToInstanceEntry : moduleToInstances.entrySet()) {
+            for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstanceEntry.getValue()
                     .entrySet()) {
 
-                ModuleConfig mapping = moduleConfigs.get(moduleNamespace).get(moduleMappingEntry.getKey());
+                ModuleConfig mapping = moduleConfigs.get(moduleToInstanceEntry.getKey()).get(moduleMappingEntry.getKey());
 
                 if (moduleMappingEntry.getValue().isEmpty()) {
                     continue;
                 }
 
                 for (ObjectName objectName : moduleMappingEntry.getValue()) {
-                    modulesElement.appendChild(mapping.toXml(objectName, serviceTracker, document, moduleNamespace));
+                    modulesElement.appendChild(mapping.toXml(objectName, document, moduleToInstanceEntry.getKey()));
                 }
 
             }
         }
 
-        root.appendChild(Services.toXml(serviceTracker, document));
+        dataElement.appendChild(Services.toXml(serviceTracker, document));
 
-        return root;
+        return dataElement;
     }
 
     // TODO refactor, replace string representing namespace with namespace class
@@ -264,10 +249,12 @@ public class Config {
         Optional<XmlElement> modulesOpt = getModulesElement(parent);
 
         List<XmlElement> recognised = Lists.newArrayList();
-        if(servicesOpt.isPresent())
+        if(servicesOpt.isPresent()){
             recognised.add(servicesOpt.get());
-        if(modulesOpt.isPresent())
+        }
+        if(modulesOpt.isPresent()){
             recognised.add(modulesOpt.get());
+        }
 
         parent.checkUnrecognisedElements(recognised);
     }
@@ -275,7 +262,7 @@ public class Config {
     private String getFactoryName(String factoryNameWithPrefix, String prefixOrEmptyString) {
         checkState(
                 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
-                format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
+                String.format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
                         factoryNameWithPrefix, prefixOrEmptyString));
 
         int factoryNameAfterPrefixIndex;