Validate parsed QName to identity
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractModuleStringIdentityrefCodec.java
index dc04682699e3967cba8cbce1e59783159a74ae64..d2c373eea1f4e57be03f166eb1c5236486fa3773 100644 (file)
@@ -7,12 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.data.util;
 
-import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
 
+import com.google.common.annotations.Beta;
 import javax.annotation.Nonnull;
-
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 
 @Beta
@@ -20,15 +20,23 @@ public abstract class AbstractModuleStringIdentityrefCodec extends AbstractStrin
     /**
      * Resolve a string prefix into the corresponding module.
      *
-     * @param prefix
+     * @param prefix Prefix
      * @return module mapped to prefix, or null if the module cannot be resolved
      */
     protected abstract Module moduleForPrefix(@Nonnull String prefix);
 
     @Override
-    protected final QName createQName(final String prefix, final String localName) {
+    protected final QName createQName(@Nonnull final String prefix, @Nonnull final String localName) {
         final Module module = moduleForPrefix(prefix);
-        Preconditions.checkArgument(module != null, "Failed to lookup prefix %s", prefix);
-        return QName.create(module.getQNameModule(), localName);
+        checkArgument(module != null, "Failed to lookup prefix %s", prefix);
+
+        final QName qname = QName.create(module.getQNameModule(), localName);
+        for (IdentitySchemaNode identity : module.getIdentities()) {
+            if (qname.equals(identity.getQName())) {
+                return identity.getQName();
+            }
+        }
+
+        throw new IllegalArgumentException("Failed to find identity matching " + qname);
     }
 }