2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static java.util.Objects.requireNonNull;
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.AbstractIllegalArgumentCodec;
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;
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.
24 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
25 abstract class SchemaUnawareCodec extends AbstractIllegalArgumentCodec<Object, Object> {
27 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
28 * binary, strings and empty.
30 static final @NonNull SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
32 protected Object serializeImpl(final Object input) {
37 protected Object deserializeImpl(final Object input) {
42 static @NonNull SchemaUnawareCodec of(final Class<?> typeClz, final TypeDefinition<?> def) {
43 if (BindingReflections.isBindingClass(typeClz)) {
44 return getCachedSchemaUnawareCodec(typeClz, def);
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);
54 final var base = rootType.getBaseType();
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);
68 return EncapsulatedValueCodec.of(typeClz);
70 } catch (ExecutionException e) {
71 throw new IllegalStateException(e);