BUG 2868: Update iana-if-type with RFC 7224
[mdsal.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 import org.opendaylight.yangtools.yang.binding.BindingCodec;
13 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
14 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
15 import org.opendaylight.yangtools.yang.data.impl.codec.InstanceIdentifierCodec;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public final class CodecMapping {
20
21     private static final Logger LOG = LoggerFactory.getLogger(CodecMapping.class);
22
23     public static final String INSTANCE_IDENTIFIER_CODEC = "INSTANCE_IDENTIFIER_CODEC";
24     public static final String IDENTITYREF_CODEC = "IDENTITYREF_CODEC";
25
26     public static final String CLASS_TO_CASE_MAP = "CLASS_TO_CASE";
27     public static final String COMPOSITE_TO_CASE = "COMPOSITE_TO_CASE";
28     public static final String AUGMENTATION_CODEC = "AUGMENTATION_CODEC";
29     public static final String DISPATCH_CODEC = "DISPATCH_CODEC";
30
31     private CodecMapping() {
32         throw new UnsupportedOperationException("Utility class should not be instantiated");
33     }
34
35     public static void setIdentifierCodec(Class<?> obj,InstanceIdentifierCodec codec) {
36         Field instanceIdField;
37         try {
38             instanceIdField = obj.getField(INSTANCE_IDENTIFIER_CODEC);
39             if (instanceIdField != null) {
40                 instanceIdField.set(null, codec);
41             }
42         } catch (NoSuchFieldException e) {
43            LOG.trace("Instance identifier codec is not needed for {}",obj.getName(),e);
44         } catch (SecurityException | IllegalAccessException e) {
45             LOG.error("Instance identifier could not be set for {}",obj.getName(),e);
46         }
47     }
48
49     public static void setIdentityRefCodec(Class<?> obj,IdentityCodec<?> codec) {
50         Field instanceIdField;
51         try {
52             instanceIdField = obj.getField(IDENTITYREF_CODEC);
53             if (instanceIdField != null) {
54                 instanceIdField.set(null, codec);
55             }
56         } catch (NoSuchFieldException e) {
57            LOG.trace("Instance identifier codec is not needed for {}",obj.getName(),e);
58         } catch (SecurityException | IllegalAccessException e) {
59             LOG.error("Instance identifier could not be set for {}",obj.getName(),e);
60         }
61     }
62
63     public static void setClassToCaseMap(Class<? extends BindingCodec<?,?>> codec,
64             Map<Class<?>,BindingCodec<?,?>> classToCaseRawCodec) {
65         Field instanceIdField;
66         try {
67             instanceIdField = codec.getField(CLASS_TO_CASE_MAP);
68             instanceIdField.set(null, classToCaseRawCodec);
69         } catch (NoSuchFieldException e) {
70             LOG.debug("BUG: Class to case mappping is not needed for {}",codec.getName(),e);
71         } catch (SecurityException | IllegalAccessException e) {
72             LOG.error("Class to case mappping could not be set for {}",codec.getName(),e);
73         }
74     }
75
76     public static void setCompositeNodeToCaseMap(Class<? extends BindingCodec<?,?>> codec,
77             Map<CompositeNode,BindingCodec<?,?>> compositeToCase) {
78         Field instanceIdField;
79         try {
80             instanceIdField = codec.getField(COMPOSITE_TO_CASE);
81             instanceIdField.set(null, compositeToCase);
82         } catch (NoSuchFieldException e) {
83             LOG.debug("BUG: Class to case mappping is not needed for {}",codec.getName(),e);
84         } catch (SecurityException | IllegalAccessException e) {
85             LOG.error("Composite node to case mappping could not be set for {}",codec.getName(),e);
86         }
87     }
88
89     public static void setDispatchCodec(Class<? extends BindingCodec<?,?>> codec,
90             BindingCodec<?, ?> dispatchCodec) {
91         Field instanceIdField;
92         try {
93             instanceIdField = codec.getField(DISPATCH_CODEC);
94             instanceIdField.set(null, dispatchCodec);
95         } catch (NoSuchFieldException e) {
96             LOG.debug("BUG: dispatch codec is not needed for {}",codec.getName(),e);
97         } catch (SecurityException | IllegalAccessException e) {
98             LOG.error("Dispatch codec could not be set for {}",codec.getName(),e);
99         }
100     }
101
102     public static void setAugmentationCodec(Class<? extends BindingCodec<?,?>> dataCodec,
103             BindingCodec<?,?> augmentableCodec) {
104             Field instanceIdField;
105             try {
106                 instanceIdField = dataCodec.getField(AUGMENTATION_CODEC);
107                 instanceIdField.set(null, augmentableCodec);
108             } catch (NoSuchFieldException e) {
109                 LOG.debug("BUG: Augmentation codec is not needed for {}",dataCodec.getName(),e);
110             } catch (SecurityException | IllegalAccessException e) {
111                 LOG.error("Augmentation codec could not be set for {}",dataCodec.getName(),e);
112             }
113     }
114
115
116     public static BindingCodec<?,?> getAugmentationCodec(Class<? extends BindingCodec<?,?>> dataCodec) {
117             Field instanceIdField;
118             try {
119                 instanceIdField = dataCodec.getField(AUGMENTATION_CODEC);
120                 return (BindingCodec<?,?>) instanceIdField.get(null);
121             } catch (NoSuchFieldException e) {
122                 LOG.debug("BUG: Augmentation codec is not needed for {}",dataCodec.getName(),e);
123             } catch (SecurityException | IllegalAccessException e) {
124                 LOG.error("Augmentation codec could not be set for {}",dataCodec.getName(),e);
125             }
126             return null;
127     }
128 }