2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.util.codec;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.TypeAware;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.util.AbstractEffectiveModelContextProvider;
42 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * A type-to-codec factory base class with logic to efficiently lookup and cache codec instances,
48 * also dealing with union type composition. This class is thread-safe as long as its underlying {@link CodecCache}
51 * @param <T> Codec type
52 * @author Robert Varga
54 public abstract class AbstractCodecFactory<T extends TypeAwareCodec<?, ?, ?>>
55 extends AbstractEffectiveModelContextProvider {
56 private static final Logger LOG = LoggerFactory.getLogger(AbstractCodecFactory.class);
58 private final @NonNull CodecCache<T> cache;
60 protected AbstractCodecFactory(final @NonNull EffectiveModelContext schemaContext,
61 final @NonNull CodecCache<T> cache) {
63 this.cache = requireNonNull(cache);
66 public final <S extends TypeAware & SchemaNode> @NonNull T codecFor(final S schema) {
68 * There are many trade-offs to be made here. We need the common case being as fast as possible while reusing
69 * codecs as much as possible.
71 * This gives us essentially four classes of codecs:
72 * - simple codecs, which are based on the type definition only
73 * - complex codecs, which depend on both type definition and the leaf
74 * - null codec, which does not depend on anything
75 * - instance identifier codec, which is based on namespace mapping
77 * We assume prevalence is in above order and that caching is effective.
79 final TypeDefinition<?> type = schema.getType();
80 T ret = cache.lookupSimple(type);
82 LOG.trace("Type {} hit simple {}", type, ret);
85 ret = cache.lookupComplex(schema);
87 LOG.trace("Type {} hit complex {}", type, ret);
91 // Dealing with simple types first...
92 ret = getSimpleCodecFor(type);
94 LOG.trace("Type {} miss simple {}", type, ret);
98 // ... and complex types afterwards
99 ret = createComplexCodecFor(schema, type);
100 LOG.trace("Type {} miss complex {}", type, ret);
101 return cache.getComplex(schema, ret);
104 protected abstract T binaryCodec(BinaryTypeDefinition type);
106 protected abstract T booleanCodec(BooleanTypeDefinition type);
108 protected abstract T bitsCodec(BitsTypeDefinition type);
110 protected abstract T emptyCodec(EmptyTypeDefinition type);
112 protected abstract T enumCodec(EnumTypeDefinition type);
114 protected abstract T identityRefCodec(IdentityrefTypeDefinition type, QNameModule module);
116 // FIXME: there really are two favors, as 'require-instance true' needs to be validated. In order to deal
117 // with that, though, we need access to the current data store.
118 protected abstract T instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
120 protected abstract T int8Codec(Int8TypeDefinition type);
122 protected abstract T int16Codec(Int16TypeDefinition type);
124 protected abstract T int32Codec(Int32TypeDefinition type);
126 protected abstract T int64Codec(Int64TypeDefinition type);
128 protected abstract T decimalCodec(DecimalTypeDefinition type);
130 protected abstract T stringCodec(StringTypeDefinition type);
132 protected abstract T uint8Codec(Uint8TypeDefinition type);
134 protected abstract T uint16Codec(Uint16TypeDefinition type);
136 protected abstract T uint32Codec(Uint32TypeDefinition type);
138 protected abstract T uint64Codec(Uint64TypeDefinition type);
140 protected abstract T unionCodec(UnionTypeDefinition type, List<T> codecs);
142 protected abstract T unknownCodec(UnknownTypeDefinition type);
144 private T getSimpleCodecFor(final TypeDefinition<?> type) {
145 // These types are expected to be fully-shared
146 if (type instanceof EmptyTypeDefinition) {
147 return emptyCodec((EmptyTypeDefinition) type);
148 } else if (type instanceof UnknownTypeDefinition) {
149 return unknownCodec((UnknownTypeDefinition) type);
152 // Now deal with simple types. Note we consider union composed of purely simple types a simple type itself.
153 // The checks here are optimized for common types.
155 if (type instanceof StringTypeDefinition) {
156 ret = stringCodec((StringTypeDefinition) type);
157 } else if (type instanceof Int8TypeDefinition) {
158 ret = int8Codec((Int8TypeDefinition) type);
159 } else if (type instanceof Int16TypeDefinition) {
160 ret = int16Codec((Int16TypeDefinition) type);
161 } else if (type instanceof Int32TypeDefinition) {
162 ret = int32Codec((Int32TypeDefinition) type);
163 } else if (type instanceof Int64TypeDefinition) {
164 ret = int64Codec((Int64TypeDefinition) type);
165 } else if (type instanceof Uint8TypeDefinition) {
166 ret = uint8Codec((Uint8TypeDefinition) type);
167 } else if (type instanceof Uint16TypeDefinition) {
168 ret = uint16Codec((Uint16TypeDefinition) type);
169 } else if (type instanceof Uint32TypeDefinition) {
170 ret = uint32Codec((Uint32TypeDefinition) type);
171 } else if (type instanceof Uint64TypeDefinition) {
172 ret = uint64Codec((Uint64TypeDefinition) type);
173 } else if (type instanceof BooleanTypeDefinition) {
174 ret = booleanCodec((BooleanTypeDefinition) type);
175 } else if (type instanceof DecimalTypeDefinition) {
176 ret = decimalCodec((DecimalTypeDefinition) type);
177 } else if (type instanceof EnumTypeDefinition) {
178 ret = enumCodec((EnumTypeDefinition) type);
179 } else if (type instanceof BitsTypeDefinition) {
180 ret = bitsCodec((BitsTypeDefinition) type);
181 } else if (type instanceof UnionTypeDefinition) {
182 final UnionTypeDefinition union = (UnionTypeDefinition) type;
183 if (!isSimpleUnion(union)) {
186 ret = createSimpleUnion(union);
187 } else if (type instanceof BinaryTypeDefinition) {
188 ret = binaryCodec((BinaryTypeDefinition) type);
189 } else if (type instanceof InstanceIdentifierTypeDefinition) {
190 return instanceIdentifierCodec((InstanceIdentifierTypeDefinition) type);
195 return cache.getSimple(type, verifyNotNull(ret));
198 private static boolean isSimpleUnion(final UnionTypeDefinition union) {
199 for (TypeDefinition<?> t : union.getTypes()) {
200 if (t instanceof IdentityrefTypeDefinition || t instanceof LeafrefTypeDefinition
201 || t instanceof UnionTypeDefinition && !isSimpleUnion((UnionTypeDefinition) t)) {
202 LOG.debug("Type {} has non-simple subtype", t);
207 LOG.debug("Type {} is simple", union);
211 private T createComplexCodecFor(final SchemaNode schema, final TypeDefinition<?> type) {
212 if (type instanceof UnionTypeDefinition) {
213 return createComplexUnion(schema, (UnionTypeDefinition) type);
214 } else if (type instanceof LeafrefTypeDefinition) {
215 final TypeDefinition<?> target = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) type,
216 getEffectiveModelContext(), schema);
217 verifyNotNull(target, "Unable to find base type for leafref node %s type %s.", schema, target);
219 final T ret = getSimpleCodecFor(target);
220 return ret != null ? ret : createComplexCodecFor(schema, target);
221 } else if (type instanceof IdentityrefTypeDefinition) {
222 return identityRefCodec((IdentityrefTypeDefinition) type, schema.getQName().getModule());
224 throw new IllegalArgumentException("Unsupported type " + type);
228 private T createSimpleUnion(final UnionTypeDefinition union) {
229 final List<TypeDefinition<?>> types = union.getTypes();
230 final List<T> codecs = new ArrayList<>(types.size());
232 for (TypeDefinition<?> type : types) {
233 T codec = cache.lookupSimple(type);
235 codec = verifyNotNull(getSimpleCodecFor(type), "Type %s did not resolve to a simple codec", type);
241 return unionCodec(union, codecs);
244 private T createComplexUnion(final SchemaNode schema, final UnionTypeDefinition union) {
245 final List<TypeDefinition<?>> types = union.getTypes();
246 final List<T> codecs = new ArrayList<>(types.size());
248 for (TypeDefinition<?> type : types) {
249 T codec = cache.lookupSimple(type);
251 codec = getSimpleCodecFor(type);
253 codec = createComplexCodecFor(schema, type);
257 codecs.add(verifyNotNull(codec, "Schema %s subtype %s has no codec", schema, type));
260 return unionCodec(union, codecs);