Mass-migrate to use EffectiveModelContext
[yangtools.git] / yang / 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 com.google.common.annotations.Beta;
11 import java.util.List;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
15 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
16 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
17 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
18 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
19 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
21 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
22 import org.opendaylight.yangtools.yang.data.util.codec.CodecCache;
23 import org.opendaylight.yangtools.yang.data.util.codec.LazyCodecCache;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
45
46 /**
47  * Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
48  * a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
49  */
50 @Beta
51 public abstract class JSONCodecFactory extends AbstractCodecFactory<JSONCodec<?>> {
52     JSONCodecFactory(final @NonNull EffectiveModelContext context, final @NonNull CodecCache<JSONCodec<?>> cache) {
53         super(context, cache);
54     }
55
56     @Override
57     protected final JSONCodec<?> binaryCodec(final BinaryTypeDefinition type) {
58         return new QuotedJSONCodec<>(BinaryStringCodec.from(type));
59     }
60
61     @Override
62     protected final JSONCodec<?> booleanCodec(final BooleanTypeDefinition type) {
63         return new BooleanJSONCodec(BooleanStringCodec.from(type));
64     }
65
66     @Override
67     protected final JSONCodec<?> bitsCodec(final BitsTypeDefinition type) {
68         return new QuotedJSONCodec<>(BitsStringCodec.from(type));
69     }
70
71     @Override
72     protected final JSONCodec<?> decimalCodec(final DecimalTypeDefinition type) {
73         return wrapDecimalCodec(DecimalStringCodec.from(type));
74     }
75
76     @Override
77     protected final JSONCodec<?> emptyCodec(final EmptyTypeDefinition type) {
78         return EmptyJSONCodec.INSTANCE;
79     }
80
81     @Override
82     protected final JSONCodec<?> enumCodec(final EnumTypeDefinition type) {
83         return new QuotedJSONCodec<>(EnumStringCodec.from(type));
84     }
85
86     @Override
87     protected final JSONCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
88         return new IdentityrefJSONCodec(getEffectiveModelContext(), module);
89     }
90
91     @Override
92     protected final JSONCodec<?> int8Codec(final Int8TypeDefinition type) {
93         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
94     }
95
96     @Override
97     protected final JSONCodec<?> int16Codec(final Int16TypeDefinition type) {
98         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
99     }
100
101     @Override
102     protected final JSONCodec<?> int32Codec(final Int32TypeDefinition type) {
103         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
104     }
105
106     @Override
107     protected final JSONCodec<?> int64Codec(final Int64TypeDefinition type) {
108         return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
109     }
110
111     @Override
112     protected final JSONCodec<?> stringCodec(final StringTypeDefinition type) {
113         return new QuotedJSONCodec<>(StringStringCodec.from(type));
114     }
115
116     @Override
117     protected final JSONCodec<?> uint8Codec(final Uint8TypeDefinition type) {
118         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
119     }
120
121     @Override
122     protected final JSONCodec<?> uint16Codec(final Uint16TypeDefinition type) {
123         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
124     }
125
126     @Override
127     protected final JSONCodec<?> uint32Codec(final Uint32TypeDefinition type) {
128         return new NumberJSONCodec<>(AbstractIntegerStringCodec.from(type));
129     }
130
131     @Override
132     protected final JSONCodec<?> uint64Codec(final Uint64TypeDefinition type) {
133         return wrapIntegerCodec(AbstractIntegerStringCodec.from(type));
134     }
135
136     @Override
137     protected final JSONCodec<?> unionCodec(final UnionTypeDefinition type, final List<JSONCodec<?>> codecs) {
138         return UnionJSONCodec.create(type, codecs);
139     }
140
141     @Override
142     protected final JSONCodec<?> unknownCodec(final UnknownTypeDefinition type) {
143         return NullJSONCodec.INSTANCE;
144     }
145
146     @Override
147     protected abstract JSONCodec<?> instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
148
149     // Returns a one-off factory for the purposes of normalizing an anydata tree.
150     //
151     // FIXME: 6.0.0: this is really ugly, as we should be able to tell if the new context is the same as ours and
152     //               whether our cache is thread-safe -- in which case we should just return this.
153     //               The supplier/cache/factory layout needs to be reworked so that this call ends up being equivalent
154     //               to JSONCodecFactorySupplier.getShared() in case this factory is not thread safe.
155     //
156     //               The above is not currently possible, as we cannot reference JSONCodecFactorySupplier from the
157     //               factory due to that potentially creating a circular reference.
158     final JSONCodecFactory rebaseTo(final EffectiveModelContext newSchemaContext) {
159         return rebaseTo(newSchemaContext, new LazyCodecCache<>());
160     }
161
162     abstract JSONCodecFactory rebaseTo(EffectiveModelContext newSchemaContext, CodecCache<JSONCodec<?>> newCache);
163
164     abstract JSONCodec<?> wrapDecimalCodec(DecimalStringCodec decimalCodec);
165
166     abstract JSONCodec<?> wrapIntegerCodec(AbstractIntegerStringCodec<?, ?> integerCodec);
167 }