Merge branch 'master' of ../controller
[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.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;
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<?, ?, ?>> extends AbstractSchemaContextProvider {
55     private static final Logger LOG = LoggerFactory.getLogger(AbstractCodecFactory.class);
56
57     private final @NonNull CodecCache<T> cache;
58
59     protected AbstractCodecFactory(final @NonNull SchemaContext schemaContext, final @NonNull CodecCache<T> cache) {
60         super(schemaContext);
61         this.cache = requireNonNull(cache);
62     }
63
64     public final <S extends TypeAware & SchemaNode> @NonNull T codecFor(final S schema) {
65         /*
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.
68          *
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
74          *
75          * We assume prevalence is in above order and that caching is effective.
76          */
77         final TypeDefinition<?> type = schema.getType();
78         T ret = cache.lookupSimple(type);
79         if (ret != null) {
80             LOG.trace("Type {} hit simple {}", type, ret);
81             return ret;
82         }
83         ret = cache.lookupComplex(schema);
84         if (ret != null) {
85             LOG.trace("Type {} hit complex {}", type, ret);
86             return ret;
87         }
88
89         // Dealing with simple types first...
90         ret = getSimpleCodecFor(type);
91         if (ret != null) {
92             LOG.trace("Type {} miss simple {}", type, ret);
93             return ret;
94         }
95
96         // ... and complex types afterwards
97         ret = createComplexCodecFor(schema, type);
98         LOG.trace("Type {} miss complex {}", type, ret);
99         return cache.getComplex(schema, ret);
100     }
101
102     protected abstract T binaryCodec(BinaryTypeDefinition type);
103
104     protected abstract T booleanCodec(BooleanTypeDefinition type);
105
106     protected abstract T bitsCodec(BitsTypeDefinition type);
107
108     protected abstract T emptyCodec(EmptyTypeDefinition type);
109
110     protected abstract T enumCodec(EnumTypeDefinition type);
111
112     protected abstract T identityRefCodec(IdentityrefTypeDefinition type, QNameModule module);
113
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);
117
118     protected abstract T int8Codec(Int8TypeDefinition type);
119
120     protected abstract T int16Codec(Int16TypeDefinition type);
121
122     protected abstract T int32Codec(Int32TypeDefinition type);
123
124     protected abstract T int64Codec(Int64TypeDefinition type);
125
126     protected abstract T decimalCodec(DecimalTypeDefinition type);
127
128     protected abstract T stringCodec(StringTypeDefinition type);
129
130     protected abstract T uint8Codec(Uint8TypeDefinition type);
131
132     protected abstract T uint16Codec(Uint16TypeDefinition type);
133
134     protected abstract T uint32Codec(Uint32TypeDefinition type);
135
136     protected abstract T uint64Codec(Uint64TypeDefinition type);
137
138     protected abstract T unionCodec(UnionTypeDefinition type, List<T> codecs);
139
140     protected abstract T unknownCodec(UnknownTypeDefinition type);
141
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);
148         }
149
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.
152         final T ret;
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)) {
182                 return null;
183             }
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);
189         } else {
190             return null;
191         }
192
193         return cache.getSimple(type, verifyNotNull(ret));
194     }
195
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);
201                 return false;
202             }
203         }
204
205         LOG.debug("Type {} is simple", union);
206         return true;
207     }
208
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(),
216                     target);
217
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());
222         } else {
223             throw new IllegalArgumentException("Unsupported type " + type);
224         }
225     }
226
227     private T createSimpleUnion(final UnionTypeDefinition union) {
228         final List<TypeDefinition<?>> types = union.getTypes();
229         final List<T> codecs = new ArrayList<>(types.size());
230
231         for (TypeDefinition<?> type : types) {
232             T codec = cache.lookupSimple(type);
233             if (codec == null) {
234                 codec = verifyNotNull(getSimpleCodecFor(type), "Type %s did not resolve to a simple codec", type);
235             }
236
237             codecs.add(codec);
238         }
239
240         return unionCodec(union, codecs);
241     }
242
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());
246
247         for (TypeDefinition<?> type : types) {
248             T codec = cache.lookupSimple(type);
249             if (codec == null) {
250                 codec = getSimpleCodecFor(type);
251                 if (codec == null) {
252                     codec = createComplexCodecFor(schema, type);
253                 }
254             }
255
256             codecs.add(verifyNotNull(codec, "Schema %s subtype %s has no codec", schema, type));
257         }
258
259         return unionCodec(union, codecs);
260     }
261 }