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.Verify.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.List;
15 import java.util.function.BiFunction;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
24 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
25 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
26 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
27 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
28 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
29 import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
52 * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
53 * a particular {@link EffectiveModelContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
56 public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
57 static final class Lhotka02 extends JSONCodecFactory {
58 Lhotka02(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
59 super(context, cache, JSONInstanceIdentifierCodec.Lhotka02::new);
63 Lhotka02 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
64 return new Lhotka02(newSchemaContext, newCache);
68 JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
69 return new NumberJSONCodec<>(decimalCodec);
73 JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
74 return new NumberJSONCodec<>(integerCodec);
78 static final class RFC7951 extends JSONCodecFactory {
79 RFC7951(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
80 super(context, cache, JSONInstanceIdentifierCodec.RFC7951::new);
84 RFC7951 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
85 return new RFC7951(newSchemaContext, newCache);
89 JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
90 return new QuotedJSONCodec<>(decimalCodec);
94 JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
95 return new QuotedJSONCodec<>(integerCodec);
99 private final @NonNull JSONInstanceIdentifierCodec iidCodec;
101 @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
102 justification = "https://github.com/spotbugs/spotbugs/issues/1867")
103 private JSONCodecFactory(final @NonNull EffectiveModelContext context,
104 final @NonNull CodecCache<JSONCodec<?>> cache,
105 final BiFunction<EffectiveModelContext, JSONCodecFactory, @NonNull JSONInstanceIdentifierCodec> iidCodec) {
106 super(context, cache);
107 this.iidCodec = verifyNotNull(iidCodec.apply(context, this));
111 protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
112 return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
116 protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
117 return new BooleanJSONCodec(BooleanStringCodec.from(type));
121 protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
122 return new QuotedJSONCodec<>(BitsStringCodec.from(type));
126 protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
127 return wrapDecimalCodec(DecimalStringCodec.from(type));
131 protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
132 return EmptyJSONCodec.INSTANCE;
136 protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
137 return new QuotedJSONCodec<>(EnumStringCodec.from(type));
141 protected final JSONCodec<QName> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
142 return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
146 protected final JSONCodec<YangInstanceIdentifier> instanceIdentifierCodec(
147 final InstanceIdentifierTypeDefinition type) {
152 protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
153 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
157 protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
158 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
162 protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
163 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
167 protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
168 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
172 protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
173 return new QuotedJSONCodec<>(StringStringCodec.from(type));
177 protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
178 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
182 protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
183 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
187 protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
188 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
192 protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
193 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
197 protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
198 return UnionJSONCodec.create(type, codecs);
202 protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
203 return NullJSONCodec.INSTANCE;
206 // Returns a one-off factory for the purposes of normalizing an anydata tree.
208 // FIXME: 7.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
209 // whether our cache is thread-safe -- in which case we should just return this.
210 // The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
211 // to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
213 // The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
214 // factory due to that potentially creating a circular reference.
215 final JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext) {
216 return rebaseTo(newSchemaContext, new LazyCodecCache<>());
219 abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
221 abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
223 abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);