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.SchemaContext;
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.AbstractSchemaContextProvider;
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<?, ?, ?>> extends AbstractSchemaContextProvider {
55 private static final Logger LOG = LoggerFactory.getLogger(AbstractCodecFactory.class);
57 private final @NonNull CodecCache<T> cache;
59 protected AbstractCodecFactory(final @NonNull SchemaContext schemaContext, final @NonNull CodecCache<T> cache) {
61 this.cache = requireNonNull(cache);
64 public final <S extends TypeAware & SchemaNode> @NonNull T codecFor(final S schema) {
66 * There are many trade-offs to be made here. We need the common case being as fast as possible while reusing
67 * codecs as much as possible.
69 * This gives us essentially four classes of codecs:
70 * - simple codecs, which are based on the type definition only
71 * - complex codecs, which depend on both type definition and the leaf
72 * - null codec, which does not depend on anything
73 * - instance identifier codec, which is based on namespace mapping
75 * We assume prevalence is in above order and that caching is effective.
77 final TypeDefinition<?> type = schema.getType();
78 T ret = cache.lookupSimple(type);
80 LOG.trace("Type {} hit simple {}", type, ret);
83 ret = cache.lookupComplex(schema);
85 LOG.trace("Type {} hit complex {}", type, ret);
89 // Dealing with simple types first...
90 ret = getSimpleCodecFor(type);
92 LOG.trace("Type {} miss simple {}", type, ret);
96 // ... and complex types afterwards
97 ret = createComplexCodecFor(schema, type);
98 LOG.trace("Type {} miss complex {}", type, ret);
99 return cache.getComplex(schema, ret);
102 protected abstract T binaryCodec(BinaryTypeDefinition type);
104 protected abstract T booleanCodec(BooleanTypeDefinition type);
106 protected abstract T bitsCodec(BitsTypeDefinition type);
108 protected abstract T emptyCodec(EmptyTypeDefinition type);
110 protected abstract T enumCodec(EnumTypeDefinition type);
112 protected abstract T identityRefCodec(IdentityrefTypeDefinition type, QNameModule module);
114 // FIXME: there really are two favors, as 'require-instance true' needs to be validated. In order to deal
115 // with that, though, we need access to the current data store.
116 protected abstract T instanceIdentifierCodec(InstanceIdentifierTypeDefinition type);
118 protected abstract T int8Codec(Int8TypeDefinition type);
120 protected abstract T int16Codec(Int16TypeDefinition type);
122 protected abstract T int32Codec(Int32TypeDefinition type);
124 protected abstract T int64Codec(Int64TypeDefinition type);
126 protected abstract T decimalCodec(DecimalTypeDefinition type);
128 protected abstract T stringCodec(StringTypeDefinition type);
130 protected abstract T uint8Codec(Uint8TypeDefinition type);
132 protected abstract T uint16Codec(Uint16TypeDefinition type);
134 protected abstract T uint32Codec(Uint32TypeDefinition type);
136 protected abstract T uint64Codec(Uint64TypeDefinition type);
138 protected abstract T unionCodec(UnionTypeDefinition type, List<T> codecs);
140 protected abstract T unknownCodec(UnknownTypeDefinition type);
142 private T getSimpleCodecFor(final TypeDefinition<?> type) {
143 // These types are expected to be fully-shared
144 if (type instanceof EmptyTypeDefinition) {
145 return emptyCodec((EmptyTypeDefinition) type);
146 } else if (type instanceof UnknownTypeDefinition) {
147 return unknownCodec((UnknownTypeDefinition) type);
150 // Now deal with simple types. Note we consider union composed of purely simple types a simple type itself.
151 // The checks here are optimized for common types.
153 if (type instanceof StringTypeDefinition) {
154 ret = stringCodec((StringTypeDefinition) type);
155 } else if (type instanceof Int8TypeDefinition) {
156 ret = int8Codec((Int8TypeDefinition) type);
157 } else if (type instanceof Int16TypeDefinition) {
158 ret = int16Codec((Int16TypeDefinition) type);
159 } else if (type instanceof Int32TypeDefinition) {
160 ret = int32Codec((Int32TypeDefinition) type);
161 } else if (type instanceof Int64TypeDefinition) {
162 ret = int64Codec((Int64TypeDefinition) type);
163 } else if (type instanceof Uint8TypeDefinition) {
164 ret = uint8Codec((Uint8TypeDefinition) type);
165 } else if (type instanceof Uint16TypeDefinition) {
166 ret = uint16Codec((Uint16TypeDefinition) type);
167 } else if (type instanceof Uint32TypeDefinition) {
168 ret = uint32Codec((Uint32TypeDefinition) type);
169 } else if (type instanceof Uint64TypeDefinition) {
170 ret = uint64Codec((Uint64TypeDefinition) type);
171 } else if (type instanceof BooleanTypeDefinition) {
172 ret = booleanCodec((BooleanTypeDefinition) type);
173 } else if (type instanceof DecimalTypeDefinition) {
174 ret = decimalCodec((DecimalTypeDefinition) type);
175 } else if (type instanceof EnumTypeDefinition) {
176 ret = enumCodec((EnumTypeDefinition) type);
177 } else if (type instanceof BitsTypeDefinition) {
178 ret = bitsCodec((BitsTypeDefinition) type);
179 } else if (type instanceof UnionTypeDefinition) {
180 final UnionTypeDefinition union = (UnionTypeDefinition) type;
181 if (!isSimpleUnion(union)) {
184 ret = createSimpleUnion(union);
185 } else if (type instanceof BinaryTypeDefinition) {
186 ret = binaryCodec((BinaryTypeDefinition) type);
187 } else if (type instanceof InstanceIdentifierTypeDefinition) {
188 return instanceIdentifierCodec((InstanceIdentifierTypeDefinition) type);
193 return cache.getSimple(type, verifyNotNull(ret));
196 private static boolean isSimpleUnion(final UnionTypeDefinition union) {
197 for (TypeDefinition<?> t : union.getTypes()) {
198 if (t instanceof IdentityrefTypeDefinition || t instanceof LeafrefTypeDefinition
199 || t instanceof UnionTypeDefinition && !isSimpleUnion((UnionTypeDefinition) t)) {
200 LOG.debug("Type {} has non-simple subtype", t);
205 LOG.debug("Type {} is simple", union);
209 private T createComplexCodecFor(final SchemaNode schema, final TypeDefinition<?> type) {
210 if (type instanceof UnionTypeDefinition) {
211 return createComplexUnion(schema, (UnionTypeDefinition) type);
212 } else if (type instanceof LeafrefTypeDefinition) {
213 final TypeDefinition<?> target = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) type,
214 getSchemaContext(), schema);
215 verifyNotNull(target, "Unable to find base type for leafref node %s type %s.", schema.getPath(),
218 final T ret = getSimpleCodecFor(target);
219 return ret != null ? ret : createComplexCodecFor(schema, target);
220 } else if (type instanceof IdentityrefTypeDefinition) {
221 return identityRefCodec((IdentityrefTypeDefinition) type, schema.getQName().getModule());
223 throw new IllegalArgumentException("Unsupported type " + type);
227 private T createSimpleUnion(final UnionTypeDefinition union) {
228 final List<TypeDefinition<?>> types = union.getTypes();
229 final List<T> codecs = new ArrayList<>(types.size());
231 for (TypeDefinition<?> type : types) {
232 T codec = cache.lookupSimple(type);
234 codec = verifyNotNull(getSimpleCodecFor(type), "Type %s did not resolve to a simple codec", type);
240 return unionCodec(union, codecs);
243 private T createComplexUnion(final SchemaNode schema, final UnionTypeDefinition union) {
244 final List<TypeDefinition<?>> types = union.getTypes();
245 final List<T> codecs = new ArrayList<>(types.size());
247 for (TypeDefinition<?> type : types) {
248 T codec = cache.lookupSimple(type);
250 codec = getSimpleCodecFor(type);
252 codec = createComplexCodecFor(schema, type);
256 codecs.add(verifyNotNull(codec, "Schema %s subtype %s has no codec", schema, type));
259 return unionCodec(union, codecs);