Remove @Beta from yang-data-codec-gson
[yangtools.git] / codec / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONCodecFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
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;
49
50 /**
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.
53  */
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);
58         }
59
60         @Override
61         Lhotka02 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
62             return new Lhotka02(newSchemaContext, newCache);
63         }
64
65         @Override
66         JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
67             return new NumberJSONCodec<>(decimalCodec);
68         }
69
70         @Override
71         JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
72             return new NumberJSONCodec<>(integerCodec);
73         }
74     }
75
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);
79         }
80
81         @Override
82         RFC7951 rebaseTo(final EffectiveModelContext newSchemaContext, final CodecCache<JSONCodec<?>> newCache) {
83             return new RFC7951(newSchemaContext, newCache);
84         }
85
86         @Override
87         JSONCodec<?> wrapDecimalCodec(final DecimalStringCodec decimalCodec) {
88             return new QuotedJSONCodec<>(decimalCodec);
89         }
90
91         @Override
92         JSONCodec<?> wrapIntegerCodec(final AbstractIntegerStringCodec<?, ?> integerCodec) {
93             return new QuotedJSONCodec<>(integerCodec);
94         }
95     }
96
97     private final @NonNull JSONInstanceIdentifierCodec iidCodec;
98
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));
106     }
107
108     @Override
109     protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
110         return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
111     }
112
113     @Override
114     protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
115         return new BooleanJSONCodec(BooleanStringCodec.from(type));
116     }
117
118     @Override
119     protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
120         return new QuotedJSONCodec<>(BitsStringCodec.from(type));
121     }
122
123     @Override
124     protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
125         return wrapDecimalCodec(DecimalStringCodec.from(type));
126     }
127
128     @Override
129     protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
130         return EmptyJSONCodec.INSTANCE;
131     }
132
133     @Override
134     protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
135         return new QuotedJSONCodec<>(EnumStringCodec.from(type));
136     }
137
138     @Override
139     protected final JSONCodec<QName> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
140         return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
141     }
142
143     @Override
144     protected final JSONCodec<YangInstanceIdentifier> instanceIdentifierCodec(
145             final InstanceIdentifierTypeDefinition type) {
146         return iidCodec;
147     }
148
149     @Override
150     protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
151         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
152     }
153
154     @Override
155     protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
156         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
157     }
158
159     @Override
160     protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
161         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
162     }
163
164     @Override
165     protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
166         return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
167     }
168
169     @Override
170     protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
171         return new QuotedJSONCodec<>(StringStringCodec.from(type));
172     }
173
174     @Override
175     protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
176         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
177     }
178
179     @Override
180     protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
181         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
182     }
183
184     @Override
185     protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
186         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
187     }
188
189     @Override
190     protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
191         return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
192     }
193
194     @Override
195     protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
196         return UnionJSONCodec.create(type, codecs);
197     }
198
199     @Override
200     protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
201         return NullJSONCodec.INSTANCE;
202     }
203
204     // Returns a one-off factory for the purposes of normalizing an anydata tree.
205     //
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.
210     //
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<>());
215     }
216
217     abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
218
219     abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
220
221     abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);
222 }