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