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