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.reflect.Field;
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;
20 public class CodecMapping {
22 private static final Logger LOG = LoggerFactory.getLogger(CodecMapping.class);
24 public static final String INSTANCE_IDENTIFIER_CODEC = "INSTANCE_IDENTIFIER_CODEC";
25 public static final String IDENTITYREF_CODEC = "IDENTITYREF_CODEC";
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";
31 public static void setIdentifierCodec(Class<?> obj,InstanceIdentifierCodec codec) {
32 Field instanceIdField;
34 instanceIdField = obj.getField(INSTANCE_IDENTIFIER_CODEC);
35 if (instanceIdField != null) {
36 instanceIdField.set(null, codec);
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);
45 public static void setIdentityRefCodec(Class<?> obj,IdentityCodec<?> codec) {
46 Field instanceIdField;
48 instanceIdField = obj.getField(IDENTITYREF_CODEC);
49 if (instanceIdField != null) {
50 instanceIdField.set(null, codec);
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);
59 public static void setClassToCaseMap(Class<? extends BindingCodec<?,?>> codec,
60 Map<Class<?>,BindingCodec<?,?>> classToCaseRawCodec) {
61 Field instanceIdField;
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);
72 public static void setCompositeNodeToCaseMap(Class<? extends BindingCodec<?,?>> codec,
73 Map<CompositeNode,BindingCodec<?,?>> compositeToCase) {
74 Field instanceIdField;
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);
85 public static void setAugmentationCodec(Class<? extends BindingCodec<?,?>> dataCodec,
86 BindingCodec<?,?> augmentableCodec) {
87 Field instanceIdField;
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);
99 public static BindingCodec<?,?> getAugmentationCodec(Class<? extends BindingCodec<?,?>> dataCodec) {
100 Field instanceIdField;
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);