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.EmptyTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
22 * Value codec, which serializes / deserializes values from DOM simple values.
25 abstract class ValueTypeCodec implements Codec<Object, Object> {
27 private static final Cache<Class<?>, SchemaUnawareCodec> staticCodecs = CacheBuilder.newBuilder().weakKeys()
32 * Marker interface for codecs, which functionality will not be
33 * affected by schema change (introduction of new YANG modules)
34 * they may have one static instance generated when
38 interface SchemaUnawareCodec extends Codec<Object,Object> {
45 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model
46 * for base YANG types, representing numbers, binary and strings.
50 public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
53 public Object serialize(final Object input) {
58 public Object deserialize(final Object input) {
63 public static final SchemaUnawareCodec EMPTY_CODEC = new SchemaUnawareCodec() {
66 public Object serialize(final Object arg0) {
67 // Empty type has null value in NormalizedNode and Composite Node
73 public Object deserialize(final Object arg0) {
74 /* Empty type has boolean.TRUE representation in Binding-aware world
75 * otherwise it is null / false.
76 * So when codec is triggered, empty leaf is present, that means we
77 * are safe to return true.
83 private static final Callable<? extends SchemaUnawareCodec> EMPTY_LOADER = new Callable<SchemaUnawareCodec>() {
86 public SchemaUnawareCodec call() {
92 public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
93 if (BindingReflections.isBindingClass(typeClz)) {
94 return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
96 return def instanceof EmptyTypeDefinition ? EMPTY_CODEC : NOOP_CODEC;
99 private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz, final Callable<? extends SchemaUnawareCodec> loader) {
101 return staticCodecs.get(typeClz, loader);
102 } catch (ExecutionException e) {
103 throw new IllegalStateException(e);
107 private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz, final TypeDefinition<?> def) {
109 TypeDefinition<?> rootType = def;
110 while (rootType.getBaseType() != null) {
111 rootType = rootType.getBaseType();
113 if (rootType instanceof EnumTypeDefinition) {
114 return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
115 } else if (rootType instanceof BitsTypeDefinition) {
116 return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
117 } else if (rootType instanceof EmptyTypeDefinition) {
120 return EncapsulatedValueCodec.loader(typeClz);
123 @SuppressWarnings("rawtypes")
124 static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final Codec delegate) {
125 SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz, EncapsulatedValueCodec.loader(typeClz));
126 return new CompositeValueCodec(extractor, delegate);