*/
package org.opendaylight.yangtools.yang.data.codec.gson;
-import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import com.google.gson.stream.JsonWriter;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
this.context = requireNonNull(context);
- this.dataContextTree = DataSchemaContextTree.from(context);
- this.codecFactory = requireNonNull(jsonCodecFactory);
+ dataContextTree = DataSchemaContextTree.from(context);
+ codecFactory = requireNonNull(jsonCodecFactory);
}
@Override
protected final Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
final String value) {
requireNonNull(schemaNode, "schemaNode cannot be null");
- checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
- final JSONCodec<?> objectJSONCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver);
- return objectJSONCodec.parseValue(null, value);
+ if (schemaNode instanceof LeafSchemaNode) {
+ return codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver).parseValue(null, value);
+ } else if (schemaNode instanceof LeafListSchemaNode) {
+ return codecFactory.codecFor((LeafListSchemaNode) schemaNode, resolver).parseValue(null, value);
+ }
+ throw new IllegalArgumentException("schemaNode " + schemaNode
+ + " must be of type LeafSchemaNode or LeafListSchemaNode");
}
@Override
}
@Test
- @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
void testSerializeIdentityRefSame() throws Exception {
assertSerdes("/foo:bar[qname='one']", buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, FOO_ONE));
}
@Test
- @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
void testSerializeIdentityRefOther() throws Exception {
// No escaping is needed, use double quotes and escape
assertSerdes("/foo:bar[qname='bar:two']", buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO));
}
@Test
- @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
void testSerializeIdentityValue() throws Exception {
assertSerdes("/bar:foo[.='foo:one']", buildYangInstanceIdentifier(BAR_FOO, FOO_ONE));
assertSerdes("/bar:foo[.='two']", buildYangInstanceIdentifier(BAR_FOO, BAR_TWO));
*/
package org.opendaylight.yangtools.yang.data.codec.xml;
-import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import java.util.ArrayDeque;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
XmlStringInstanceIdentifierCodec(final EffectiveModelContext context, final XmlCodecFactory xmlCodecFactory) {
this.context = requireNonNull(context);
- this.dataContextTree = DataSchemaContextTree.from(context);
- this.codecFactory = requireNonNull(xmlCodecFactory);
+ dataContextTree = DataSchemaContextTree.from(context);
+ codecFactory = requireNonNull(xmlCodecFactory);
}
@Override
protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
final String value) {
requireNonNull(schemaNode, "schemaNode cannot be null");
- checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
- final XmlCodec<?> objectXmlCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver);
+ final XmlCodec<?> objectXmlCodec;
+ if (schemaNode instanceof LeafSchemaNode) {
+ objectXmlCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver);
+ } else if (schemaNode instanceof LeafListSchemaNode) {
+ objectXmlCodec = codecFactory.codecFor((LeafListSchemaNode) schemaNode, resolver);
+ } else {
+ throw new IllegalArgumentException("schemaNode " + schemaNode
+ + " must be of type LeafSchemaNode or LeafListSchemaNode");
+ }
return objectXmlCodec.parseValue(getNamespaceContext(), value);
}
}
@Test
- @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
public void testSerializeIdentity() throws Exception {
assertSerdes("/foo:bar[foo:qname='foo:one']", buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, FOO_ONE));
assertSerdes("/foo:bar[foo:qname='bar:two']", buildYangInstanceIdentifier(FOO_BAR, FOO_QNAME, BAR_TWO));
}
@Test
- @Disabled("YT-1473: QName values need to be recognized and properly encoded via identity codec")
public void testSerializeIdentityValue() throws Exception {
assertSerdes("/bar:foo[.='foo:one']", buildYangInstanceIdentifier(BAR_FOO, FOO_ONE));
assertSerdes("/bar:foo[.='bar:two']", buildYangInstanceIdentifier(BAR_FOO, BAR_TWO));
return sb.toString();
}
- private static StringBuilder appendValue(final StringBuilder sb, final QNameModule currentModule,
+ private StringBuilder appendValue(final StringBuilder sb, final QNameModule currentModule,
final Object value) {
+ if (value instanceof QName) {
+ // QName implies identity-ref, which can never be escaped
+ return appendQName(sb.append('\''), (QName) value, currentModule).append('\'');
+ }
+
final var str = String.valueOf(value);
// We have two specifications here: Section 6.1.3 of both RFC6020 and RFC7950:
// Break-out from method for leaf-list case
if (key == null && currentNode.isLeaf()) {
checkValid(offset == data.length(), "Leaf argument must be last argument of instance identifier.");
- return new NodeWithValue<>(name, keyValue);
+ final Object value = codec.deserializeKeyValue(currentNode.getDataSchemaNode(),
+ type -> resolveLeafref(currentNode.getIdentifier().getNodeType(), type), keyValue);
+ return new NodeWithValue<>(name, value);
}
final DataSchemaContextNode<?> keyNode = currentNode.getChild(key);
checkValid(keyNode != null, "%s is not correct schema node identifier.", key);