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