import java.io.IOException;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.util.codec.IdentityCodecUtil;
import org.opendaylight.yangtools.yang.data.util.codec.QNameCodecUtil;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@Override
public QName parseValue(final Object ctx, final String value) {
- return QNameCodecUtil.decodeQName(value, prefix -> {
+ return IdentityCodecUtil.parseIdentity(value, schemaContext, prefix -> {
if (prefix.isEmpty()) {
return parentModule;
}
final Module module = schemaContext.findModuleByName(prefix, null);
Preconditions.checkArgument(module != null, "Could not find module %s", prefix);
return module.getQNameModule();
- });
+ }).getQName();
}
/**
assertEquals("lf15_21 value", lf15_21.getAsString());
assertEquals("lf13 value", lf13.getAsString());
assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
- assertEquals("complexjson:lf11", lf15_12.getAsString());
+ assertEquals("complexjson:ident", lf15_12.getAsString());
}).validate(jsonOutput);
}
assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
assertEquals("lf13 value", lf13.getAsString());
assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
- assertEquals("complexjson:lf11", lf15_12.getAsString());
+ assertEquals("complexjson:ident", lf15_12.getAsString());
}).validate(jsonOutput);
}
private static LeafNode<Object> lf15_12Node() {
return Builders.leafBuilder()
.withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf15_12")))
- .withValue(QName.create("ns:complex:json", "2014-08-11", "lf11")).build();
+ .withValue(QName.create("ns:complex:json", "2014-08-11", "ident")).build();
}
private static LeafNode<Object> lf15_11Node() {
"lf15_11" : "one two",
"lf13" : "lf13 value",
"lf15_21" : "lf15_21 value",
- "lf15_12" : "complexjson:lf11"
+ "lf15_12" : "complexjson:ident"
}
}
"lf15_11" : "one two",
"lf13" : "lf13 value",
"complexjson-augmentation:lf15_12" : "lf15_12 value from augmentation",
- "lf15_12" : "complexjson:lf11"
+ "lf15_12" : "complexjson:ident"
}
}
"complexjson-augmentation:lf15_11" : "lf15_11 value from augmentation",
"complexjson-augmentation:lf15_12" : "lf15_12 value from augmentation",
"lf15_11" : "one two",
- "lf15_12" : "complexjson:lf11",
+ "lf15_12" : "complexjson:ident",
"lf15_21" : "lf15_21 value",
"lf17" : "lf17 value",
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.yangtools.yang.data.codec.xml;
import com.google.common.base.Preconditions;
import javax.xml.stream.XMLStreamWriter;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.util.codec.IdentityCodecUtil;
import org.opendaylight.yangtools.yang.data.util.codec.QNameCodecUtil;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@Override
public QName parseValue(final NamespaceContext ctx, final String str) {
- return QNameCodecUtil.decodeQName(str, prefix -> {
+ return IdentityCodecUtil.parseIdentity(str, schemaContext, prefix -> {
if (prefix.isEmpty()) {
return parentModule;
}
final Module module = schemaContext.findModuleByNamespaceAndRevision(URI.create(prefixedNS), null);
Preconditions.checkArgument(module != null, "Could not find module for namespace %s", prefixedNS);
return module.getQNameModule();
- });
+ }).getQName();
}
@Override
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.util.codec;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import com.google.common.annotations.Beta;
+import java.util.function.Function;
+import javax.annotation.concurrent.ThreadSafe;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * Utility methods for implementing string-to-identity codecs.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@NonNullByDefault
+@ThreadSafe
+public final class IdentityCodecUtil {
+
+ private IdentityCodecUtil() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Parse a string into a QName using specified prefix-to-QNameModule mapping function, interpreting the result
+ * as an IdentitySchemaNode existing in specified SchemaContext.
+ *
+ * @param value string value to parse
+ * @param schemaContext Parent schema context
+ * @param prefixToModule prefix-to-QNameModule mapping function
+ * @return Corresponding IdentitySchemaNode.
+ * @throws IllegalArgumentException if the value is invalid or does not refer to an existing identity
+ */
+ public static IdentitySchemaNode parseIdentity(final String value, final SchemaContext schemaContext,
+ final Function<String, QNameModule> prefixToModule) {
+ final QName qname = QNameCodecUtil.decodeQName(value, prefixToModule);
+ final Module module = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
+ qname.getRevision());
+ checkState(module != null, "Parsed QName %s refers to a non-existent module", qname);
+
+ for (IdentitySchemaNode identity : module.getIdentities()) {
+ if (qname.equals(identity.getQName())) {
+ return identity;
+ }
+ }
+
+ throw new IllegalArgumentException("Parsed QName " + qname + " does not refer to a valid identity");
+ }
+}
--- /dev/null
+module yangtools846 {
+ namespace "yangtools846";
+ prefix yt846;
+
+ identity foo;
+}
+