BUG-8004: handle implicit RPC input
[mdsal.git] / binding / mdsal-binding-dom-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         return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
97     }
98
99     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final Callable<? extends SchemaUnawareCodec> loader) {
100         try {
101             return staticCodecs.get(typeClz, loader);
102         } catch (ExecutionException e) {
103             throw new IllegalStateException(e);
104         }
105     }
106
107     private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz, final TypeDefinition<?> def) {
108
109         TypeDefinition<?> rootType = def;
110         while (rootType.getBaseType() != null) {
111             rootType = rootType.getBaseType();
112         }
113         if (rootType instanceof EnumTypeDefinition) {
114             return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
115         } else if (rootType instanceof BitsTypeDefinition) {
116             return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
117         } else if (rootType instanceof EmptyTypeDefinition) {
118             return EMPTY_LOADER;
119         }
120         return EncapsulatedValueCodec.loader(typeClz);
121     }
122
123     @SuppressWarnings("rawtypes")
124     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final Codec delegate) {
125         SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz, EncapsulatedValueCodec.loader(typeClz));
126         return new CompositeValueCodec(extractor, delegate);
127     }
128
129 }