1aa7192e6b0328e1dcf493407d0f00798a921b77
[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 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.QNameModule;
18 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
19 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
24 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
25 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
26 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
27 import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
48
49 /**
50  * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
51  * a particular {@link EffectiveModelContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
52  */
53 @Beta
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<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
140         return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
141     }
142
143     @Override
144     protected final JSONCodec<?> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
145         return iidCodec;
146     }
147
148     @Override
149     protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
150         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
151     }
152
153     @Override
154     protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
155         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
156     }
157
158     @Override
159     protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
160         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
161     }
162
163     @Override
164     protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
165         return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
166     }
167
168     @Override
169     protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
170         return new QuotedJSONCodec<>(StringStringCodec.from(type));
171     }
172
173     @Override
174     protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
175         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
176     }
177
178     @Override
179     protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
180         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
181     }
182
183     @Override
184     protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
185         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
186     }
187
188     @Override
189     protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
190         return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
191     }
192
193     @Override
194     protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
195         return UnionJSONCodec.create(type, codecs);
196     }
197
198     @Override
199     protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
200         return NullJSONCodec.INSTANCE;
201     }
202
203     // Returns a one-off factory for the purposes of normalizing an anydata tree.
204     //
205     // 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
206     //               whether our cache is thread-safe -- in which case we should just return this.
207     //               The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
208     //               to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
209     //
210     //               The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
211     //               factory due to that potentially creating a circular reference.
212     final JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext) {
213         return rebaseTo(newSchemaContext, new LazyCodecCache<>());
214     }
215
216     abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
217
218     abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
219
220     abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);
221 }