Binding v2 - Map 'type empty' to yangtools.yang.common.Empty
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / value / ValueTypeCodec.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
17 import org.opendaylight.yangtools.concepts.Codec;
18 import org.opendaylight.yangtools.yang.common.Empty;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
23
24 /**
25  * Value codec, which serializes / de-serializes values from DOM simple values.
26  */
27 @Beta
28 public abstract class ValueTypeCodec implements Codec<Object, Object> {
29
30     private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys()
31             .build();
32
33     /**
34      * Marker interface for codecs, which functionality will not be
35      * affected by schema change (introduction of new YANG modules)
36      * they may have one static instance generated when
37      * first time needed.
38      */
39     interface SchemaUnawareCodec extends Codec<Object,Object> {
40     }
41
42     /**
43      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model
44      * for base YANG types, representing numbers, binary and strings.
45      */
46     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
47
48         @Override
49         public Object serialize(final Object input) {
50             return input;
51         }
52
53         @Override
54         public Object deserialize(final Object input) {
55             return input;
56         }
57     };
58
59     public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
60
61         @Override
62         public Object serialize(final Object arg0) {
63             // Empty type has null value in NormalizedNode and Composite Node
64             // representation
65             return Empty.getInstance();
66         }
67
68         @Override
69         public Object deserialize(final Object arg0) {
70             /* Empty type has Empty representation in Binding-aware world
71             *  otherwise it is null.
72             *  So when codec is triggered, empty leaf is present and its
73             *  value is Empty.getInstance(), that means we are safe to
74             *  return it directly.
75             */
76             return arg0;
77         }
78     };
79
80     private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = new Callable<SchemaUnawareCodec>() {
81
82         @Override
83         public SchemaUnawareCodec call() {
84             return EMPTY_CODEC;
85         }
86     };
87
88     public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
89         if (BindingReflections.isBindingClass(typeClz)) {
90             return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
91         }
92         return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
93     }
94
95     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
96             final Callable<? extends SchemaUnawareCodec> loader) {
97         try {
98             return STATIC_CODECS.get(typeClz, loader);
99         } catch (final ExecutionException e) {
100             throw new IllegalStateException(e);
101         }
102     }
103
104     private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
105             final TypeDefinition<?> def) {
106         TypeDefinition<?> rootType = def;
107         while (rootType.getBaseType() != null) {
108             rootType = rootType.getBaseType();
109         }
110         if (rootType instanceof EnumTypeDefinition) {
111             return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
112         } else if (rootType instanceof BitsTypeDefinition) {
113             return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
114         }
115
116         return EncapsulatedValueCodec.loader(typeClz, def);
117     }
118
119     @SuppressWarnings("rawtypes")
120     public static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
121             final Codec delegate) {
122         final SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
123             EncapsulatedValueCodec.loader(typeClz, typeDef));
124         return new CompositeValueCodec(extractor, delegate);
125     }
126 }