BUG-3675 Support non-prefixed identityrefs in config subsystem 78/22378/2
authorMaros Marsalek <mmarsale@cisco.com>
Thu, 11 Jun 2015 14:39:29 +0000 (16:39 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 15 Jun 2015 07:46:48 +0000 (07:46 +0000)
If a non prefixed but valid identityref was submitted as e.g. service type,
netconf connector for config subsystem failed.

Change-Id: I59dbb7dc83da9558db06118bdf1296f1ab9d782f
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/ObjectNameAttributeReadingStrategy.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleIdentityRefAttributeReadingStrategy.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/operations/editconfig/EditConfig.java
opendaylight/netconf/config-netconf-connector/src/test/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleIdentityRefAttributeReadingStrategyTest.java [new file with mode: 0644]

index f85fe29..709c8d2 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
 
 import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
 import java.util.List;
 import java.util.Map;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
@@ -50,15 +49,18 @@ public class ObjectNameAttributeReadingStrategy extends AbstractAttributeReading
 
     public static String checkPrefixAndExtractServiceName(XmlElement typeElement, Map.Entry<String, String> prefixNamespace) throws NetconfDocumentedException {
         String serviceName = typeElement.getTextContent();
-        // FIXME: comparing Entry with String:
-        Preconditions.checkState(!Strings.isNullOrEmpty(prefixNamespace.getKey()), "Service %s value not prefixed with namespace",
+        Preconditions.checkNotNull(prefixNamespace.getKey(), "Service %s value cannot be linked to namespace",
                 XmlNetconfConstants.TYPE_KEY);
-        String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
-        Preconditions.checkState(serviceName.startsWith(prefix),
-                "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
-                serviceName);
-        serviceName = serviceName.substring(prefix.length());
-        return serviceName;
+        if(prefixNamespace.getKey().isEmpty()) {
+            return serviceName;
+        } else {
+            String prefix = prefixNamespace.getKey() + PREFIX_SEPARATOR;
+            Preconditions.checkState(serviceName.startsWith(prefix),
+                    "Service %s not correctly prefixed, expected %s, but was %s", XmlNetconfConstants.TYPE_KEY, prefix,
+                    serviceName);
+            serviceName = serviceName.substring(prefix.length());
+            return serviceName;
+        }
     }
 
 }
index 23e67b3..6d702ef 100644 (file)
@@ -33,15 +33,20 @@ public class SimpleIdentityRefAttributeReadingStrategy extends SimpleAttributeRe
 
     @Override
     protected String readElementContent(XmlElement xmlElement) throws NetconfDocumentedException {
-        // TODO test
         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);
index ab2a081..b7699bf 100644 (file)
@@ -222,7 +222,7 @@ public class EditConfig extends AbstractConfigNetconfOperation {
     public static class IdentityMapping {
         private final Map<String, IdentitySchemaNode> identityNameToSchemaNode;
 
-        IdentityMapping() {
+        public IdentityMapping() {
             this.identityNameToSchemaNode = Maps.newHashMap();
         }
 
diff --git a/opendaylight/netconf/config-netconf-connector/src/test/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleIdentityRefAttributeReadingStrategyTest.java b/opendaylight/netconf/config-netconf-connector/src/test/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/fromxml/SimpleIdentityRefAttributeReadingStrategyTest.java
new file mode 100644 (file)
index 0000000..ef35ce3
--- /dev/null
@@ -0,0 +1,34 @@
+package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.Maps;
+import java.net.URI;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Map;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
+import org.opendaylight.controller.netconf.util.xml.XmlElement;
+
+public class SimpleIdentityRefAttributeReadingStrategyTest {
+
+    @Test
+    public void testReadIdRef() throws Exception {
+        final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMapping = Maps.newHashMap();
+        final EditConfig.IdentityMapping value = new EditConfig.IdentityMapping();
+        final Date rev = new Date();
+        identityMapping.put("namespace", Collections.singletonMap(rev, value));
+        identityMapping.put("inner", Collections.singletonMap(rev, value));
+        final SimpleIdentityRefAttributeReadingStrategy key = new SimpleIdentityRefAttributeReadingStrategy(null, "key", identityMapping);
+
+        String read = key.readElementContent(XmlElement.fromString("<el xmlns=\"namespace\">local</el>"));
+        assertEquals(org.opendaylight.yangtools.yang.common.QName.create(URI.create("namespace"), rev, "local").toString(), read);
+
+        read = key.readElementContent(XmlElement.fromString("<el xmlns:a=\"inner\" xmlns=\"namespace\">a:local</el>"));
+        assertEquals(org.opendaylight.yangtools.yang.common.QName.create(URI.create("inner"), rev, "local").toString(), read);
+
+        read = key.readElementContent(XmlElement.fromString("<top xmlns=\"namespace\"><el>local</el></top>").getOnlyChildElement());
+        assertEquals(org.opendaylight.yangtools.yang.common.QName.create(URI.create("namespace"), rev, "local").toString(), read);
+    }
+}
\ No newline at end of file

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.