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.lang.reflect.ParameterizedType;
14 import java.util.Map.Entry;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
19 import org.opendaylight.yangtools.sal.binding.generator.util.ClassLoaderUtils;
20 import org.opendaylight.yangtools.yang.data.impl.codec.*;
21 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
22 import org.opendaylight.yangtools.binding.generator.util.Types;
23 import org.opendaylight.yangtools.concepts.Delegator;
24 import org.opendaylight.yangtools.concepts.Identifiable;
25 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
26 import org.opendaylight.yangtools.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
28 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
29 import org.opendaylight.yangtools.yang.binding.*;
30 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.data.api.Node;
34 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
35 import org.opendaylight.yangtools.yang.model.api.*;
36 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.base.Preconditions;
42 public class LazyGeneratedCodecRegistry implements //
44 SchemaServiceListener, //
47 private final static Logger LOG = LoggerFactory.getLogger(LazyGeneratedCodecRegistry.class);
48 private final static LateMixinCodec NOT_READY_CODEC = new LateMixinCodec();
50 private final InstanceIdentifierCodec instanceIdentifierCodec = new InstanceIdentifierCodecImpl(this);
51 private final IdentityCompositeCodec identityRefCodec = new IdentityCompositeCodec();
53 private TransformerGenerator generator;
55 // Concrete class to codecs
56 private static final Map<Class<?>, DataContainerCodec<?>> containerCodecs = Collections.synchronizedMap(new WeakHashMap<Class<?>, DataContainerCodec<?>>());
57 private static final Map<Class<?>, IdentifierCodec<?>> identifierCodecs = Collections.synchronizedMap(new WeakHashMap<Class<?>, IdentifierCodec<?>>());
58 private static final Map<Class<?>, ChoiceCodecImpl<?>> choiceCodecs = Collections.synchronizedMap(new WeakHashMap<Class<?>, ChoiceCodecImpl<?>>());
59 private static final Map<Class<?>, ChoiceCaseCodecImpl<?>> caseCodecs = Collections.synchronizedMap(new WeakHashMap<Class<?>, ChoiceCaseCodecImpl<?>>());
60 private static final Map<Class<?>, AugmentableCompositeCodec> augmentableCodecs = Collections.synchronizedMap(new WeakHashMap<Class<?>, AugmentableCompositeCodec>());
61 private static final Map<Class<?>, AugmentationCodec<?>> augmentationCodecs = Collections.synchronizedMap(new WeakHashMap<Class<?>, AugmentationCodec<?>>());
62 private static final Map<Class<?>, QName> identityQNames = Collections.synchronizedMap(new WeakHashMap<Class<?>, QName>());
63 private static final Map<QName, Type> qnamesToIdentityMap = new ConcurrentHashMap<>();
64 /** Binding type to encountered classes mapping **/
65 @SuppressWarnings("rawtypes")
66 private static final Map<Type, WeakReference<Class>> typeToClass = new ConcurrentHashMap<>();
68 @SuppressWarnings("rawtypes")
69 private static final ConcurrentMap<Type, ChoiceCaseCodecImpl> typeToCaseCodecs = new ConcurrentHashMap<>();
71 private CaseClassMapFacade classToCaseRawCodec = new CaseClassMapFacade();
73 private static final Map<SchemaPath, GeneratedTypeBuilder> pathToType = new ConcurrentHashMap<>();
74 private static final Map<List<QName>, Type> pathToInstantiatedType = new ConcurrentHashMap<>();
75 private static final Map<Type, QName> typeToQname = new ConcurrentHashMap<>();
77 private final SchemaLock lock;
79 private SchemaContext currentSchema;
81 LazyGeneratedCodecRegistry(SchemaLock lock) {
82 this.lock = Preconditions.checkNotNull(lock);
85 public SchemaLock getLock() {
89 public TransformerGenerator getGenerator() {
93 public void setGenerator(TransformerGenerator generator) {
94 this.generator = generator;
98 public InstanceIdentifierCodec getInstanceIdentifierCodec() {
99 return instanceIdentifierCodec;
103 public <T extends Augmentation<?>> AugmentationCodec<T> getCodecForAugmentation(Class<T> object) {
104 AugmentationCodec<T> codec = null;
105 @SuppressWarnings("rawtypes")
106 AugmentationCodec potentialCodec = augmentationCodecs.get(object);
107 if (potentialCodec != null) {
108 codec = potentialCodec;
111 lock.waitForSchema(object);
112 Class<? extends BindingCodec<Map<QName, Object>, Object>> augmentRawCodec = generator
113 .augmentationTransformerFor(object);
114 BindingCodec<Map<QName, Object>, Object> rawCodec = augmentRawCodec.newInstance();
115 codec = new AugmentationCodecWrapper<T>(rawCodec);
116 augmentationCodecs.put(augmentRawCodec, codec);
117 } catch (InstantiationException e) {
118 LOG.error("Can not instantiate raw augmentation codec {}", object.getSimpleName(), e);
119 } catch (IllegalAccessException e) {
120 LOG.debug("BUG: Constructor for {} is not accessible.", object.getSimpleName(), e);
122 Class<? extends Augmentable<?>> objectSupertype = getAugmentableArgumentFrom(object);
123 if (objectSupertype != null) {
124 getAugmentableCodec(objectSupertype).addAugmentationCodec(object, codec);
126 LOG.warn("Could not find augmentation target for augmentation {}", object);
132 public QName getQNameForAugmentation(Class<?> cls) {
133 Preconditions.checkArgument(Augmentation.class.isAssignableFrom(cls));
134 return getCodecForAugmentation((Class<? extends Augmentation>)cls).getAugmentationQName();
137 private static Class<? extends Augmentable<?>> getAugmentableArgumentFrom(
138 final Class<? extends Augmentation<?>> augmentation) {
140 Class<? extends Augmentable<?>> ret = ClassLoaderUtils.withClassLoader(augmentation.getClassLoader(),
141 new Callable<Class<? extends Augmentable<?>>>() {
143 @SuppressWarnings("unchecked")
144 public Class<? extends Augmentable<?>> call() throws Exception {
145 for (java.lang.reflect.Type supertype : augmentation.getGenericInterfaces()) {
146 if (supertype instanceof ParameterizedType
147 && Augmentation.class.equals(((ParameterizedType) supertype).getRawType())) {
148 ParameterizedType augmentationGeneric = (ParameterizedType) supertype;
149 return (Class<? extends Augmentable<?>>) augmentationGeneric
150 .getActualTypeArguments()[0];
157 } catch (Exception e) {
158 LOG.debug("Could not find augmentable for {} using {}", augmentation, augmentation.getClassLoader(), e);
164 public Class<?> getClassForPath(List<QName> names) {
165 DataSchemaNode node = getSchemaNode(names);
166 SchemaPath path = node.getPath();
167 Type type = pathToType.get(path);
169 type = new ReferencedTypeImpl(type.getPackageName(), type.getName());
171 type = pathToInstantiatedType.get(names);
173 @SuppressWarnings("rawtypes")
174 WeakReference<Class> weakRef = typeToClass.get(type);
175 if (weakRef == null) {
176 LOG.error("Could not find loaded class for path: {} and type: {}", path, type.getFullyQualifiedName());
178 return weakRef.get();
182 public void putPathToClass(List<QName> names, Class<?> cls) {
183 Type reference = Types.typeForClass(cls);
184 pathToInstantiatedType.put(names, reference);
185 bindingClassEncountered(cls);
189 public IdentifierCodec<?> getKeyCodecForPath(List<QName> names) {
190 @SuppressWarnings("unchecked")
191 Class<? extends Identifiable<?>> cls = (Class<? extends Identifiable<?>>) getClassForPath(names);
192 return getIdentifierCodecForIdentifiable(cls);
196 public <T extends DataContainer> DataContainerCodec<T> getCodecForDataObject(Class<T> type) {
197 @SuppressWarnings("unchecked")
198 DataContainerCodec<T> ret = (DataContainerCodec<T>) containerCodecs.get(type);
202 Class<? extends BindingCodec<Map<QName, Object>, Object>> newType = generator.transformerFor(type);
203 BindingCodec<Map<QName, Object>, Object> rawCodec = newInstanceOf(newType);
204 DataContainerCodecImpl<T> newWrapper = new DataContainerCodecImpl<>(rawCodec);
205 containerCodecs.put(type, newWrapper);
209 @SuppressWarnings("rawtypes")
210 public void bindingClassEncountered(Class cls) {
212 ConcreteType typeRef = Types.typeForClass(cls);
213 if (typeToClass.containsKey(typeRef)) {
216 LOG.trace("Binding Class {} encountered.", cls);
217 WeakReference<Class> weakRef = new WeakReference<>(cls);
218 typeToClass.put(typeRef, weakRef);
219 if (Augmentation.class.isAssignableFrom(cls)) {
221 } else if (DataObject.class.isAssignableFrom(cls)) {
222 @SuppressWarnings({ "unchecked", "unused" })
223 Object cdc = getCodecForDataObject((Class<? extends DataObject>) cls);
228 public void onClassProcessed(Class<?> cls) {
229 ConcreteType typeRef = Types.typeForClass(cls);
230 if (typeToClass.containsKey(typeRef)) {
233 LOG.trace("Binding Class {} encountered.", cls);
234 WeakReference<Class> weakRef = new WeakReference<>((Class) cls);
235 typeToClass.put(typeRef, weakRef);
238 private DataSchemaNode getSchemaNode(List<QName> path) {
239 QName firstNode = path.get(0);
240 DataNodeContainer previous = currentSchema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
241 firstNode.getRevision());
242 Iterator<QName> iterator = path.iterator();
243 while (iterator.hasNext()) {
244 QName arg = iterator.next();
245 DataSchemaNode currentNode = previous.getDataChildByName(arg);
246 if (currentNode == null && previous instanceof DataNodeContainer) {
247 currentNode = searchInChoices(previous, arg);
249 if (currentNode instanceof DataNodeContainer) {
250 previous = (DataNodeContainer) currentNode;
251 } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
252 Preconditions.checkState(!iterator.hasNext(), "Path tries to nest inside leaf node.");
256 return (DataSchemaNode) previous;
259 private DataSchemaNode searchInChoices(DataNodeContainer node, QName arg) {
260 Set<DataSchemaNode> children = node.getChildNodes();
261 for (DataSchemaNode child : children) {
262 if (child instanceof ChoiceNode) {
263 ChoiceNode choiceNode = (ChoiceNode) child;
264 DataSchemaNode potential = searchInCases(choiceNode, arg);
265 if (potential != null) {
273 private DataSchemaNode searchInCases(ChoiceNode choiceNode, QName arg) {
274 Set<ChoiceCaseNode> cases = choiceNode.getCases();
275 for (ChoiceCaseNode caseNode : cases) {
276 DataSchemaNode node = caseNode.getDataChildByName(arg);
284 private <T> T newInstanceOf(Class<?> newType) {
286 @SuppressWarnings("unchecked")
287 T ret = (T) newType.newInstance();
289 } catch (InstantiationException e) {
290 throw new IllegalStateException(e);
291 } catch (IllegalAccessException e) {
292 throw new IllegalStateException(e);
297 public <T extends Identifiable<?>> IdentifierCodec<?> getIdentifierCodecForIdentifiable(Class<T> type) {
298 IdentifierCodec<?> obj = identifierCodecs.get(type);
302 Class<? extends BindingCodec<Map<QName, Object>, Object>> newCodec = generator
303 .keyTransformerForIdentifiable(type);
304 BindingCodec<Map<QName, Object>, Object> newInstance;
305 newInstance = newInstanceOf(newCodec);
306 IdentifierCodecImpl<?> newWrapper = new IdentifierCodecImpl<>(newInstance);
307 identifierCodecs.put(type, newWrapper);
312 public IdentityCodec<?> getIdentityCodec() {
313 return identityRefCodec;
317 public <T extends BaseIdentity> IdentityCodec<T> getCodecForIdentity(Class<T> codec) {
318 bindingClassEncountered(codec);
319 return identityRefCodec;
323 public void onCodecCreated(Class<?> cls) {
324 CodecMapping.setIdentifierCodec(cls, instanceIdentifierCodec);
325 CodecMapping.setIdentityRefCodec(cls, identityRefCodec);
329 public <T extends Identifier<?>> IdentifierCodec<T> getCodecForIdentifier(Class<T> object) {
330 @SuppressWarnings("unchecked")
331 IdentifierCodec<T> obj = (IdentifierCodec<T>) identifierCodecs.get(object);
335 Class<? extends BindingCodec<Map<QName, Object>, Object>> newCodec = generator
336 .keyTransformerForIdentifier(object);
337 BindingCodec<Map<QName, Object>, Object> newInstance;
338 newInstance = newInstanceOf(newCodec);
339 IdentifierCodecImpl<T> newWrapper = new IdentifierCodecImpl<>(newInstance);
340 identifierCodecs.put(object, newWrapper);
344 @SuppressWarnings("rawtypes")
345 public ChoiceCaseCodecImpl getCaseCodecFor(Class caseClass) {
346 ChoiceCaseCodecImpl<?> potential = caseCodecs.get(caseClass);
347 if (potential != null) {
350 ConcreteType typeref = Types.typeForClass(caseClass);
351 ChoiceCaseCodecImpl caseCodec = typeToCaseCodecs.get(typeref);
353 Preconditions.checkState(caseCodec != null, "Case Codec was not created proactivelly for %s", caseClass.getName());
354 Preconditions.checkState(caseCodec.getSchema() != null, "Case schema is not available for %s", caseClass.getName());
355 @SuppressWarnings("unchecked")
356 Class<? extends BindingCodec> newCodec = generator.caseCodecFor(caseClass, caseCodec.getSchema());
357 BindingCodec newInstance = newInstanceOf(newCodec);
358 caseCodec.setDelegate(newInstance);
359 caseCodecs.put(caseClass, caseCodec);
361 for (Entry<Class<?>, ChoiceCodecImpl<?>> choice : choiceCodecs.entrySet()) {
362 if (choice.getKey().isAssignableFrom(caseClass)) {
363 choice.getValue().cases.put(caseClass, caseCodec);
369 public void onModuleContextAdded(SchemaContext schemaContext, Module module, ModuleContext context) {
370 pathToType.putAll(context.getChildNodes());
371 qnamesToIdentityMap.putAll(context.getIdentities());
372 for (Entry<QName, GeneratedTOBuilder> identity : context.getIdentities().entrySet()) {
374 new ReferencedTypeImpl(identity.getValue().getPackageName(), identity.getValue().getName()),
377 captureCases(context.getCases(), schemaContext);
380 private void captureCases(Map<SchemaPath, GeneratedTypeBuilder> cases, SchemaContext module) {
381 for (Entry<SchemaPath, GeneratedTypeBuilder> caseNode : cases.entrySet()) {
382 ReferencedTypeImpl typeref = new ReferencedTypeImpl(caseNode.getValue().getPackageName(), caseNode
383 .getValue().getName());
385 pathToType.put(caseNode.getKey(), caseNode.getValue());
387 ChoiceCaseNode node = (ChoiceCaseNode) SchemaContextUtil.findDataSchemaNode(module, caseNode.getKey());
390 LOG.error("YANGTools Bug: SchemaNode for {}, with path {} was not found in context.",
391 typeref.getFullyQualifiedName(), caseNode.getKey());
392 @SuppressWarnings("rawtypes")
393 ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl();
394 typeToCaseCodecs.putIfAbsent(typeref, value);
397 @SuppressWarnings("rawtypes")
398 ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl(node);
399 typeToCaseCodecs.putIfAbsent(typeref, value);
404 public void onGlobalContextUpdated(SchemaContext context) {
405 currentSchema = context;
408 @SuppressWarnings({ "unchecked", "rawtypes" })
410 public void onChoiceCodecCreated(Class<?> choiceClass,
411 Class<? extends BindingCodec<Map<QName, Object>, Object>> choiceCodec, ChoiceNode schema) {
412 ChoiceCodec<?> oldCodec = choiceCodecs.get(choiceClass);
413 Preconditions.checkState(oldCodec == null);
414 BindingCodec<Map<QName, Object>, Object> delegate = newInstanceOf(choiceCodec);
415 ChoiceCodecImpl<?> newCodec = new ChoiceCodecImpl(delegate);
416 choiceCodecs.put(choiceClass, newCodec);
417 CodecMapping.setClassToCaseMap(choiceCodec, (Map<Class<?>, BindingCodec<?, ?>>) classToCaseRawCodec);
418 CodecMapping.setCompositeNodeToCaseMap(choiceCodec, newCodec.getCompositeToCase());
420 tryToCreateCasesCodecs(schema);
424 private void tryToCreateCasesCodecs(ChoiceNode schema) {
425 for (ChoiceCaseNode caseNode : schema.getCases()) {
426 SchemaPath path = caseNode.getPath();
427 GeneratedTypeBuilder type;
428 if (path != null && (type = pathToType.get(path)) != null) {
429 ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
430 ChoiceCaseCodecImpl partialCodec = typeToCaseCodecs.get(typeref);
431 if (partialCodec.getSchema() == null) {
432 partialCodec.setSchema(caseNode);
435 Class<?> caseClass = ClassLoaderUtils.tryToLoadClassWithTCCL(type.getFullyQualifiedName());
436 if (caseClass != null) {
437 getCaseCodecFor(caseClass);
445 public void onValueCodecCreated(Class<?> valueClass, Class<?> valueCodec) {
449 public void onCaseCodecCreated(Class<?> choiceClass,
450 Class<? extends BindingCodec<Map<QName, Object>, Object>> choiceCodec) {
454 public void onDataContainerCodecCreated(Class<?> dataClass, Class<? extends BindingCodec<?, ?>> dataCodec) {
455 if (Augmentable.class.isAssignableFrom(dataClass)) {
456 AugmentableCompositeCodec augmentableCodec = getAugmentableCodec(dataClass);
457 CodecMapping.setAugmentationCodec(dataCodec, augmentableCodec);
462 public AugmentableCompositeCodec getAugmentableCodec(Class<?> dataClass) {
463 AugmentableCompositeCodec ret = augmentableCodecs.get(dataClass);
467 ret = new AugmentableCompositeCodec(dataClass);
468 augmentableCodecs.put(dataClass, ret);
472 private static abstract class IntermediateCodec<T> implements //
473 DomCodec<T>, Delegator<BindingCodec<Map<QName, Object>, Object>> {
475 private final BindingCodec<Map<QName, Object>, Object> delegate;
478 public BindingCodec<Map<QName, Object>, Object> getDelegate() {
482 public IntermediateCodec(BindingCodec<Map<QName, Object>, Object> delegate) {
483 this.delegate = delegate;
487 public Node<?> serialize(ValueWithQName<T> input) {
488 Map<QName, Object> intermediateOutput = delegate.serialize(input);
489 return IntermediateMapping.toNode(intermediateOutput);
493 private static class IdentifierCodecImpl<T extends Identifier<?>> //
494 extends IntermediateCodec<T> //
495 implements IdentifierCodec<T> {
497 public IdentifierCodecImpl(BindingCodec<Map<QName, Object>, Object> delegate) {
502 public ValueWithQName<T> deserialize(Node<?> input) {
503 QName qname = input.getNodeType();
504 @SuppressWarnings("unchecked")
505 T value = (T) getDelegate().deserialize((Map<QName, Object>) input);
506 return new ValueWithQName<T>(qname, value);
510 public CompositeNode serialize(ValueWithQName<T> input) {
511 return (CompositeNode) super.serialize(input);
515 private static class DataContainerCodecImpl<T extends DataContainer> //
516 extends IntermediateCodec<T> //
517 implements DataContainerCodec<T> {
519 public DataContainerCodecImpl(BindingCodec<Map<QName, Object>, Object> delegate) {
524 public ValueWithQName<T> deserialize(Node<?> input) {
528 QName qname = input.getNodeType();
529 @SuppressWarnings("unchecked")
530 T value = (T) getDelegate().deserialize((Map<QName, Object>) input);
531 return new ValueWithQName<T>(qname, value);
535 public CompositeNode serialize(ValueWithQName<T> input) {
536 return (CompositeNode) super.serialize(input);
540 @SuppressWarnings("rawtypes")
541 private static class ChoiceCaseCodecImpl<T extends DataContainer> implements ChoiceCaseCodec<T>, //
542 Delegator<BindingCodec> {
543 private boolean augmenting;
544 private BindingCodec delegate;
546 private Set<String> validNames;
547 private Set<QName> validQNames;
548 private ChoiceCaseNode schema;
550 public void setSchema(ChoiceCaseNode caseNode) {
551 this.schema = schema;
552 this.schema = caseNode;
553 validNames = new HashSet<>();
554 validQNames = new HashSet<>();
555 for (DataSchemaNode node : caseNode.getChildNodes()) {
556 QName qname = node.getQName();
557 validQNames.add(qname);
558 validNames.add(qname.getLocalName());
560 augmenting = caseNode.isAugmenting();
563 public ChoiceCaseCodecImpl() {
564 this.delegate = NOT_READY_CODEC;
567 public ChoiceCaseCodecImpl(ChoiceCaseNode caseNode) {
568 this.delegate = NOT_READY_CODEC;
573 public ValueWithQName<T> deserialize(Node<?> input) {
574 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
578 public CompositeNode serialize(ValueWithQName<T> input) {
579 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
582 public BindingCodec getDelegate() {
586 public void setDelegate(BindingCodec delegate) {
587 this.delegate = delegate;
590 public ChoiceCaseNode getSchema() {
595 public boolean isAcceptable(Node<?> input) {
596 if (input instanceof CompositeNode) {
598 return checkAugmenting((CompositeNode) input);
600 return checkLocal((CompositeNode) input);
606 private boolean checkLocal(CompositeNode input) {
607 QName parent = input.getNodeType();
608 for (Node<?> childNode : input.getChildren()) {
609 QName child = childNode.getNodeType();
610 if (!Objects.equals(parent.getNamespace(), child.getNamespace())
611 || !Objects.equals(parent.getRevision(), child.getRevision())) {
614 if (validNames.contains(child.getLocalName())) {
621 private boolean checkAugmenting(CompositeNode input) {
622 for (Node<?> child : input.getChildren()) {
623 if (validQNames.contains(child.getNodeType())) {
631 private static class ChoiceCodecImpl<T> implements ChoiceCodec<T> {
633 private final BindingCodec<Map<QName, Object>, Object> delegate;
635 @SuppressWarnings("rawtypes")
636 private final Map<Class, ChoiceCaseCodecImpl<?>> cases = Collections.synchronizedMap(new WeakHashMap<Class, ChoiceCaseCodecImpl<?>>());
638 private final CaseCompositeNodeMapFacade CompositeToCase;
640 public ChoiceCodecImpl(BindingCodec<Map<QName, Object>, Object> delegate) {
641 this.delegate = delegate;
642 this.CompositeToCase = new CaseCompositeNodeMapFacade(cases);
646 public ValueWithQName<T> deserialize(Node<?> input) {
647 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
651 public Node<?> serialize(ValueWithQName<T> input) {
652 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
655 public CaseCompositeNodeMapFacade getCompositeToCase() {
656 return CompositeToCase;
659 public Map<Class, ChoiceCaseCodecImpl<?>> getCases() {
663 public BindingCodec<Map<QName, Object>, Object> getDelegate() {
669 @SuppressWarnings("rawtypes")
670 private class CaseClassMapFacade extends MapFacadeBase {
673 public Set<Entry<Class, BindingCodec<Object, Object>>> entrySet() {
674 return Collections.emptySet();
678 public BindingCodec get(Object key) {
679 if (key instanceof Class) {
680 Class cls = (Class) key;
681 // bindingClassEncountered(cls);
682 ChoiceCaseCodecImpl caseCodec = getCaseCodecFor(cls);
683 return caseCodec.getDelegate();
689 @SuppressWarnings("rawtypes")
690 private static class CaseCompositeNodeMapFacade extends MapFacadeBase<CompositeNode> {
692 final Map<Class, ChoiceCaseCodecImpl<?>> choiceCases;
694 public CaseCompositeNodeMapFacade(Map<Class, ChoiceCaseCodecImpl<?>> choiceCases) {
695 this.choiceCases = choiceCases;
699 public BindingCodec get(Object key) {
700 if (!(key instanceof CompositeNode)) {
703 for (Entry<Class, ChoiceCaseCodecImpl<?>> entry : choiceCases.entrySet()) {
704 ChoiceCaseCodecImpl<?> codec = entry.getValue();
705 if (codec.isAcceptable((CompositeNode) key)) {
706 return codec.getDelegate();
715 * This map is used as only facade for {@link org.opendaylight.yangtools.yang.binding.BindingCodec} in different
716 * classloaders to retrieve codec dynamicly based on provided key.
721 @SuppressWarnings("rawtypes")
722 private static abstract class MapFacadeBase<T> implements Map<T, BindingCodec<?, ?>> {
725 public boolean containsKey(Object key) {
726 return get(key) != null;
730 public void clear() {
731 throw notModifiable();
735 public boolean equals(Object obj) {
736 return super.equals(obj);
740 public BindingCodec remove(Object key) {
750 public Collection<BindingCodec<?, ?>> values() {
751 return Collections.emptySet();
754 private UnsupportedOperationException notModifiable() {
755 return new UnsupportedOperationException("Not externally modifiable.");
759 public BindingCodec<Map<QName, Object>, Object> put(T key, BindingCodec<?, ?> value) {
760 throw notModifiable();
764 public void putAll(Map<? extends T, ? extends BindingCodec<?, ?>> m) {
765 throw notModifiable();
769 public int hashCode() {
770 return super.hashCode();
774 public boolean isEmpty() {
779 public Set<T> keySet() {
780 return Collections.emptySet();
784 public Set<Entry<T, BindingCodec<?, ?>>> entrySet() {
785 return Collections.emptySet();
789 public boolean containsValue(Object value) {
794 @SuppressWarnings({ "rawtypes", "unchecked" })
795 private class AugmentableCompositeCodec implements BindingCodec {
797 private final Class augmentableType;
799 Map<Class, AugmentationCodec<?>> localAugmentationCodecs = Collections.synchronizedMap(new WeakHashMap<Class, AugmentationCodec<?>>());
801 public AugmentableCompositeCodec(Class type) {
802 Preconditions.checkArgument(Augmentable.class.isAssignableFrom(type));
803 augmentableType = type;
807 public Object serialize(Object input) {
808 if (input instanceof Augmentable<?>) {
810 Map<Class, Augmentation> augmentations = getAugmentations(input);
811 return serializeImpl(augmentations);
816 private Map<Class, Augmentation> getAugmentations(Object input) {
817 Field augmentationField;
819 augmentationField = input.getClass().getDeclaredField("augmentation");
820 augmentationField.setAccessible(true);
821 Map<Class, Augmentation> augMap = (Map<Class, Augmentation>) augmentationField.get(input);
823 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
824 LOG.debug("Could not read augmentations for {}", input, e);
826 return Collections.emptyMap();
829 private List serializeImpl(Map<Class, Augmentation> input) {
830 List ret = new ArrayList<>();
831 for (Entry<Class, Augmentation> entry : input.entrySet()) {
832 AugmentationCodec codec = getCodecForAugmentation(entry.getKey());
833 CompositeNode node = codec.serialize(new ValueWithQName(null, entry.getValue()));
834 ret.addAll(node.getChildren());
839 public synchronized <T extends Augmentation<?>> void addAugmentationCodec(Class<T> augmentationClass,
840 AugmentationCodec<T> value) {
841 localAugmentationCodecs.put(augmentationClass, value);
845 public Map<Class, Augmentation> deserialize(Object input) {
846 Map<Class, Augmentation> ret = new HashMap<>();
847 if (input instanceof CompositeNode) {
848 List<Entry<Class, AugmentationCodec<?>>> codecs = new ArrayList<>(localAugmentationCodecs.entrySet());
849 for (Entry<Class, AugmentationCodec<?>> codec : codecs) {
850 ValueWithQName<?> value = codec.getValue().deserialize((CompositeNode) input);
851 if (value != null && value.getValue() != null) {
852 ret.put(codec.getKey(), (Augmentation) value.getValue());
859 public Class getAugmentableType() {
860 return augmentableType;
864 @SuppressWarnings({ "rawtypes", "unchecked" })
865 private static class LateMixinCodec implements BindingCodec, Delegator<BindingCodec> {
867 private BindingCodec delegate;
870 public BindingCodec getDelegate() {
871 if (delegate == null) {
872 throw new IllegalStateException("Codec not initialized yet.");
878 public Object deserialize(Object input) {
879 return getDelegate().deserialize(input);
883 public Object serialize(Object input) {
884 return getDelegate().serialize(input);
888 private static class AugmentationCodecWrapper<T extends Augmentation<?>> implements AugmentationCodec<T>,
889 Delegator<BindingCodec> {
891 private BindingCodec delegate;
892 private QName augmentationQName;
894 public AugmentationCodecWrapper(BindingCodec<Map<QName, Object>, Object> rawCodec) {
895 this.delegate = rawCodec;
896 this.augmentationQName = BindingReflections.findQName(rawCodec.getClass());
900 public BindingCodec getDelegate() {
905 public CompositeNode serialize(ValueWithQName<T> input) {
906 @SuppressWarnings("unchecked")
907 List<Map<QName, Object>> rawValues = (List<Map<QName, Object>>) getDelegate().serialize(input);
908 List<Node<?>> serialized = new ArrayList<>(rawValues.size());
909 for (Map<QName, Object> val : rawValues) {
910 serialized.add(IntermediateMapping.toNode(val));
912 return new CompositeNodeTOImpl(input.getQname(), null, serialized);
916 @SuppressWarnings("unchecked")
917 public ValueWithQName<T> deserialize(Node<?> input) {
918 Object rawCodecValue = getDelegate().deserialize((Map<QName, Object>) input);
919 return new ValueWithQName<T>(input.getNodeType(), (T) rawCodecValue);
923 public QName getAugmentationQName() {
924 return augmentationQName;
928 private class IdentityCompositeCodec implements IdentityCodec {
931 public Object deserialize(Object input) {
932 Preconditions.checkArgument(input instanceof QName);
933 return deserialize((QName) input);
937 public Class<?> deserialize(QName input) {
938 Type type = qnamesToIdentityMap.get(input);
942 ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
943 WeakReference<Class> softref = typeToClass.get(typeref);
944 if (softref == null) {
947 return softref.get();
951 public QName serialize(Class input) {
952 Preconditions.checkArgument(BaseIdentity.class.isAssignableFrom(input));
953 bindingClassEncountered(input);
954 QName qname = identityQNames.get(input);
958 ConcreteType typeref = Types.typeForClass(input);
959 qname = typeToQname.get(typeref);
961 identityQNames.put(input, qname);
967 public Object serialize(Object input) {
968 Preconditions.checkArgument(input instanceof Class);
969 return serialize((Class) input);
973 public boolean isCodecAvailable(Class<? extends DataContainer> cls) {
974 if (containerCodecs.containsKey(cls)) {
977 if (identifierCodecs.containsKey(cls)) {
980 if (choiceCodecs.containsKey(cls)) {
983 if (caseCodecs.containsKey(cls)) {
986 if (augmentableCodecs.containsKey(cls)) {
989 if (augmentationCodecs.containsKey(cls)) {