Split STATIC_CODECS into per-type caches
[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 static java.util.Objects.requireNonNull;
11
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
14 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
18
19 /**
20  * Value codec, which serializes / deserializes values from DOM simple values.
21  */
22 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
23 abstract class ValueTypeCodec implements IllegalArgumentCodec<Object, Object> {
24     /**
25      * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
26      * modules) they may have one static instance generated when first time needed.
27      */
28     // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
29     interface SchemaUnawareCodec extends IllegalArgumentCodec<Object, Object> {
30
31     }
32
33     /**
34      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
35      * binary, strings and empty.
36      */
37     public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
38         @Override
39         public Object serialize(final Object input) {
40             return input;
41         }
42
43         @Override
44         public Object deserialize(final Object input) {
45             return input;
46         }
47     };
48
49     public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
50         if (BindingReflections.isBindingClass(typeClz)) {
51             return getCachedSchemaUnawareCodec(typeClz, def);
52         }
53         return NOOP_CODEC;
54     }
55
56     private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final TypeDefinition<?> def) {
57         // FIXME: extract this only when really needed
58         var rootType = requireNonNull(def);
59         while (true) {
60             final var base = rootType.getBaseType();
61             if (base != null) {
62                 rootType = base;
63             } else {
64                 break;
65             }
66         }
67
68         try {
69             if (rootType instanceof EnumTypeDefinition) {
70                 return EnumerationCodec.of(typeClz, (EnumTypeDefinition) rootType);
71             } else if (rootType instanceof BitsTypeDefinition) {
72                 return BitsCodec.of(typeClz, (BitsTypeDefinition) rootType);
73             } else {
74                 return EncapsulatedValueCodec.of(typeClz, def);
75             }
76         } catch (ExecutionException e) {
77             throw new IllegalStateException(e);
78         }
79     }
80
81     @SuppressWarnings("rawtypes")
82     static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
83              final IllegalArgumentCodec delegate) {
84         return new CompositeValueCodec(getCachedSchemaUnawareCodec(typeClz, typeDef), delegate);
85     }
86 }