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 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.mdsal.binding.spec.reflect.BindingReflections;
15 import org.opendaylight.yangtools.concepts.Codec;
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.
23 abstract class ValueTypeCodec implements Codec<Object, Object> {
25 private static final Cache<Class<?>, SchemaUnawareCodec> STATIC_CODECS = CacheBuilder.newBuilder().weakKeys()
29 * Marker interface for codecs, which functionality will not be affected by schema change (introduction of new YANG
30 * modules) they may have one static instance generated when first time needed.
32 interface SchemaUnawareCodec extends Codec<Object,Object> {
37 * No-op Codec, Java YANG Binding uses same types as NormalizedNode model for base YANG types, representing numbers,
38 * binary, strings and empty.
40 public static final SchemaUnawareCodec NOOP_CODEC = new SchemaUnawareCodec() {
43 public Object serialize(final Object input) {
48 public Object deserialize(final Object input) {
53 public static SchemaUnawareCodec getCodecFor(final Class<?> typeClz, final TypeDefinition<?> def) {
54 if (BindingReflections.isBindingClass(typeClz)) {
55 return getCachedSchemaUnawareCodec(typeClz, getCodecLoader(typeClz, def));
60 private static SchemaUnawareCodec getCachedSchemaUnawareCodec(final Class<?> typeClz,
61 final Callable<? extends SchemaUnawareCodec> loader) {
63 return STATIC_CODECS.get(typeClz, loader);
64 } catch (ExecutionException e) {
65 throw new IllegalStateException(e);
69 private static Callable<? extends SchemaUnawareCodec> getCodecLoader(final Class<?> typeClz,
70 final TypeDefinition<?> def) {
72 TypeDefinition<?> rootType = def;
73 while (rootType.getBaseType() != null) {
74 rootType = rootType.getBaseType();
76 if (rootType instanceof EnumTypeDefinition) {
77 return EnumerationCodec.loader(typeClz, (EnumTypeDefinition) rootType);
78 } else if (rootType instanceof BitsTypeDefinition) {
79 return BitsCodec.loader(typeClz, (BitsTypeDefinition) rootType);
81 return EncapsulatedValueCodec.loader(typeClz, def);
84 @SuppressWarnings("rawtypes")
85 static ValueTypeCodec encapsulatedValueCodecFor(final Class<?> typeClz, final TypeDefinition<?> typeDef,
86 final Codec delegate) {
87 SchemaUnawareCodec extractor = getCachedSchemaUnawareCodec(typeClz,
88 EncapsulatedValueCodec.loader(typeClz, typeDef));
89 return new CompositeValueCodec(extractor, delegate);