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.yangtools.binding.data.codec.impl;
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.yangtools.concepts.Codec;
15 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
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 * Value codec, which serializes / deserializes values from DOM simple values.
24 abstract class ValueTypeCodec implements Codec<Object, Object> {
26 private static final Cache<Class<?>, SchemaUnawareCodec> staticCodecs = CacheBuilder.newBuilder().weakKeys()
31 * Marker interface for codecs, which functionality will not be
32 * affected by schema change (introduction of new YANG modules)
33 * they may have one static instance generated when
37 interface SchemaUnawareCodec extends Codec<Object,Object> {
44 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model
45 * for base YANG types, representing numbers, binary and strings.
49 public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
52 public Object serialize(final Object input) {
57 public Object deserialize(final Object input) {
63 public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
64 if (BindingReflections.isBindingClass(typeClz)) {
65 return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
70 private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final Callable<? extends SchemaUnawareCodec> loader) {
72 return staticCodecs.get(typeClz, loader);
73 } catch (ExecutionException e) {
74 throw new IllegalStateException(e);
78 private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz, final TypeDefinition<?> def) {
80 TypeDefinition<?> rootType = def;
81 while (rootType.getBaseType() != null) {
82 rootType = rootType.getBaseType();
84 if (rootType instanceof EnumTypeDefinition) {
85 return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
86 } else if (rootType instanceof BitsTypeDefinition) {
87 return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
89 return EncapsulatedValueCodec.loader(typeClz);
92 @SuppressWarnings("rawtypes")
93 static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final Codec delegate) {
94 SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz, EncapsulatedValueCodec.loader(typeClz));
95 return new CompositeValueCodec(extractor, delegate);