42168b1b51de92ad7920a618aa3834aaa345ff21
[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.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 abstract class ValueTypeCodec implements Codec<Object, Object> {
25
26     private static final Cache<Class<?>, SchemaUnawareCodec> staticCodecs = CacheBuilder.newBuilder().weakKeys()
27             .build();
28
29
30     /**
31      * Marker interface for codecs, which functionality will not be
32      * affected by schema change (introduction of new YANG modules)
33      * they may have one static instance generated when
34      * first time needed.
35      *
36      */
37     interface SchemaUnawareCodec extends Codec<Object,Object> {
38
39     }
40
41
42     /**
43      *
44      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model
45      * for base YANG types, representing numbers, binary and strings.
46      *
47      *
48      */
49     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
50
51         @Override
52         public Object serialize(final Object input) {
53             return input;
54         }
55
56         @Override
57         public Object deserialize(final Object input) {
58             return input;
59         }
60     };
61
62     public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
63
64         @Override
65         public Object serialize(final Object arg0) {
66             // Empty type has null value in NormalizedNode and Composite Node
67             // representation
68             return null;
69         }
70
71         @Override
72         public Object deserialize(final Object arg0) {
73             /* Empty type has boolean.TRUE representation in Binding-aware world
74             *  otherwise it is null / false.
75             *  So when codec is triggered, empty leaf is present, that means we
76             *  are safe to return true.
77             */
78             return Boolean.TRUE;
79         }
80     };
81
82     private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = () -> EMPTY_CODEC;
83
84
85     public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
86         if (BindingReflections.isBindingClass(typeClz)) {
87             return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
88         }
89         return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
90     }
91
92     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final Callable<? extends SchemaUnawareCodec> loader) {
93         try {
94             return staticCodecs.get(typeClz, loader);
95         } catch (ExecutionException e) {
96             throw new IllegalStateException(e);
97         }
98     }
99
100     private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz, final TypeDefinition<?> def) {
101
102         TypeDefinition<?> rootType = def;
103         while (rootType.getBaseType() != null) {
104             rootType = rootType.getBaseType();
105         }
106         if (rootType instanceof EnumTypeDefinition) {
107             return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
108         } else if (rootType instanceof BitsTypeDefinition) {
109             return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
110         } else if (rootType instanceof EmptyTypeDefinition) {
111             return EMPTY_LOADER;
112         }
113         return EncapsulatedValueCodec.loader(typeClz);
114     }
115
116     @SuppressWarnings("rawtypes")
117     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final Codec delegate) {
118         SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz, EncapsulatedValueCodec.loader(typeClz));
119         return new CompositeValueCodec(extractor, delegate);
120     }
121
122 }