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