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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.List;
14 import java.util.function.BiFunction;
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.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
24 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
25 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
26 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
27 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
28 import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
51 * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
52 * a particular {@link EffectiveModelContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
54 public abstract sealed class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
55 static final class Lhotka02 extends JSONCodecFactory {
56 Lhotka02(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
57 super(context, cache, JSONInstanceIdentifierCodec.Lhotka02::new);
61 Lhotka02 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
62 return new Lhotka02(newSchemaContext, newCache);
66 JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
67 return new NumberJSONCodec<>(decimalCodec);
71 JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
72 return new NumberJSONCodec<>(integerCodec);
76 static final class RFC7951 extends JSONCodecFactory {
77 RFC7951(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
78 super(context, cache, JSONInstanceIdentifierCodec.RFC7951::new);
82 RFC7951 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
83 return new RFC7951(newSchemaContext, newCache);
87 JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
88 return new QuotedJSONCodec<>(decimalCodec);
92 JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
93 return new QuotedJSONCodec<>(integerCodec);
97 private final @NonNull JSONInstanceIdentifierCodec iidCodec;
99 @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
100 justification = "https://github.com/spotbugs/spotbugs/issues/1867")
101 private JSONCodecFactory(final @NonNull EffectiveModelContext context,
102 final @NonNull CodecCache<JSONCodec<?>> cache,
103 final BiFunction<EffectiveModelContext, JSONCodecFactory, @NonNull JSONInstanceIdentifierCodec> iidCodec) {
104 super(context, cache);
105 this.iidCodec = verifyNotNull(iidCodec.apply(context, this));
109 protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
110 return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
114 protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
115 return new BooleanJSONCodec(BooleanStringCodec.from(type));
119 protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
120 return new QuotedJSONCodec<>(BitsStringCodec.from(type));
124 protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
125 return wrapDecimalCodec(DecimalStringCodec.from(type));
129 protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
130 return EmptyJSONCodec.INSTANCE;
134 protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
135 return new QuotedJSONCodec<>(EnumStringCodec.from(type));
139 protected final JSONCodec<QName> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
140 return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
144 protected final JSONCodec<YangInstanceIdentifier> instanceIdentifierCodec(
145 final InstanceIdentifierTypeDefinition type) {
150 protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
151 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
155 protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
156 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
160 protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
161 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
165 protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
166 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
170 protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
171 return new QuotedJSONCodec<>(StringStringCodec.from(type));
175 protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
176 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
180 protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
181 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
185 protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
186 return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
190 protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
191 return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
195 protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
196 return UnionJSONCodec.create(type, codecs);
200 protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
201 return NullJSONCodec.INSTANCE;
204 // Returns a one-off factory for the purposes of normalizing an anydata tree.
206 // 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
207 // whether our cache is thread-safe -- in which case we should just return this.
208 // The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
209 // to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
211 // The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
212 // factory due to that potentially creating a circular reference.
213 final JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext) {
214 return rebaseTo(newSchemaContext, new LazyCodecCache<>());
217 abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
219 abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
221 abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);