Bump versions to 14.0.0-SNAPSHOT
[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.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  * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
21  * modules) they may have one static instance generated when first time needed.
22  */
23 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
24 abstract class SchemaUnawareCodec extends AbstractValueCodec<Object, Object> {
25     /**
26      * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
27      * binary, strings and empty.
28      */
29     static final @NonNull SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
30         @Override
31         protected Object serializeImpl(final Object input) {
32             return input;
33         }
34
35         @Override
36         protected Object deserializeImpl(final Object input) {
37             return input;
38         }
39     };
40
41     static @NonNull SchemaUnawareCodec of(final Class<?> typeClz, final TypeDefinition<?> def) {
42         if (BindingReflections.isBindingClass(typeClz)) {
43             return getCachedSchemaUnawareCodec(typeClz, def);
44         }
45         return NOOP_CODEC;
46     }
47
48     private static @NonNull SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
49             final TypeDefinition<?> def) {
50         // FIXME: extract this only when really needed
51         var rootType = requireNonNull(def);
52         while (true) {
53             final var base = rootType.getBaseType();
54             if (base != null) {
55                 rootType = base;
56             } else {
57                 break;
58             }
59         }
60
61         try {
62             if (rootType instanceof EnumTypeDefinition enumType) {
63                 return EnumerationCodec.of(typeClz, enumType);
64             } else if (rootType instanceof BitsTypeDefinition bitsType) {
65                 return BitsCodec.of(typeClz, bitsType);
66             } else {
67                 return EncapsulatedValueCodec.of(typeClz);
68             }
69         } catch (ExecutionException e) {
70             throw new IllegalStateException(e);
71         }
72     }
73 }