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