X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fdom%2Fcodec%2Fimpl%2FBindingCodecContext.java;h=fe0824110d12d0abb1a190b99fe743f60fc01981;hb=d90adcf214a8f17ae5de22cf8964cbb2a9963972;hp=967e45b5db700e99b5b63f6fb85009220d493504;hpb=d1081ff6798c7678eaaa5decb1a389a884389f51;p=mdsal.git diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java index 967e45b5db..fe0824110d 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java @@ -7,7 +7,10 @@ */ package org.opendaylight.mdsal.binding.dom.codec.impl; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.ImmutableMap; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; @@ -20,14 +23,14 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Callable; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree; import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode; import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory; +import org.opendaylight.mdsal.binding.dom.codec.util.BindingSchemaMapping; import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext; import org.opendaylight.mdsal.binding.model.api.GeneratedType; -import org.opendaylight.mdsal.binding.spec.naming.BindingMapping; import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.yangtools.concepts.Codec; import org.opendaylight.yangtools.concepts.Immutable; @@ -52,11 +55,9 @@ import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode; -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; import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; @@ -75,11 +76,11 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree private final SchemaRootCodecContext root; BindingCodecContext(final BindingRuntimeContext context, final BindingNormalizedNodeCodecRegistry registry) { - this.context = Preconditions.checkNotNull(context, "Binding Runtime Context is required."); + this.context = requireNonNull(context, "Binding Runtime Context is required."); this.root = SchemaRootCodecContext.create(this); this.identityCodec = new IdentityCodec(context); this.instanceIdentifierCodec = new InstanceIdentifierCodec(this); - this.registry = Preconditions.checkNotNull(registry); + this.registry = requireNonNull(registry); } @Override @@ -128,7 +129,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree DataContainerCodecContext currentNode = root; for (final InstanceIdentifier.PathArgument bindingArg : binding.getPathArguments()) { currentNode = currentNode.bindingPathArgumentChild(bindingArg, builder); - Preconditions.checkArgument(currentNode != null, "Supplied Instance Identifier %s is not valid.", binding); + checkArgument(currentNode != null, "Supplied Instance Identifier %s is not valid.", binding); } return currentNode; } @@ -145,15 +146,15 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree * binding representation (choice, case, leaf). * */ - @Nullable NodeCodecContext getCodecContextNode(final @Nonnull YangInstanceIdentifier dom, + @Nullable NodeCodecContext getCodecContextNode(final @NonNull YangInstanceIdentifier dom, final @Nullable Collection bindingArguments) { NodeCodecContext currentNode = root; ListNodeCodecContext currentList = null; for (final YangInstanceIdentifier.PathArgument domArg : dom.getPathArguments()) { - Preconditions.checkArgument(currentNode instanceof DataContainerCodecContext, + checkArgument(currentNode instanceof DataContainerCodecContext, "Unexpected child of non-container node %s", currentNode); - final DataContainerCodecContext previous = (DataContainerCodecContext) currentNode; + final DataContainerCodecContext previous = (DataContainerCodecContext) currentNode; final NodeCodecContext nextNode = previous.yangPathArgumentChild(domArg); /* @@ -165,7 +166,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree * Identifier as Item or IdentifiableItem */ if (currentList != null) { - Preconditions.checkArgument(currentList == nextNode, + checkArgument(currentList == nextNode, "List should be referenced two times in YANG Instance Identifier %s", dom); // We entered list, so now we have all information to emit @@ -183,13 +184,13 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree // We do not add path argument for choice, since // it is not supported by binding instance identifier. currentNode = nextNode; - } else if (nextNode instanceof DataContainerCodecContext) { + } else if (nextNode instanceof DataContainerCodecContext) { if (bindingArguments != null) { - bindingArguments.add(((DataContainerCodecContext) nextNode).getBindingPathArgument(domArg)); + bindingArguments.add(((DataContainerCodecContext) nextNode).getBindingPathArgument(domArg)); } currentNode = nextNode; } else if (nextNode instanceof LeafNodeCodecContext) { - LOG.debug("Instance identifier referencing a leaf is not representable (%s)", dom); + LOG.debug("Instance identifier referencing a leaf is not representable ({})", dom); return null; } } @@ -197,11 +198,11 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree // Algorithm ended in list as whole representation // we sill need to emit identifier for list if (currentNode instanceof ChoiceNodeCodecContext) { - LOG.debug("Instance identifier targeting a choice is not representable (%s)", dom); + LOG.debug("Instance identifier targeting a choice is not representable ({})", dom); return null; } if (currentNode instanceof CaseNodeCodecContext) { - LOG.debug("Instance identifier targeting a case is not representable (%s)", dom); + LOG.debug("Instance identifier targeting a case is not representable ({})", dom); return null; } @@ -232,28 +233,17 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree final Map getterToLeafSchema = new HashMap<>(); for (final DataSchemaNode leaf : childSchema.getChildNodes()) { if (leaf instanceof TypedDataSchemaNode) { - getterToLeafSchema.put(getGetterName(leaf, ((TypedDataSchemaNode) leaf).getType()), leaf); + getterToLeafSchema.put(BindingSchemaMapping.getGetterMethodName((TypedDataSchemaNode) leaf), leaf); } } return getLeafNodesUsingReflection(parentClass, getterToLeafSchema); } - private static String getGetterName(final SchemaNode node, final TypeDefinition typeDef) { - final String suffix = BindingMapping.getGetterSuffix(node.getQName()); - // Bug 8903: If it is a derived type of boolean or empty, not an inner type, then the return type - // of method would be the generated type of typedef not build-in types, so here it should be 'get'. - if ((typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) - && (typeDef.getPath().equals(node.getPath()) || typeDef.getBaseType() == null)) { - return "is" + suffix; - } - return "get" + suffix; - } - private ImmutableMap> getLeafNodesUsingReflection(final Class parentClass, final Map getterToLeafSchema) { final Map> leaves = new HashMap<>(); for (final Method method : parentClass.getMethods()) { - if (method.getParameterTypes().length == 0) { + if (method.getParameterCount() == 0) { final DataSchemaNode schema = getterToLeafSchema.get(method.getName()); final Class valueType; if (schema instanceof LeafSchemaNode) { @@ -282,8 +272,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree } private Codec getCodec(final Class valueType, final DataSchemaNode schema) { - Preconditions.checkArgument(schema instanceof TypedDataSchemaNode, "Unsupported leaf node type %s", schema); - + checkArgument(schema instanceof TypedDataSchemaNode, "Unsupported leaf node type %s", schema); return getCodec(valueType, ((TypedDataSchemaNode)schema).getType()); } @@ -323,7 +312,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree } else if (typeDef instanceof LeafrefTypeDefinition) { final Entry typeWithSchema = context.getTypeWithSchema(valueType); final WithStatus schema = typeWithSchema.getValue(); - Preconditions.checkState(schema instanceof TypeDefinition); + checkState(schema instanceof TypeDefinition); return getCodec(valueType, (TypeDefinition) schema); } return ValueTypeCodec.getCodecFor(valueType, typeDef);