X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fbinding%2Fdata%2Fcodec%2Fimpl%2FBindingCodecContext.java;h=1f8f9eb79d542b0d12d75aab3095ac4b071e0f8f;hb=eba6505fa7ca734ff895fcf753664f3d73f1324b;hp=be83c9071b6c937394c47fa2bc26557070f8c5c8;hpb=c4b07e397f8a344a3fa638d83c60f1a5bf070794;p=mdsal.git diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/BindingCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/BindingCodecContext.java index be83c9071b..1f8f9eb79d 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/BindingCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/BindingCodecContext.java @@ -52,6 +52,7 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode; import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; @@ -63,7 +64,6 @@ import org.slf4j.LoggerFactory; final class BindingCodecContext implements CodecContextFactory, BindingCodecTree, Immutable { private static final Logger LOG = LoggerFactory.getLogger(BindingCodecContext.class); - static final String GETTER_PREFIX = "get"; private final Codec> instanceIdentifierCodec; private final Codec> identityCodec; @@ -201,7 +201,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree } if (currentList != null) { - if(bindingArguments != null) { + if (bindingArguments != null) { bindingArguments.add(currentList.getBindingPathArgument(null)); } return currentList; @@ -213,7 +213,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree return root.getNotification(notification); } - ContainerNodeCodecContext getRpcDataContext(final SchemaPath path) { + RpcInputCodec getRpcInputCodec(final SchemaPath path) { return root.getRpc(path); } @@ -222,31 +222,19 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree final DataNodeContainer childSchema) { final Map getterToLeafSchema = new HashMap<>(); for (final DataSchemaNode leaf : childSchema.getChildNodes()) { - final TypeDefinition typeDef; - if (leaf instanceof LeafSchemaNode) { - typeDef = ((LeafSchemaNode) leaf).getType(); - } else if (leaf instanceof LeafListSchemaNode) { - typeDef = ((LeafListSchemaNode) leaf).getType(); - } else { - continue; + if (leaf instanceof TypedSchemaNode) { + getterToLeafSchema.put(getGetterName(leaf.getQName(), ((TypedSchemaNode) leaf).getType()), leaf); } - - final String getterName = getGetterName(leaf.getQName(), typeDef); - getterToLeafSchema.put(getterName, leaf); } return getLeafNodesUsingReflection(parentClass, getterToLeafSchema); } - private String getGetterName(final QName qName, TypeDefinition typeDef) { - final String suffix = BindingMapping.getClassName(qName); - - while (typeDef.getBaseType() != null) { - typeDef = typeDef.getBaseType(); - } + private static String getGetterName(final QName qName, final TypeDefinition typeDef) { + final String suffix = BindingMapping.getGetterSuffix(qName); if (typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) { return "is" + suffix; } - return GETTER_PREFIX + suffix; + return "get" + suffix; } private ImmutableMap> getLeafNodesUsingReflection(final Class parentClass, @@ -281,14 +269,12 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree } private Codec getCodec(final Class valueType, final DataSchemaNode schema) { - final TypeDefinition instantiatedType; - if (schema instanceof LeafSchemaNode) { - instantiatedType = ((LeafSchemaNode) schema).getType(); - } else if (schema instanceof LeafListSchemaNode) { - instantiatedType = ((LeafListSchemaNode) schema).getType(); - } else { - throw new IllegalArgumentException("Unsupported leaf node type " + schema.getClass()); - } + Preconditions.checkArgument(schema instanceof TypedSchemaNode, "Unsupported leaf node type %s", schema); + + return getCodec(valueType, ((TypedSchemaNode)schema).getType()); + } + + Codec getCodec(final Class valueType, final TypeDefinition instantiatedType) { if (Class.class.equals(valueType)) { @SuppressWarnings({ "unchecked", "rawtypes" }) final Codec casted = (Codec) identityCodec; @@ -298,39 +284,34 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree final Codec casted = (Codec) instanceIdentifierCodec; return casted; } else if (Boolean.class.equals(valueType)) { - if(instantiatedType instanceof EmptyTypeDefinition) { + if (instantiatedType instanceof EmptyTypeDefinition) { return ValueTypeCodec.EMPTY_CODEC; } } else if (BindingReflections.isBindingClass(valueType)) { - return getCodec(valueType, instantiatedType); + return getCodecForBindingClass(valueType, instantiatedType); } return ValueTypeCodec.NOOP_CODEC; } - private Codec getCodec(final Class valueType, final TypeDefinition instantiatedType) { - @SuppressWarnings("rawtypes") - TypeDefinition rootType = instantiatedType; - while (rootType.getBaseType() != null) { - rootType = rootType.getBaseType(); - } - if (rootType instanceof IdentityrefTypeDefinition) { + private Codec getCodecForBindingClass(final Class valueType, final TypeDefinition typeDef) { + if (typeDef instanceof IdentityrefTypeDefinition) { return ValueTypeCodec.encapsulatedValueCodecFor(valueType, identityCodec); - } else if (rootType instanceof InstanceIdentifierTypeDefinition) { + } else if (typeDef instanceof InstanceIdentifierTypeDefinition) { return ValueTypeCodec.encapsulatedValueCodecFor(valueType, instanceIdentifierCodec); - } else if (rootType instanceof UnionTypeDefinition) { - final Callable loader = UnionTypeCodec.loader(valueType, (UnionTypeDefinition) rootType); + } else if (typeDef instanceof UnionTypeDefinition) { + final Callable loader = UnionTypeCodec.loader(valueType, (UnionTypeDefinition) typeDef, this); try { return loader.call(); } catch (final Exception e) { throw new IllegalStateException("Unable to load codec for " + valueType, e); } - } else if(rootType instanceof LeafrefTypeDefinition) { + } else if (typeDef instanceof LeafrefTypeDefinition) { final Entry typeWithSchema = context.getTypeWithSchema(valueType); final Object schema = typeWithSchema.getValue(); Preconditions.checkState(schema instanceof TypeDefinition); return getCodec(valueType, (TypeDefinition) schema); } - return ValueTypeCodec.getCodecFor(valueType, instantiatedType); + return ValueTypeCodec.getCodecFor(valueType, typeDef); } @Override