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