Move RuntimeGeneratedMappingService from md-sal to yang-data-impl(codecs + apis)...
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / CodecMapping.java
1 package org.opendaylight.yangtools.sal.binding.generator.impl;
2
3 import java.lang.reflect.Field;
4 import java.util.Map;
5
6 import org.opendaylight.yangtools.yang.data.impl.codec.IdentitityCodec;
7 import org.opendaylight.yangtools.yang.data.impl.codec.InstanceIdentifierCodec;
8 import org.opendaylight.yangtools.yang.binding.BindingCodec;
9 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 public class CodecMapping {
14
15     private static final Logger LOG = LoggerFactory.getLogger(CodecMapping.class);
16
17     public static final String INSTANCE_IDENTIFIER_CODEC = "INSTANCE_IDENTIFIER_CODEC";
18     public static final String IDENTITYREF_CODEC = "IDENTITYREF_CODEC";
19
20     public static final String CLASS_TO_CASE_MAP = "CLASS_TO_CASE";
21     public static final String COMPOSITE_TO_CASE = "COMPOSITE_TO_CASE";
22     public static final String AUGMENTATION_CODEC = "AUGMENTATION_CODEC";
23
24     public static void setIdentifierCodec(Class<?> obj,InstanceIdentifierCodec codec) {
25         Field instanceIdField;
26         try {
27             instanceIdField = obj.getField(INSTANCE_IDENTIFIER_CODEC);
28             if(obj != null) {
29                 instanceIdField.set(null, codec);
30             }
31         } catch (NoSuchFieldException e) {
32            LOG.trace("Instance identifier codec is not needed for {}",obj.getName(),e);
33         } catch (SecurityException | IllegalAccessException e) {
34             LOG.error("Instance identifier could not be set for {}",obj.getName(),e);
35         }
36     }
37
38
39     public static void setIdentityRefCodec(Class<?> obj,IdentitityCodec<?> codec) {
40         Field instanceIdField;
41         try {
42             instanceIdField = obj.getField(IDENTITYREF_CODEC);
43             if(obj != null) {
44                 instanceIdField.set(null, codec);
45             }
46         } catch (NoSuchFieldException e) {
47            LOG.trace("Instance identifier codec is not needed for {}",obj.getName(),e);
48         } catch (SecurityException | IllegalAccessException e) {
49             LOG.error("Instance identifier could not be set for {}",obj.getName(),e);
50         }
51     }
52
53     public static void setClassToCaseMap(Class<? extends BindingCodec<?,?>> codec,
54             Map<Class<?>,BindingCodec<?,?>> classToCaseRawCodec) {
55         Field instanceIdField;
56         try {
57             instanceIdField = codec.getField(CLASS_TO_CASE_MAP);
58             instanceIdField.set(null, classToCaseRawCodec);
59         } catch (NoSuchFieldException e) {
60             LOG.debug("BUG: Class to case mappping is not needed for {}",codec.getName(),e);
61         } catch (SecurityException | IllegalAccessException e) {
62             LOG.error("Class to case mappping could not be set for {}",codec.getName(),e);
63         }
64     }
65
66     public static void setCompositeNodeToCaseMap(Class<? extends BindingCodec<?,?>> codec,
67             Map<CompositeNode,BindingCodec<?,?>> compositeToCase) {
68         Field instanceIdField;
69         try {
70             instanceIdField = codec.getField(COMPOSITE_TO_CASE);
71             instanceIdField.set(null, compositeToCase);
72         } catch (NoSuchFieldException e) {
73             LOG.debug("BUG: Class to case mappping is not needed for {}",codec.getName(),e);
74         } catch (SecurityException | IllegalAccessException e) {
75             LOG.error("Composite node to case mappping could not be set for {}",codec.getName(),e);
76         }
77     }
78
79     public static void setAugmentationCodec(Class<? extends BindingCodec<?,?>> dataCodec,
80             BindingCodec<?,?> augmentableCodec) {
81             Field instanceIdField;
82             try {
83                 instanceIdField = dataCodec.getField(AUGMENTATION_CODEC);
84                 instanceIdField.set(null, augmentableCodec);
85             } catch (NoSuchFieldException e) {
86                 LOG.debug("BUG: Augmentation codec is not needed for {}",dataCodec.getName(),e);
87             } catch (SecurityException | IllegalAccessException e) {
88                 LOG.error("Augmentation codec could not be set for {}",dataCodec.getName(),e);
89             }
90     }
91
92
93     public static BindingCodec<?,?> getAugmentationCodec(Class<? extends BindingCodec<?,?>> dataCodec) {
94             Field instanceIdField;
95             try {
96                 instanceIdField = dataCodec.getField(AUGMENTATION_CODEC);
97                 return (BindingCodec<?,?>) instanceIdField.get(null);
98             } catch (NoSuchFieldException e) {
99                 LOG.debug("BUG: Augmentation codec is not needed for {}",dataCodec.getName(),e);
100             } catch (SecurityException | IllegalAccessException e) {
101                 LOG.error("Augmentation codec could not be set for {}",dataCodec.getName(),e);
102             }
103             return null;
104     }
105 }