63cf8de71ccb8feb44af03830ba2c0db19242cec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / 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.mdsal.binding.dom.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.mdsal.binding.spec.reflect.BindingReflections;
15 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
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.EnumTypeDefinition;
19
20 /**
21  * Value codec, which serializes / deserializes values from DOM simple values.
22  */
23 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
24 abstract class ValueTypeCodec implements IllegalArgumentCodec<Object, Object> {
25     private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys()
26             .build();
27
28     /**
29      * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
30      * modules) they may have one static instance generated when first time needed.
31      */
32     // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
33     interface SchemaUnawareCodec extends IllegalArgumentCodec<Object, Object> {
34
35     }
36
37     /**
38      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
39      * binary, strings and empty.
40      */
41     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
42
43         @Override
44         public Object serialize(final Object input) {
45             return input;
46         }
47
48         @Override
49         public Object deserialize(final Object input) {
50             return input;
51         }
52     };
53
54     public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
55         if (BindingReflections.isBindingClass(typeClz)) {
56             return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
57         }
58         return NOOP_CODEC;
59     }
60
61     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
62             final Callable<? extends SchemaUnawareCodec> loader) {
63         try {
64             return STATIC_CODECS.get(typeClz, loader);
65         } catch (ExecutionException e) {
66             throw new IllegalStateException(e);
67         }
68     }
69
70     private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
71             final TypeDefinition<?> def) {
72
73         TypeDefinition<?> rootType = def;
74         while (rootType.getBaseType() != null) {
75             rootType = rootType.getBaseType();
76         }
77         if (rootType instanceof EnumTypeDefinition) {
78             return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
79         } else if (rootType instanceof BitsTypeDefinition) {
80             return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
81         }
82         return EncapsulatedValueCodec.loader(typeClz, def);
83     }
84
85     @SuppressWarnings("rawtypes")
86     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
87              final IllegalArgumentCodec delegate) {
88         SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
89             EncapsulatedValueCodec.loader(typeClz, typeDef));
90         return new CompositeValueCodec(extractor, delegate);
91     }
92
93 }