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