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.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;
20 * Value codec, which serializes / deserializes values from DOM simple values.
22 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
23 abstract class ValueTypeCodec implements IllegalArgumentCodec<Object, Object> {
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.
28 // FIXME: IllegalArgumentCodec is perhaps not appropriate here due to null behavior
29 interface SchemaUnawareCodec extends IllegalArgumentCodec<Object, Object> {
34 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
35 * binary, strings and empty.
37 public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
39 public Object serialize(final Object input) {
44 public Object deserialize(final Object input) {
49 public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
50 if (BindingReflections.isBindingClass(typeClz)) {
51 return getCachedSchemaUnawareCodec(typeClz, def);
56 private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final TypeDefinition<?> def) {
57 // FIXME: extract this only when really needed
58 var rootType = requireNonNull(def);
60 final var base = rootType.getBaseType();
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);
74 return EncapsulatedValueCodec.of(typeClz, def);
76 } catch (ExecutionException e) {
77 throw new IllegalStateException(e);
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);