BUG-3675 Support non-prefixed identityrefs in config subsystem
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / fromxml / SimpleIdentityRefAttributeReadingStrategy.java
index 120559712552d4a963c8a90c411e99c9f4b0b96a..6d702ef59fa675c7c624d3d456f9b46a60476109 100644 (file)
@@ -8,13 +8,13 @@
 
 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
 import java.net.URI;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -32,30 +32,35 @@ public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeRe
     }
 
     @Override
-    protected String readElementContent(XmlElement xmlElement) {
-        // TODO test
+    protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
         Map.Entry<String, String> namespaceOfTextContent = xmlElement.findNamespaceOfTextContent();
         String content = xmlElement.getTextContent();
 
-        String prefix = namespaceOfTextContent.getKey() + ":";
-        Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed");
-
-        String localName = content.substring(prefix.length());
-        String namespace = namespaceOfTextContent.getValue();
+        final String namespace;
+        final String localName;
+        if(namespaceOfTextContent.getKey().isEmpty()) {
+            localName = content;
+            namespace = xmlElement.getNamespace();
+        } else {
+            String prefix = namespaceOfTextContent.getKey() + ":";
+            Preconditions.checkArgument(content.startsWith(prefix), "Identity ref should be prefixed with \"%s\"", prefix);
+            localName = content.substring(prefix.length());
+            namespace = namespaceOfTextContent.getValue();
+        }
 
         Date revision = null;
         Map<Date, EditConfig.IdentityMapping> revisions = identityMap.get(namespace);
         if(revisions.keySet().size() > 1) {
-            for (Date date : revisions.keySet()) {
-                if(revisions.get(date).containsIdName(localName)) {
+            for (Map.Entry<Date, EditConfig.IdentityMapping> revisionToIdentityEntry : revisions.entrySet()) {
+                if(revisionToIdentityEntry.getValue().containsIdName(localName)) {
                     Preconditions.checkState(revision == null, "Duplicate identity %s, in namespace %s, with revisions: %s, %s detected. Cannot map attribute",
-                            localName, namespace, revision, date);
-                    revision = date;
+                            localName, namespace, revision, revisionToIdentityEntry.getKey());
+                    revision = revisionToIdentityEntry.getKey();
                 }
             }
-        } else
+        } else {
             revision = revisions.keySet().iterator().next();
-
+        }
 
         return QName.create(URI.create(namespace), revision, localName).toString();
     }