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.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
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.
23 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
24 abstract class SchemaUnawareCodec extends AbstractValueCodec<Object, Object> {
26 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
27 * binary, strings and empty.
29 static final @NonNull SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
31 protected Object serializeImpl(final Object input) {
36 protected Object deserializeImpl(final Object input) {
41 static @NonNull SchemaUnawareCodec of(final Class<?> typeClz, final TypeDefinition<?> def) {
42 if (BindingReflections.isBindingClass(typeClz)) {
43 return getCachedSchemaUnawareCodec(typeClz, def);
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);
53 final var base = rootType.getBaseType();
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);
67 return EncapsulatedValueCodec.of(typeClz);
69 } catch (ExecutionException e) {
70 throw new IllegalStateException(e);