Introduce top-level pom file.
[mdsal.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / ValueTypeCodec.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.binding.data.codec.impl;
9
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.yangtools.concepts.Codec;
15 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
20
21 /**
22  * Value codec, which serializes / deserializes values from DOM simple values.
23  *
24  */
25 abstract class ValueTypeCodec implements Codec<Object, Object> {
26
27     private static final Cache<Class<?>, SchemaUnawareCodec> staticCodecs = CacheBuilder.newBuilder().weakKeys()
28             .build();
29
30
31     /**
32      * Marker interface for codecs, which functionality will not be
33      * affected by schema change (introduction of new YANG modules)
34      * they may have one static instance generated when
35      * first time needed.
36      *
37      */
38     interface SchemaUnawareCodec extends Codec<Object,Object> {
39
40     }
41
42
43     /**
44      *
45      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model
46      * for base YANG types, representing numbers, binary and strings.
47      *
48      *
49      */
50     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
51
52         @Override
53         public Object serialize(final Object input) {
54             return input;
55         }
56
57         @Override
58         public Object deserialize(final Object input) {
59             return input;
60         }
61     };
62
63     public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
64
65         @Override
66         public Object serialize(final Object arg0) {
67             // Empty type has null value in NormalizedNode and Composite Node
68             // representation
69             return null;
70         }
71
72         @Override
73         public Object deserialize(final Object arg0) {
74             /* Empty type has boolean.TRUE representation in Binding-aware world
75             *  otherwise it is null / false.
76             *  So when codec is triggered, empty leaf is present, that means we
77             *  are safe to return true.
78             */
79             return Boolean.TRUE;
80         }
81     };
82
83     private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = new Callable<SchemaUnawareCodec>() {
84
85         @Override
86         public SchemaUnawareCodec call() {
87             return EMPTY_CODEC;
88         }
89     };
90
91
92     public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
93         if (BindingReflections.isBindingClass(typeClz)) {
94             return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
95         }
96         TypeDefinition<?> rootType = def;
97         while (rootType.getBaseType() != null) {
98             rootType = rootType.getBaseType();
99         }
100         if (rootType instanceof EmptyTypeDefinition) {
101             return EMPTY_CODEC;
102         }
103         return NOOP_CODEC;
104     }
105
106     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final Callable<? extends SchemaUnawareCodec> loader) {
107         try {
108             return staticCodecs.get(typeClz, loader);
109         } catch (ExecutionException e) {
110             throw new IllegalStateException(e);
111         }
112     }
113
114     private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz, final TypeDefinition<?> def) {
115
116         TypeDefinition<?> rootType = def;
117         while (rootType.getBaseType() != null) {
118             rootType = rootType.getBaseType();
119         }
120         if (rootType instanceof EnumTypeDefinition) {
121             return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
122         } else if (rootType instanceof BitsTypeDefinition) {
123             return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
124         } else if (rootType instanceof EmptyTypeDefinition) {
125             return EMPTY_LOADER;
126         }
127         return EncapsulatedValueCodec.loader(typeClz);
128     }
129
130     @SuppressWarnings("rawtypes")
131     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final Codec delegate) {
132         SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz, EncapsulatedValueCodec.loader(typeClz));
133         return new CompositeValueCodec(extractor, delegate);
134     }
135
136 }