Mass-migrate to use EffectiveModelContext
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / codec / AbstractCodecFactory.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.util.codec;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
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;
45
46 /**
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}
49  * is thread-safe
50  *
51  * @param <T> Codec type
52  * @author Robert Varga
53  */
54 public abstract class AbstractCodecFactory<T extends TypeAwareCodec<?, ?, ?>>
55         extends AbstractEffectiveModelContextProvider {
56     private static final Logger LOG = LoggerFactory.getLogger(AbstractCodecFactory.class);
57
58     private final @NonNull CodecCache<T> cache;
59
60     protected AbstractCodecFactory(final @NonNull EffectiveModelContext schemaContext,
61             final @NonNull CodecCache<T> cache) {
62         super(schemaContext);
63         this.cache = requireNonNull(cache);
64     }
65
66     public final <S extends TypeAware & SchemaNode> @NonNull T codecFor(final S schema) {
67         /*
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.
70          *
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
76          *
77          * We assume prevalence is in above order and that caching is effective.
78          */
79         final TypeDefinition<?> type = schema.getType();
80         T ret = cache.lookupSimple(type);
81         if (ret != null) {
82             LOG.trace("Type {} hit simple {}", type, ret);
83             return ret;
84         }
85         ret = cache.lookupComplex(schema);
86         if (ret != null) {
87             LOG.trace("Type {} hit complex {}", type, ret);
88             return ret;
89         }
90
91         // Dealing with simple types first...
92         ret = getSimpleCodecFor(type);
93         if (ret != null) {
94             LOG.trace("Type {} miss simple {}", type, ret);
95             return ret;
96         }
97
98         // ... and complex types afterwards
99         ret = createComplexCodecFor(schema, type);
100         LOG.trace("Type {} miss complex {}", type, ret);
101         return cache.getComplex(schema, ret);
102     }
103
104     protected abstract T binaryCodec(BinaryTypeDefinition type);
105
106     protected abstract T booleanCodec(BooleanTypeDefinition type);
107
108     protected abstract T bitsCodec(BitsTypeDefinition type);
109
110     protected abstract T emptyCodec(EmptyTypeDefinition type);
111
112     protected abstract T enumCodec(EnumTypeDefinition type);
113
114     protected abstract T identityRefCodec(IdentityrefTypeDefinition type, QNameModule module);
115
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);
119
120     protected abstract T int8Codec(Int8TypeDefinition type);
121
122     protected abstract T int16Codec(Int16TypeDefinition type);
123
124     protected abstract T int32Codec(Int32TypeDefinition type);
125
126     protected abstract T int64Codec(Int64TypeDefinition type);
127
128     protected abstract T decimalCodec(DecimalTypeDefinition type);
129
130     protected abstract T stringCodec(StringTypeDefinition type);
131
132     protected abstract T uint8Codec(Uint8TypeDefinition type);
133
134     protected abstract T uint16Codec(Uint16TypeDefinition type);
135
136     protected abstract T uint32Codec(Uint32TypeDefinition type);
137
138     protected abstract T uint64Codec(Uint64TypeDefinition type);
139
140     protected abstract T unionCodec(UnionTypeDefinition type, List<T> codecs);
141
142     protected abstract T unknownCodec(UnknownTypeDefinition type);
143
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);
150         }
151
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.
154         final T ret;
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)) {
184                 return null;
185             }
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);
191         } else {
192             return null;
193         }
194
195         return cache.getSimple(type, verifyNotNull(ret));
196     }
197
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);
203                 return false;
204             }
205         }
206
207         LOG.debug("Type {} is simple", union);
208         return true;
209     }
210
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);
218
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());
223         } else {
224             throw new IllegalArgumentException("Unsupported type " + type);
225         }
226     }
227
228     private T createSimpleUnion(final UnionTypeDefinition union) {
229         final List<TypeDefinition<?>> types = union.getTypes();
230         final List<T> codecs = new ArrayList<>(types.size());
231
232         for (TypeDefinition<?> type : types) {
233             T codec = cache.lookupSimple(type);
234             if (codec == null) {
235                 codec = verifyNotNull(getSimpleCodecFor(type), "Type %s did not resolve to a simple codec", type);
236             }
237
238             codecs.add(codec);
239         }
240
241         return unionCodec(union, codecs);
242     }
243
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());
247
248         for (TypeDefinition<?> type : types) {
249             T codec = cache.lookupSimple(type);
250             if (codec == null) {
251                 codec = getSimpleCodecFor(type);
252                 if (codec == null) {
253                     codec = createComplexCodecFor(schema, type);
254                 }
255             }
256
257             codecs.add(verifyNotNull(codec, "Schema %s subtype %s has no codec", schema, type));
258         }
259
260         return unionCodec(union, codecs);
261     }
262 }