2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.codec.gson;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.gson.stream.JsonWriter;
14 import java.io.IOException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.util.AbstractStringInstanceIdentifierCodec;
21 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
28 abstract sealed class JSONInstanceIdentifierCodec extends AbstractStringInstanceIdentifierCodec
29 implements JSONCodec<YangInstanceIdentifier> {
30 static final class Lhotka02 extends JSONInstanceIdentifierCodec {
31 Lhotka02(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
32 super(context, jsonCodecFactory);
36 static final class RFC7951 extends JSONInstanceIdentifierCodec {
37 RFC7951(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
38 super(context, jsonCodecFactory);
42 protected StringBuilder appendQName(final StringBuilder sb, final QName qname, final QNameModule lastModule) {
43 return qname.getModule().equals(lastModule) ? sb.append(qname.getLocalName())
44 : super.appendQName(sb, qname, lastModule);
48 protected QName createQName(final QNameModule lastModule, final String localName) {
49 checkArgument(lastModule != null, "Unprefixed leading name %s", localName);
50 return QName.create(lastModule, localName);
54 private final @NonNull DataSchemaContextTree dataContextTree;
55 private final JSONCodecFactory codecFactory;
56 private final EffectiveModelContext context;
58 JSONInstanceIdentifierCodec(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
59 this.context = requireNonNull(context);
60 dataContextTree = DataSchemaContextTree.from(context);
61 codecFactory = requireNonNull(jsonCodecFactory);
65 protected final QNameModule moduleForPrefix(final String prefix) {
66 final var modules = context.findModuleStatements(prefix).iterator();
67 return modules.hasNext() ? modules.next().localQNameModule() : null;
71 protected final String prefixForNamespace(final XMLNamespace namespace) {
72 final var modules = context.findModuleStatements(namespace).iterator();
73 return modules.hasNext() ? modules.next().argument().getLocalName() : null;
77 protected final DataSchemaContextTree getDataContextTree() {
78 return dataContextTree;
82 protected final Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
84 requireNonNull(schemaNode, "schemaNode cannot be null");
85 if (schemaNode instanceof LeafSchemaNode leafSchemaNode) {
86 return codecFactory.codecFor(leafSchemaNode, resolver).parseValue(null, value);
87 } else if (schemaNode instanceof LeafListSchemaNode leafListSchemaNode) {
88 return codecFactory.codecFor(leafListSchemaNode, resolver).parseValue(null, value);
90 throw new IllegalArgumentException("schemaNode " + schemaNode
91 + " must be of type LeafSchemaNode or LeafListSchemaNode");
95 public final Class<YangInstanceIdentifier> getDataType() {
96 return YangInstanceIdentifier.class;
100 public final YangInstanceIdentifier parseValue(final Object ctx, final String str) {
101 return deserialize(str);
105 public final void writeValue(final JsonWriter ctx, final YangInstanceIdentifier value) throws IOException {
108 str = serialize(value);
109 } catch (IllegalArgumentException e) {
110 throw new IOException("Failed to encode instance-identifier", e);