55e5d1a50c67cc2932e1d8196a9ca787610e1458
[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     /*
26      * Use identity comparison for keys and allow classes to be GCd themselves.
27      *
28      * Since codecs can (and typically do) hold a direct or indirect strong reference to the class, they need to be also
29      * accessed via reference. Using a weak reference could be problematic, because the codec would quite often be only
30      * weakly reachable. We therefore use a soft reference, whose implementation guidance is suitable to our use case:
31      *
32      *     "Virtual machine implementations are, however, encouraged to bias against clearing recently-created or
33      *      recently-used soft references."
34      */
35     private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder()
36             .weakKeys().softValues().build();
37
38     /**
39      * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
40      * modules) they may have one static instance generated when first time needed.
41      */
42     // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
43     interface SchemaUnawareCodec extends IllegalArgumentCodec<Object, Object> {
44
45     }
46
47     /**
48      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
49      * binary, strings and empty.
50      */
51     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
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 SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
64         if (BindingReflections.isBindingClass(typeClz)) {
65             return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
66         }
67         return NOOP_CODEC;
68     }
69
70     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
71             final Callable<? extends SchemaUnawareCodec> loader) {
72         try {
73             return STATIC_CODECS.get(typeClz, loader);
74         } catch (ExecutionException e) {
75             throw new IllegalStateException(e);
76         }
77     }
78
79     private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
80             final TypeDefinition<?> def) {
81
82         TypeDefinition<?> rootType = def;
83         while (rootType.getBaseType() != null) {
84             rootType = rootType.getBaseType();
85         }
86         if (rootType instanceof EnumTypeDefinition) {
87             return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
88         } else if (rootType instanceof BitsTypeDefinition) {
89             return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
90         }
91         return EncapsulatedValueCodec.loader(typeClz, def);
92     }
93
94     @SuppressWarnings("rawtypes")
95     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
96              final IllegalArgumentCodec delegate) {
97         SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
98             EncapsulatedValueCodec.loader(typeClz, typeDef));
99         return new CompositeValueCodec(extractor, delegate);
100     }
101 }