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