Promote SchemaUnawareCodec to a top-level construct
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / SchemaUnawareCodec.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.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
15 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
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.EnumTypeDefinition;
19
20 /**
21  * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
22  * modules) they may have one static instance generated when first time needed.
23  */
24 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
25 interface SchemaUnawareCodec extends IllegalArgumentCodec<Object, Object> {
26     /**
27      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
28      * binary, strings and empty.
29      */
30     @NonNull SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
31         @Override
32         public Object serialize(final Object input) {
33             return input;
34         }
35
36         @Override
37         public Object deserialize(final Object input) {
38             return input;
39         }
40     };
41
42     static @NonNull SchemaUnawareCodec of(final Class<?> typeClz, final TypeDefinition<?> def) {
43         if (BindingReflections.isBindingClass(typeClz)) {
44             return getCachedSchemaUnawareCodec(typeClz, def);
45         }
46         return NOOP_CODEC;
47     }
48
49     private static @NonNull SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
50             final TypeDefinition<?> def) {
51         // FIXME: extract this only when really needed
52         var rootType = requireNonNull(def);
53         while (true) {
54             final var base = rootType.getBaseType();
55             if (base != null) {
56                 rootType = base;
57             } else {
58                 break;
59             }
60         }
61
62         try {
63             if (rootType instanceof EnumTypeDefinition) {
64                 return EnumerationCodec.of(typeClz, (EnumTypeDefinition) rootType);
65             } else if (rootType instanceof BitsTypeDefinition) {
66                 return BitsCodec.of(typeClz, (BitsTypeDefinition) rootType);
67             } else {
68                 return EncapsulatedValueCodec.of(typeClz, def);
69             }
70         } catch (ExecutionException e) {
71             throw new IllegalStateException(e);
72         }
73     }
74 }