2 * Copyright (c) 2014 Cisco Systems, Inc. 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.sal.binding.generator.impl;
10 import java.lang.ref.WeakReference;
11 import java.lang.reflect.Field;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
20 import java.util.Map.Entry;
21 import java.util.Objects;
23 import java.util.WeakHashMap;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
27 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
28 import org.opendaylight.yangtools.binding.generator.util.Types;
29 import org.opendaylight.yangtools.concepts.Delegator;
30 import org.opendaylight.yangtools.concepts.Identifiable;
31 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
32 import org.opendaylight.yangtools.sal.binding.generator.util.CodeGenerationException;
33 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
34 import org.opendaylight.yangtools.sal.binding.model.api.Type;
35 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
36 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
37 import org.opendaylight.yangtools.yang.binding.Augmentable;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
40 import org.opendaylight.yangtools.yang.binding.BindingCodec;
41 import org.opendaylight.yangtools.yang.binding.DataContainer;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.Identifier;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
48 import org.opendaylight.yangtools.yang.data.api.Node;
49 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
50 import org.opendaylight.yangtools.yang.data.impl.codec.AugmentationCodec;
51 import org.opendaylight.yangtools.yang.data.impl.codec.ChoiceCaseCodec;
52 import org.opendaylight.yangtools.yang.data.impl.codec.ChoiceCodec;
53 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
54 import org.opendaylight.yangtools.yang.data.impl.codec.DataContainerCodec;
55 import org.opendaylight.yangtools.yang.data.impl.codec.DomCodec;
56 import org.opendaylight.yangtools.yang.data.impl.codec.IdentifierCodec;
57 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
58 import org.opendaylight.yangtools.yang.data.impl.codec.InstanceIdentifierCodec;
59 import org.opendaylight.yangtools.yang.data.impl.codec.ValueWithQName;
60 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
61 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
62 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
63 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
64 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
65 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
66 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
67 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
68 import org.opendaylight.yangtools.yang.model.api.Module;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
70 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
71 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
72 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
76 import com.google.common.base.Optional;
77 import com.google.common.base.Preconditions;
78 import com.google.common.collect.BiMap;
79 import com.google.common.collect.HashBiMap;
80 import com.google.common.collect.HashMultimap;
81 import com.google.common.collect.Multimap;
82 import com.google.common.collect.Multimaps;
84 public class LazyGeneratedCodecRegistry implements //
86 SchemaContextListener, //
89 private static final Logger LOG = LoggerFactory.getLogger(LazyGeneratedCodecRegistry.class);
90 private static final LateMixinCodec NOT_READY_CODEC = new LateMixinCodec();
92 private final InstanceIdentifierCodec instanceIdentifierCodec = new InstanceIdentifierCodecImpl(this);
93 private final IdentityCompositeCodec identityRefCodec = new IdentityCompositeCodec();
95 private TransformerGenerator generator;
97 // Concrete class to codecs
98 private static final Map<Class<?>, DataContainerCodec<?>> containerCodecs = Collections
99 .synchronizedMap(new WeakHashMap<Class<?>, DataContainerCodec<?>>());
100 private static final Map<Class<?>, IdentifierCodec<?>> identifierCodecs = Collections
101 .synchronizedMap(new WeakHashMap<Class<?>, IdentifierCodec<?>>());
102 private static final Map<Class<?>, PublicChoiceCodecImpl<?>> choiceCodecs = Collections
103 .synchronizedMap(new WeakHashMap<Class<?>, PublicChoiceCodecImpl<?>>());
104 private static final Map<Class<?>, ChoiceCaseCodecImpl<?>> caseCodecs = Collections
105 .synchronizedMap(new WeakHashMap<Class<?>, ChoiceCaseCodecImpl<?>>());
106 private static final Map<Class<?>, AugmentableDispatchCodec> augmentableCodecs = Collections
107 .synchronizedMap(new WeakHashMap<Class<?>, AugmentableDispatchCodec>());
108 private static final Map<Class<?>, AugmentationCodecWrapper<?>> augmentationCodecs = Collections
109 .synchronizedMap(new WeakHashMap<Class<?>, AugmentationCodecWrapper<?>>());
110 private static final Map<Class<?>, QName> identityQNames = Collections
111 .synchronizedMap(new WeakHashMap<Class<?>, QName>());
112 private static final Map<QName, Type> qnamesToIdentityMap = new ConcurrentHashMap<>();
113 /** Binding type to encountered classes mapping **/
114 @SuppressWarnings("rawtypes")
115 private static final Map<Type, WeakReference<Class>> typeToClass = new ConcurrentHashMap<>();
117 @SuppressWarnings("rawtypes")
118 private static final ConcurrentMap<Type, ChoiceCaseCodecImpl> typeToCaseCodecs = new ConcurrentHashMap<>();
120 private final CaseClassMapFacade classToCaseRawCodec = new CaseClassMapFacade();
122 private static final Map<SchemaPath, InstanceIdentifier<?>> pathToBindingIdentifier = new ConcurrentHashMap<>();
123 private static final Map<SchemaPath, GeneratedTypeBuilder> pathToType = new ConcurrentHashMap<>();
124 private static final Map<List<QName>, Type> pathToInstantiatedType = new ConcurrentHashMap<>();
125 private static final Map<Type, QName> typeToQname = new ConcurrentHashMap<>();
126 private static final BiMap<Type, AugmentationSchema> typeToAugment = HashBiMap
127 .create(new ConcurrentHashMap<Type, AugmentationSchema>());
129 private static final Multimap<Type, Type> augmentableToAugmentations = Multimaps.synchronizedMultimap(HashMultimap
130 .<Type, Type> create());
131 private static final Multimap<Type, Type> choiceToCases = Multimaps.synchronizedMultimap(HashMultimap
132 .<Type, Type> create());
134 private final SchemaLock lock;
136 // FIXME: how is this protected?
137 private SchemaContext currentSchema;
139 private final ClassLoadingStrategy classLoadingStrategy;
141 LazyGeneratedCodecRegistry(final SchemaLock lock, final ClassLoadingStrategy identityClassLoadingStrategy) {
142 this.lock = Preconditions.checkNotNull(lock);
143 this.classLoadingStrategy = identityClassLoadingStrategy;
146 public SchemaLock getLock() {
150 public TransformerGenerator getGenerator() {
154 public void setGenerator(final TransformerGenerator generator) {
155 this.generator = generator;
159 public InstanceIdentifierCodec getInstanceIdentifierCodec() {
160 return instanceIdentifierCodec;
163 @SuppressWarnings("unchecked")
165 public <T extends Augmentation<?>> AugmentationCodecWrapper<T> getCodecForAugmentation(final Class<T> object) {
166 AugmentationCodecWrapper<T> codec = null;
167 @SuppressWarnings("rawtypes")
168 AugmentationCodecWrapper potentialCodec = augmentationCodecs.get(object);
169 if (potentialCodec != null) {
170 codec = potentialCodec;
173 lock.waitForSchema(object);
174 Class<? extends BindingCodec<Map<QName, Object>, Object>> augmentRawCodec = generator
175 .augmentationTransformerFor(object);
176 BindingCodec<Map<QName, Object>, Object> rawCodec = augmentRawCodec.newInstance();
178 codec = new AugmentationCodecWrapper<T>(rawCodec, null, object);
179 augmentationCodecs.put(object, codec);
180 } catch (InstantiationException e) {
181 LOG.error("Can not instantiate raw augmentation codec {}", object.getSimpleName(), e);
182 } catch (IllegalAccessException e) {
184 "Run-time consistency issue: constructor {} is not available. This indicates either a code generation bug or a misconfiguration of JVM.",
185 object.getSimpleName(), e);
188 Class<? extends Augmentable<?>> objectSupertype = getAugmentableArgumentFrom(object);
189 if (objectSupertype != null) {
190 getAugmentableCodec(objectSupertype).addImplementation(codec);
192 LOG.warn("Could not find augmentation target for augmentation {}", object);
197 @SuppressWarnings("unchecked")
199 public QName getQNameForAugmentation(final Class<?> cls) {
200 Preconditions.checkArgument(Augmentation.class.isAssignableFrom(cls));
201 return getCodecForAugmentation((Class<? extends Augmentation<?>>) cls).getAugmentationQName();
204 private static Class<? extends Augmentable<?>> getAugmentableArgumentFrom(
205 final Class<? extends Augmentation<?>> augmentation) {
207 Class<? extends Augmentable<?>> ret = BindingReflections.findAugmentationTarget(augmentation);
210 } catch (Exception e) {
211 LOG.debug("Could not find augmentable for {} using {}", augmentation, augmentation.getClassLoader(), e);
217 public Class<?> getClassForPath(final List<QName> names) {
218 final DataSchemaNode node = getSchemaNode(names);
219 final SchemaPath path = node.getPath();
220 final Type t = pathToType.get(path);
224 type = new ReferencedTypeImpl(t.getPackageName(), t.getName());
226 type = pathToInstantiatedType.get(names);
227 Preconditions.checkState(type != null, "Failed to lookup instantiated type for path %s", path);
230 @SuppressWarnings("rawtypes")
231 final WeakReference<Class> weakRef = typeToClass.get(type);
232 Preconditions.checkState(weakRef != null, "Could not find loaded class for path: %s and type: %s", path, type.getFullyQualifiedName());
233 return weakRef.get();
237 public void putPathToClass(final List<QName> names, final Class<?> cls) {
238 final Type reference = Types.typeForClass(cls);
239 pathToInstantiatedType.put(names, reference);
240 LOG.trace("Path {} attached to class {} reference {}", names, cls, reference);
241 bindingClassEncountered(cls);
244 public InstanceIdentifier<?> getBindingIdentifierByPath(final SchemaPath path) {
245 return pathToBindingIdentifier.get(path);
248 public void putPathToBindingIdentifier(final SchemaPath path, final InstanceIdentifier<?> bindingIdentifier) {
249 pathToBindingIdentifier.put(path, bindingIdentifier);
252 public InstanceIdentifier<?> putPathToBindingIdentifier(final SchemaPath path,
253 final InstanceIdentifier<?> bindingIdentifier, final Class<?> childClass) {
254 @SuppressWarnings({ "unchecked", "rawtypes" })
255 InstanceIdentifier<?> newId = bindingIdentifier.builder().child((Class) childClass).build();
256 pathToBindingIdentifier.put(path, newId);
261 public IdentifierCodec<?> getKeyCodecForPath(final List<QName> names) {
262 @SuppressWarnings("unchecked")
263 Class<? extends Identifiable<?>> cls = (Class<? extends Identifiable<?>>) getClassForPath(names);
264 return getIdentifierCodecForIdentifiable(cls);
268 public <T extends DataContainer> DataContainerCodec<T> getCodecForDataObject(final Class<T> type) {
269 @SuppressWarnings("unchecked")
270 DataContainerCodec<T> ret = (DataContainerCodec<T>) containerCodecs.get(type);
274 Class<? extends BindingCodec<Map<QName, Object>, Object>> newType = generator.transformerFor(type);
275 BindingCodec<Map<QName, Object>, Object> rawCodec = newInstanceOf(newType);
276 DataContainerCodecImpl<T> newWrapper = new DataContainerCodecImpl<>(rawCodec);
277 containerCodecs.put(type, newWrapper);
282 @SuppressWarnings("rawtypes")
283 public void bindingClassEncountered(final Class cls) {
285 ConcreteType typeRef = Types.typeForClass(cls);
286 if (typeToClass.containsKey(typeRef)) {
289 LOG.trace("Binding Class {} encountered.", cls);
290 WeakReference<Class> weakRef = new WeakReference<>(cls);
291 typeToClass.put(typeRef, weakRef);
292 if (Augmentation.class.isAssignableFrom(cls)) {
294 } else if (DataObject.class.isAssignableFrom(cls)) {
295 @SuppressWarnings({ "unchecked", "unused" })
296 Object cdc = getCodecForDataObject((Class<? extends DataObject>) cls);
301 public void onClassProcessed(final Class<?> cls) {
302 ConcreteType typeRef = Types.typeForClass(cls);
303 if (typeToClass.containsKey(typeRef)) {
306 LOG.trace("Binding Class {} encountered.", cls);
307 @SuppressWarnings("rawtypes")
308 WeakReference<Class> weakRef = new WeakReference<Class>(cls);
309 typeToClass.put(typeRef, weakRef);
312 private DataSchemaNode getSchemaNode(final List<QName> path) {
313 QName firstNode = path.get(0);
314 DataNodeContainer previous = currentSchema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
315 firstNode.getRevision());
316 Iterator<QName> iterator = path.iterator();
317 while (iterator.hasNext()) {
318 QName arg = iterator.next();
319 DataSchemaNode currentNode = previous.getDataChildByName(arg);
320 if (currentNode == null && previous instanceof DataNodeContainer) {
321 currentNode = searchInChoices(previous, arg);
323 if (currentNode instanceof DataNodeContainer) {
324 previous = (DataNodeContainer) currentNode;
325 } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
326 Preconditions.checkState(!iterator.hasNext(), "Path tries to nest inside leaf node.");
330 return (DataSchemaNode) previous;
333 private DataSchemaNode searchInChoices(final DataNodeContainer node, final QName arg) {
334 Set<DataSchemaNode> children = node.getChildNodes();
335 for (DataSchemaNode child : children) {
336 if (child instanceof ChoiceNode) {
337 ChoiceNode choiceNode = (ChoiceNode) child;
338 DataSchemaNode potential = searchInCases(choiceNode, arg);
339 if (potential != null) {
347 private DataSchemaNode searchInCases(final ChoiceNode choiceNode, final QName arg) {
348 Set<ChoiceCaseNode> cases = choiceNode.getCases();
349 for (ChoiceCaseNode caseNode : cases) {
350 DataSchemaNode node = caseNode.getDataChildByName(arg);
358 private <T> T newInstanceOf(final Class<?> newType) {
360 @SuppressWarnings("unchecked")
361 T ret = (T) newType.newInstance();
363 } catch (InstantiationException e) {
364 throw new IllegalStateException(e);
365 } catch (IllegalAccessException e) {
366 throw new IllegalStateException(e);
371 public <T extends Identifiable<?>> IdentifierCodec<?> getIdentifierCodecForIdentifiable(final Class<T> type) {
372 IdentifierCodec<?> obj = identifierCodecs.get(type);
376 Class<? extends BindingCodec<Map<QName, Object>, Object>> newCodec = generator
377 .keyTransformerForIdentifiable(type);
378 BindingCodec<Map<QName, Object>, Object> newInstance;
379 newInstance = newInstanceOf(newCodec);
380 IdentifierCodecImpl<?> newWrapper = new IdentifierCodecImpl<>(newInstance);
381 identifierCodecs.put(type, newWrapper);
386 public IdentityCodec<?> getIdentityCodec() {
387 return identityRefCodec;
390 @SuppressWarnings("unchecked")
392 public <T extends BaseIdentity> IdentityCodec<T> getCodecForIdentity(final Class<T> codec) {
393 bindingClassEncountered(codec);
394 return identityRefCodec;
398 public void onCodecCreated(final Class<?> cls) {
399 CodecMapping.setIdentifierCodec(cls, instanceIdentifierCodec);
400 CodecMapping.setIdentityRefCodec(cls, identityRefCodec);
404 public <T extends Identifier<?>> IdentifierCodec<T> getCodecForIdentifier(final Class<T> object) {
405 @SuppressWarnings("unchecked")
406 IdentifierCodec<T> obj = (IdentifierCodec<T>) identifierCodecs.get(object);
410 Class<? extends BindingCodec<Map<QName, Object>, Object>> newCodec = generator
411 .keyTransformerForIdentifier(object);
412 BindingCodec<Map<QName, Object>, Object> newInstance;
413 newInstance = newInstanceOf(newCodec);
414 IdentifierCodecImpl<T> newWrapper = new IdentifierCodecImpl<>(newInstance);
415 identifierCodecs.put(object, newWrapper);
419 @SuppressWarnings("rawtypes")
420 public ChoiceCaseCodecImpl getCaseCodecFor(final Class caseClass) {
421 ChoiceCaseCodecImpl<?> potential = caseCodecs.get(caseClass);
422 if (potential != null) {
425 ConcreteType typeref = Types.typeForClass(caseClass);
426 ChoiceCaseCodecImpl caseCodec = typeToCaseCodecs.get(typeref);
428 Preconditions.checkState(caseCodec != null, "Case Codec was not created proactivelly for %s",
429 caseClass.getName());
430 Preconditions.checkState(caseCodec.getSchema() != null, "Case schema is not available for %s",
431 caseClass.getName());
432 @SuppressWarnings("unchecked")
433 Class<? extends BindingCodec> newCodec = generator.caseCodecFor(caseClass, caseCodec.getSchema());
434 BindingCodec newInstance = newInstanceOf(newCodec);
435 caseCodec.setDelegate(newInstance);
436 caseCodecs.put(caseClass, caseCodec);
438 for (Entry<Class<?>, PublicChoiceCodecImpl<?>> choice : choiceCodecs.entrySet()) {
439 if (choice.getKey().isAssignableFrom(caseClass)) {
440 choice.getValue().cases.put(caseClass, caseCodec);
446 public void onModuleContextAdded(final SchemaContext schemaContext, final Module module, final ModuleContext context) {
447 pathToType.putAll(context.getChildNodes());
449 BiMap<Type, AugmentationSchema> bimap = context.getTypeToAugmentation();
450 for (Map.Entry<Type, AugmentationSchema> entry : bimap.entrySet()) {
451 Type key = entry.getKey();
452 AugmentationSchema value = entry.getValue();
453 Set<DataSchemaNode> augmentedNodes = value.getChildNodes();
454 if (augmentedNodes != null && !(augmentedNodes.isEmpty())) {
455 typeToAugment.put(key, value);
459 qnamesToIdentityMap.putAll(context.getIdentities());
460 for (Entry<QName, GeneratedTOBuilder> identity : context.getIdentities().entrySet()) {
462 new ReferencedTypeImpl(identity.getValue().getPackageName(), identity.getValue().getName()),
466 synchronized(augmentableToAugmentations) {
467 augmentableToAugmentations.putAll(context.getAugmentableToAugmentations());
469 synchronized(choiceToCases) {
470 choiceToCases.putAll(context.getChoiceToCases());
472 captureCases(context.getCases(), schemaContext);
475 private void captureCases(final Map<SchemaPath, GeneratedTypeBuilder> cases, final SchemaContext module) {
476 for (Entry<SchemaPath, GeneratedTypeBuilder> caseNode : cases.entrySet()) {
477 ReferencedTypeImpl typeref = new ReferencedTypeImpl(caseNode.getValue().getPackageName(), caseNode
478 .getValue().getName());
480 pathToType.put(caseNode.getKey(), caseNode.getValue());
482 ChoiceCaseNode node = (ChoiceCaseNode) SchemaContextUtil.findDataSchemaNode(module, caseNode.getKey());
485 LOG.warn("Failed to find YANG SchemaNode for {}, with path {} was not found in context.",
486 typeref.getFullyQualifiedName(), caseNode.getKey());
487 @SuppressWarnings("rawtypes")
488 ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl();
489 typeToCaseCodecs.putIfAbsent(typeref, value);
492 @SuppressWarnings("rawtypes")
493 ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl(node);
494 typeToCaseCodecs.putIfAbsent(typeref, value);
499 public void onGlobalContextUpdated(final SchemaContext context) {
500 currentSchema = context;
503 @SuppressWarnings({ "unchecked", "rawtypes" })
505 public void onChoiceCodecCreated(final Class<?> choiceClass,
506 final Class<? extends BindingCodec<Map<QName, Object>, Object>> choiceCodec, final ChoiceNode schema) {
507 ChoiceCodec<?> oldCodec = choiceCodecs.get(choiceClass);
508 Preconditions.checkState(oldCodec == null);
509 BindingCodec<Map<QName, Object>, Object> delegate = newInstanceOf(choiceCodec);
510 PublicChoiceCodecImpl<?> newCodec = new PublicChoiceCodecImpl(delegate);
511 choiceCodecs.put(choiceClass, newCodec);
512 CodecMapping.setClassToCaseMap(choiceCodec, classToCaseRawCodec);
513 CodecMapping.setCompositeNodeToCaseMap(choiceCodec, newCodec.getCompositeToCase());
515 tryToCreateCasesCodecs(schema);
520 private void tryToCreateCasesCodecs(final ChoiceNode schema) {
521 for (ChoiceCaseNode choiceCase : schema.getCases()) {
522 ChoiceCaseNode caseNode = choiceCase;
523 if (caseNode.isAddedByUses()) {
524 DataSchemaNode origCaseNode = SchemaContextUtil.findOriginal(caseNode, currentSchema);
525 if (origCaseNode instanceof ChoiceCaseNode) {
526 caseNode = (ChoiceCaseNode) origCaseNode;
529 SchemaPath path = caseNode.getPath();
531 GeneratedTypeBuilder type;
532 if (path != null && (type = pathToType.get(path)) != null) {
533 ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
534 @SuppressWarnings("rawtypes")
535 ChoiceCaseCodecImpl partialCodec = typeToCaseCodecs.get(typeref);
536 if (partialCodec.getSchema() == null) {
537 partialCodec.setSchema(caseNode);
540 Class<?> caseClass = classLoadingStrategy.loadClass(type.getFullyQualifiedName());
541 getCaseCodecFor(caseClass);
542 } catch (ClassNotFoundException e) {
543 LOG.trace("Could not proactivelly create case codec for {}", type, e);
551 public void onValueCodecCreated(final Class<?> valueClass, final Class<?> valueCodec) {
555 public void onCaseCodecCreated(final Class<?> choiceClass,
556 final Class<? extends BindingCodec<Map<QName, Object>, Object>> choiceCodec) {
560 public void onDataContainerCodecCreated(final Class<?> dataClass,
561 final Class<? extends BindingCodec<?, ?>> dataCodec) {
562 if (Augmentable.class.isAssignableFrom(dataClass)) {
563 AugmentableDispatchCodec augmentableCodec = getAugmentableCodec(dataClass);
564 CodecMapping.setAugmentationCodec(dataCodec, augmentableCodec);
568 public AugmentableDispatchCodec getAugmentableCodec(final Class<?> dataClass) {
569 AugmentableDispatchCodec ret = augmentableCodecs.get(dataClass);
573 ret = new AugmentableDispatchCodec(dataClass);
574 augmentableCodecs.put(dataClass, ret);
575 ret.tryToLoadImplementations();
579 private static abstract class IntermediateCodec<T> implements //
580 DomCodec<T>, Delegator<BindingCodec<Map<QName, Object>, Object>> {
582 private final BindingCodec<Map<QName, Object>, Object> delegate;
585 public BindingCodec<Map<QName, Object>, Object> getDelegate() {
589 public IntermediateCodec(final BindingCodec<Map<QName, Object>, Object> delegate) {
590 this.delegate = delegate;
594 public Node<?> serialize(final ValueWithQName<T> input) {
595 Map<QName, Object> intermediateOutput = delegate.serialize(input);
596 return IntermediateMapping.toNode(intermediateOutput);
601 private static class IdentifierCodecImpl<T extends Identifier<?>> //
602 extends IntermediateCodec<T> //
603 implements IdentifierCodec<T> {
605 public IdentifierCodecImpl(final BindingCodec<Map<QName, Object>, Object> delegate) {
610 public ValueWithQName<T> deserialize(final Node<?> input) {
611 QName qname = input.getNodeType();
612 @SuppressWarnings("unchecked")
613 T value = (T) getDelegate().deserialize((Map<QName, Object>) input);
614 return new ValueWithQName<T>(qname, value);
618 public ValueWithQName<T> deserialize(final Node<?> input, final InstanceIdentifier<?> bindingIdentifier) {
619 QName qname = input.getNodeType();
620 @SuppressWarnings("unchecked")
621 T value = (T) getDelegate().deserialize((Map<QName, Object>) input, bindingIdentifier);
622 return new ValueWithQName<T>(qname, value);
626 public CompositeNode serialize(final ValueWithQName<T> input) {
627 return (CompositeNode) super.serialize(input);
631 private static class DataContainerCodecImpl<T extends DataContainer> //
632 extends IntermediateCodec<T> //
633 implements DataContainerCodec<T> {
635 public DataContainerCodecImpl(final BindingCodec<Map<QName, Object>, Object> delegate) {
640 public ValueWithQName<T> deserialize(final Node<?> input) {
644 QName qname = input.getNodeType();
645 @SuppressWarnings("unchecked")
646 T value = (T) getDelegate().deserialize((Map<QName, Object>) input);
647 return new ValueWithQName<T>(qname, value);
651 public ValueWithQName<T> deserialize(final Node<?> input, final InstanceIdentifier<?> bindingIdentifier) {
655 QName qname = input.getNodeType();
656 @SuppressWarnings("unchecked")
657 T value = (T) getDelegate().deserialize((Map<QName, Object>) input, bindingIdentifier);
658 return new ValueWithQName<T>(qname, value);
662 public CompositeNode serialize(final ValueWithQName<T> input) {
663 return (CompositeNode) super.serialize(input);
667 private interface LocationAwareBindingCodec<P, I> extends BindingCodec<P, I> {
669 boolean isApplicable(InstanceIdentifier<?> location);
671 public Class<?> getDataType();
675 @SuppressWarnings("rawtypes")
676 private abstract class LocationAwareDispatchCodec<T extends LocationAwareBindingCodec> implements BindingCodec {
678 private final Map<Class, T> implementations = Collections.synchronizedMap(new WeakHashMap<Class, T>());
679 private final Set<InstanceIdentifier<?>> adaptedForPaths = new HashSet<>();
681 protected Map<Class, T> getImplementations() {
682 return implementations;
685 protected void addImplementation(final T implementation) {
686 implementations.put(implementation.getDataType(), implementation);
690 public final Object deserialize(final Object input) {
691 throw new UnsupportedOperationException("Invocation of deserialize without Tree location is unsupported");
695 public Object serialize(final Object input) {
696 Preconditions.checkArgument(input instanceof DataContainer);
697 Class<? extends DataContainer> inputType = ((DataContainer) input).getImplementedInterface();
698 T implementation = implementations.get(inputType);
699 if (implementation == null) {
700 implementation = tryToLoadImplementationImpl(inputType);
706 private T tryToLoadImplementationImpl(final Class<? extends DataContainer> inputType) {
707 T implementation = tryToLoadImplementation(inputType);
708 Preconditions.checkArgument(implementation != null, "Data type %s is not supported.", inputType);
709 addImplementation(implementation);
710 return implementation;
713 protected final void adaptForPath(final InstanceIdentifier<?> path) {
714 if (adaptedForPaths.contains(path)) {
718 * We search in schema context if the use of this location aware codec (augmentable codec, case codec)
719 * makes sense on provided location (path)
722 Optional<DataNodeContainer> contextNode = BindingSchemaContextUtils.findDataNodeContainer(currentSchema, path);
724 * If context node is present, this codec makes sense on provided location.
727 if (contextNode.isPresent()) {
728 synchronized (this) {
731 * We adapt (turn on / off) possible implementations of child codecs (augmentations, cases)
732 * based on this location.
736 adaptForPathImpl(path, contextNode.get());
739 * We trigger serialization of instance identifier, to make sure instance identifier
740 * codec is aware of combination of this path / augmentation / case
742 instanceIdentifierCodec.serialize(path);
743 } catch (Exception e) {
744 LOG.warn("Exception during preparation of instance identifier codec for path {}.",path,e);
746 adaptedForPaths.add(path);
749 LOG.debug("Context node (parent node) not found for {}",path);
753 protected abstract T tryToLoadImplementation(Class<? extends DataContainer> inputType);
755 protected abstract void tryToLoadImplementations();
757 protected abstract void adaptForPathImpl(InstanceIdentifier<?> path, DataNodeContainer ctx);
760 @SuppressWarnings("rawtypes")
761 private static class ChoiceCaseCodecImpl<T extends DataContainer> implements ChoiceCaseCodec<T>, //
762 Delegator<BindingCodec>, LocationAwareBindingCodec<Node<?>, ValueWithQName<T>> {
763 private boolean augmenting;
764 private boolean uses;
765 private BindingCodec delegate;
767 private Set<String> validNames;
768 private Set<QName> validQNames;
769 private ChoiceCaseNode schema;
770 private Set<InstanceIdentifier<?>> applicableLocations;
773 public boolean isApplicable(final InstanceIdentifier location) {
774 return applicableLocations.contains(location);
777 public void setSchema(final ChoiceCaseNode caseNode) {
778 this.schema = caseNode;
779 validNames = new HashSet<>();
780 validQNames = new HashSet<>();
781 for (DataSchemaNode node : caseNode.getChildNodes()) {
782 QName qname = node.getQName();
783 validQNames.add(qname);
784 validNames.add(qname.getLocalName());
786 augmenting = caseNode.isAugmenting();
787 uses = caseNode.isAddedByUses();
788 applicableLocations = new HashSet<>();
791 public ChoiceCaseCodecImpl() {
792 this.delegate = NOT_READY_CODEC;
795 public ChoiceCaseCodecImpl(final ChoiceCaseNode caseNode) {
796 this.delegate = NOT_READY_CODEC;
801 public ValueWithQName<T> deserialize(final Node<?> input) {
802 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
806 public ValueWithQName<T> deserialize(final Node<?> input, final InstanceIdentifier<?> bindingIdentifier) {
807 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
811 public CompositeNode serialize(final ValueWithQName<T> input) {
812 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
816 public BindingCodec getDelegate() {
820 public void setDelegate(final BindingCodec delegate) {
821 this.delegate = delegate;
824 public ChoiceCaseNode getSchema() {
829 public boolean isAcceptable(final Node<?> input) {
830 if (input instanceof CompositeNode) {
831 if (augmenting && !uses) {
832 return checkAugmenting((CompositeNode) input);
834 return checkLocal((CompositeNode) input);
840 @SuppressWarnings("deprecation")
841 private boolean checkLocal(final CompositeNode input) {
842 QName parent = input.getNodeType();
843 for (Node<?> childNode : input.getChildren()) {
844 QName child = childNode.getNodeType();
845 if (!Objects.equals(parent.getNamespace(), child.getNamespace())
846 || !Objects.equals(parent.getRevision(), child.getRevision())) {
849 if (validNames.contains(child.getLocalName())) {
856 @SuppressWarnings("deprecation")
857 private boolean checkAugmenting(final CompositeNode input) {
858 for (Node<?> child : input.getChildren()) {
859 if (validQNames.contains(child.getNodeType())) {
867 public Class<?> getDataType() {
868 // TODO Auto-generated method stub
869 throw new UnsupportedOperationException("Not implemented Yet.");
873 private static class PublicChoiceCodecImpl<T> implements ChoiceCodec<T>, Delegator<BindingCodec<Map<QName, Object>, Object>> {
875 private final BindingCodec<Map<QName, Object>, Object> delegate;
877 @SuppressWarnings("rawtypes")
878 private final Map<Class, ChoiceCaseCodecImpl<?>> cases = Collections
879 .synchronizedMap(new WeakHashMap<Class, ChoiceCaseCodecImpl<?>>());
881 private final CaseCompositeNodeMapFacade CompositeToCase;
883 public PublicChoiceCodecImpl(final BindingCodec<Map<QName, Object>, Object> delegate) {
884 this.delegate = delegate;
885 this.CompositeToCase = new CaseCompositeNodeMapFacade(cases);
889 public ValueWithQName<T> deserialize(final Node<?> input) {
890 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
894 public ValueWithQName<T> deserialize(final Node<?> input, final InstanceIdentifier<?> bindingIdentifier) {
895 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
899 public Node<?> serialize(final ValueWithQName<T> input) {
900 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
903 public CaseCompositeNodeMapFacade getCompositeToCase() {
904 return CompositeToCase;
908 public BindingCodec<Map<QName, Object>, Object> getDelegate() {
914 @SuppressWarnings("unused")
915 private class DispatchChoiceCodecImpl extends LocationAwareDispatchCodec<ChoiceCaseCodecImpl<?>> {
918 public Object deserialize(final Object input, @SuppressWarnings("rawtypes") final InstanceIdentifier bindingIdentifier) {
919 // TODO Auto-generated method stub
924 public Object serialize(final Object input) {
925 // TODO Auto-generated method stub
930 protected ChoiceCaseCodecImpl<?> tryToLoadImplementation(final Class<? extends DataContainer> inputType) {
931 return getCaseCodecFor(inputType);
935 protected void tryToLoadImplementations() {
936 // TODO Auto-generated method stub
941 protected void adaptForPathImpl(final InstanceIdentifier<?> path, final DataNodeContainer ctx) {
942 // TODO Auto-generated method stub
947 @SuppressWarnings("rawtypes")
948 private class CaseClassMapFacade extends MapFacadeBase {
951 public Set<Entry<Class, BindingCodec<Object, Object>>> entrySet() {
952 return Collections.emptySet();
956 public BindingCodec get(final Object key) {
957 if (key instanceof Class) {
958 Class cls = (Class) key;
959 // bindingClassEncountered(cls);
960 ChoiceCaseCodecImpl caseCodec = getCaseCodecFor(cls);
961 return caseCodec.getDelegate();
967 @SuppressWarnings("rawtypes")
968 private static class CaseCompositeNodeMapFacade extends MapFacadeBase<CompositeNode> {
970 final Map<Class, ChoiceCaseCodecImpl<?>> choiceCases;
972 public CaseCompositeNodeMapFacade(final Map<Class, ChoiceCaseCodecImpl<?>> choiceCases) {
973 this.choiceCases = choiceCases;
977 public BindingCodec get(final Object key) {
978 if (!(key instanceof CompositeNode)) {
981 for (Entry<Class, ChoiceCaseCodecImpl<?>> entry : choiceCases.entrySet()) {
982 ChoiceCaseCodecImpl<?> codec = entry.getValue();
983 if (codec.isAcceptable((CompositeNode) key)) {
984 return codec.getDelegate();
993 * This map is used as only facade for
994 * {@link org.opendaylight.yangtools.yang.binding.BindingCodec} in different
995 * classloaders to retrieve codec dynamicly based on provided key.
1000 @SuppressWarnings("rawtypes")
1001 private abstract static class MapFacadeBase<T> implements Map<T, BindingCodec<?, ?>> {
1004 public boolean containsKey(final Object key) {
1005 return get(key) != null;
1009 public void clear() {
1010 throw notModifiable();
1014 public boolean equals(final Object obj) {
1015 return super.equals(obj);
1019 public BindingCodec remove(final Object key) {
1029 public Collection<BindingCodec<?, ?>> values() {
1030 return Collections.emptySet();
1033 private UnsupportedOperationException notModifiable() {
1034 return new UnsupportedOperationException("Not externally modifiable.");
1038 public BindingCodec<Map<QName, Object>, Object> put(final T key, final BindingCodec<?, ?> value) {
1039 throw notModifiable();
1043 public void putAll(final Map<? extends T, ? extends BindingCodec<?, ?>> m) {
1044 throw notModifiable();
1048 public int hashCode() {
1049 return super.hashCode();
1053 public boolean isEmpty() {
1058 public Set<T> keySet() {
1059 return Collections.emptySet();
1063 public Set<Entry<T, BindingCodec<?, ?>>> entrySet() {
1064 return Collections.emptySet();
1068 public boolean containsValue(final Object value) {
1073 @SuppressWarnings({ "rawtypes", "unchecked" })
1074 private class AugmentableDispatchCodec extends LocationAwareDispatchCodec<AugmentationCodecWrapper> {
1076 private final Class augmentableType;
1078 public AugmentableDispatchCodec(final Class type) {
1079 Preconditions.checkArgument(Augmentable.class.isAssignableFrom(type));
1080 augmentableType = type;
1084 // TODO deprecate use without iid
1085 public Object serialize(final Object input) {
1086 if (input instanceof Augmentable<?>) {
1087 Map<Class, Augmentation> augmentations = getAugmentations(input);
1088 return serializeImpl(augmentations);
1093 private Map<Class, Augmentation> getAugmentations(final Object input) {
1094 Field augmentationField;
1096 augmentationField = input.getClass().getDeclaredField("augmentation");
1097 augmentationField.setAccessible(true);
1098 Map<Class, Augmentation> augMap = (Map<Class, Augmentation>) augmentationField.get(input);
1100 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
1101 LOG.debug("Could not read augmentations for {}", input, e);
1103 return Collections.emptyMap();
1106 @SuppressWarnings("deprecation")
1107 private List serializeImpl(final Map<Class, Augmentation> input) {
1108 List ret = new ArrayList<>();
1109 for (Entry<Class, Augmentation> entry : input.entrySet()) {
1110 AugmentationCodec codec = getCodecForAugmentation(entry.getKey());
1111 CompositeNode node = codec.serialize(new ValueWithQName(null, entry.getValue()));
1112 ret.addAll(node.getChildren());
1118 public Map<Class, Augmentation> deserialize(final Object input, final InstanceIdentifier path) {
1120 Map<Class, Augmentation> ret = new HashMap<>();
1122 if (input instanceof CompositeNode) {
1123 List<Entry<Class, AugmentationCodecWrapper>> codecs = new ArrayList<>(getImplementations().entrySet());
1124 for (Entry<Class, AugmentationCodecWrapper> codec : codecs) {
1125 AugmentationCodec<?> ac = codec.getValue();
1126 if (ac.isAcceptable(path)) {
1127 // We add Augmentation Identifier to path, in order to
1128 // correctly identify children.
1129 InstanceIdentifier augmentPath = path.builder().augmentation(codec.getKey()).build();
1130 ValueWithQName<?> value = codec.getValue().deserialize((CompositeNode) input, augmentPath);
1131 if (value != null && value.getValue() != null) {
1132 ret.put(codec.getKey(), (Augmentation) value.getValue());
1140 protected Optional<AugmentationCodecWrapper> tryToLoadImplementation(final Type potential) {
1142 Class<? extends Augmentation<?>> clazz = (Class<? extends Augmentation<?>>) classLoadingStrategy
1143 .loadClass(potential);
1144 return Optional.of(tryToLoadImplementation(clazz));
1145 } catch (ClassNotFoundException e) {
1146 LOG.warn("Failed to find class for augmentation of {}, reason: {}", potential, e.toString());
1148 return Optional.absent();
1152 protected AugmentationCodecWrapper tryToLoadImplementation(final Class inputType) {
1153 AugmentationCodecWrapper<? extends Augmentation<?>> potentialImpl = getCodecForAugmentation(inputType);
1154 addImplementation(potentialImpl);
1155 return potentialImpl;
1160 protected void tryToLoadImplementations() {
1161 Type type = referencedType(augmentableType);
1162 Collection<Type> potentialAugmentations;
1163 synchronized(augmentableToAugmentations) {
1164 potentialAugmentations = new ArrayList(augmentableToAugmentations.get(type));
1166 for (Type potential : potentialAugmentations) {
1168 tryToLoadImplementation(potential);
1169 } catch (CodeGenerationException e) {
1170 LOG.warn("Failed to proactively generate augment coded for {}, reason: {}", type, e.toString());
1177 protected void adaptForPathImpl(final InstanceIdentifier<?> path, final DataNodeContainer ctxNode) {
1178 if (ctxNode instanceof AugmentationTarget) {
1179 Set<AugmentationSchema> availableAugmentations = ((AugmentationTarget) ctxNode)
1180 .getAvailableAugmentations();
1181 if (!availableAugmentations.isEmpty()) {
1182 updateAugmentationMapping(path,availableAugmentations);
1188 private void updateAugmentationMapping(final InstanceIdentifier<?> path, final Set<AugmentationSchema> availableAugmentations) {
1189 for (AugmentationSchema aug : availableAugmentations) {
1191 Type potentialType = getTypeForAugmentation(aug);
1192 if (potentialType != null) {
1193 Optional<AugmentationCodecWrapper> potentialImpl = tryToLoadImplementation(potentialType);
1194 if (potentialImpl.isPresent()) {
1195 potentialImpl.get().addApplicableFor(path,aug);
1198 LOG.warn("Could not find generated type for augmentation {} with childs {}.",aug,aug.getChildNodes());
1201 availableAugmentations.toString();
1204 private Type getTypeForAugmentation(final AugmentationSchema aug) {
1205 Optional<AugmentationSchema> currentAug = Optional.of(aug);
1206 while(currentAug.isPresent()) {
1207 Type potentialType = typeToAugment.inverse().get(currentAug.get());
1208 if(potentialType != null) {
1209 return potentialType;
1211 currentAug = currentAug.get().getOriginalDefinition();
1217 @SuppressWarnings({ "rawtypes", "unchecked" })
1218 private static class LateMixinCodec implements BindingCodec, Delegator<BindingCodec> {
1220 private BindingCodec delegate;
1223 public BindingCodec getDelegate() {
1224 if (delegate == null) {
1225 throw new IllegalStateException("Codec not initialized yet.");
1231 public Object deserialize(final Object input) {
1232 return getDelegate().deserialize(input);
1236 public Object deserialize(final Object input, final InstanceIdentifier bindingIdentifier) {
1237 return getDelegate().deserialize(input, bindingIdentifier);
1241 public Object serialize(final Object input) {
1242 return getDelegate().serialize(input);
1247 @SuppressWarnings("rawtypes")
1248 private static class AugmentationCodecWrapper<T extends Augmentation<?>> implements AugmentationCodec<T>,
1249 Delegator<BindingCodec>, LocationAwareBindingCodec<Node<?>, ValueWithQName<T>> {
1251 private final BindingCodec delegate;
1252 private final QName augmentationQName;
1253 private final Multimap<InstanceIdentifier<?>,QName> validAugmentationTargets;
1254 private final Class<?> augmentationType;
1256 public AugmentationCodecWrapper(final BindingCodec<Map<QName, Object>, Object> rawCodec,
1257 final InstanceIdentifier<?> targetId, final Class<?> dataType) {
1258 this.delegate = rawCodec;
1259 this.augmentationType = dataType;
1260 this.augmentationQName = BindingReflections.findQName(rawCodec.getClass());
1261 this.validAugmentationTargets = Multimaps.synchronizedSetMultimap(HashMultimap.<InstanceIdentifier<?>,QName>create());
1264 public void addApplicableFor(final InstanceIdentifier<?> path, final AugmentationSchema aug) {
1265 for(DataSchemaNode child : aug.getChildNodes()) {
1266 validAugmentationTargets.put(path,child.getQName());
1271 public BindingCodec getDelegate() {
1276 public CompositeNode serialize(final ValueWithQName<T> input) {
1277 @SuppressWarnings("unchecked")
1278 List<Map<QName, Object>> rawValues = (List<Map<QName, Object>>) getDelegate().serialize(input);
1279 List<Node<?>> serialized = new ArrayList<>(rawValues.size());
1280 for (Map<QName, Object> val : rawValues) {
1281 serialized.add(IntermediateMapping.toNode(val));
1283 return new CompositeNodeTOImpl(input.getQname(), null, serialized);
1287 @SuppressWarnings("unchecked")
1288 public ValueWithQName<T> deserialize(final Node<?> input) {
1289 Object rawCodecValue = getDelegate().deserialize(input);
1290 return new ValueWithQName<T>(input.getNodeType(), (T) rawCodecValue);
1294 @SuppressWarnings("unchecked")
1295 public ValueWithQName<T> deserialize(final Node<?> input, final InstanceIdentifier<?> bindingIdentifier) {
1296 // if (!isAcceptable(bindingIdentifier)) {
1299 Object rawCodecValue = getDelegate().deserialize(input, bindingIdentifier);
1300 return new ValueWithQName<T>(input.getNodeType(), (T) rawCodecValue);
1304 public QName getAugmentationQName() {
1305 return augmentationQName;
1309 public boolean isAcceptable(final InstanceIdentifier<?> path) {
1313 return validAugmentationTargets.containsKey(path);
1317 public boolean isApplicable(final InstanceIdentifier location) {
1318 return isAcceptable(location);
1322 public Class<?> getDataType() {
1323 return augmentationType;
1327 @SuppressWarnings("rawtypes")
1328 private class IdentityCompositeCodec implements IdentityCodec {
1331 public Object deserialize(final Object input) {
1332 Preconditions.checkArgument(input instanceof QName);
1333 return deserialize((QName) input);
1337 public Class<?> deserialize(final QName input) {
1338 Type type = qnamesToIdentityMap.get(input);
1342 ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
1343 WeakReference<Class> softref = typeToClass.get(typeref);
1344 if (softref == null) {
1347 Class<?> cls = classLoadingStrategy.loadClass(typeref.getFullyQualifiedName());
1352 } catch (Exception e) {
1353 LOG.warn("Identity {} was not deserialized, because of missing class {}", input,
1354 typeref.getFullyQualifiedName());
1358 return softref.get();
1362 public Object deserialize(final Object input,final InstanceIdentifier bindingIdentifier) {
1363 Type type = qnamesToIdentityMap.get(input);
1367 ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
1368 WeakReference<Class> softref = typeToClass.get(typeref);
1369 if (softref == null) {
1372 Class<?> cls = classLoadingStrategy.loadClass(typeref.getFullyQualifiedName());
1377 } catch (Exception e) {
1378 LOG.warn("Identity {} was not deserialized, because of missing class {}", input,
1379 typeref.getFullyQualifiedName());
1383 return softref.get();
1387 public QName serialize(final Class input) {
1388 Preconditions.checkArgument(BaseIdentity.class.isAssignableFrom(input));
1389 bindingClassEncountered(input);
1390 QName qname = identityQNames.get(input);
1391 if (qname != null) {
1394 ConcreteType typeref = Types.typeForClass(input);
1395 qname = typeToQname.get(typeref);
1396 if (qname != null) {
1397 identityQNames.put(input, qname);
1403 public Object serialize(final Object input) {
1404 Preconditions.checkArgument(input instanceof Class);
1405 return serialize((Class) input);
1410 public boolean isCodecAvailable(final Class<?> cls) {
1411 // FIXME: enforce type?
1412 // Preconditions.checkArgument(DataContainer.class.isAssignableFrom(cls));
1414 if (containerCodecs.containsKey(cls)) {
1417 if (identifierCodecs.containsKey(cls)) {
1420 if (choiceCodecs.containsKey(cls)) {
1423 if (caseCodecs.containsKey(cls)) {
1426 if (augmentableCodecs.containsKey(cls)) {
1429 if (augmentationCodecs.containsKey(cls)) {
1435 public static final Type referencedType(final Class<?> augmentableType) {
1436 return new ReferencedTypeImpl(augmentableType.getPackage().getName(), augmentableType.getSimpleName());