import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.concurrent.Callable;
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.api.BindingDataObjectCodecTreeNode;
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.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.EmptyTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
* binding representation (choice, case, leaf).
*
*/
- @Nullable NodeCodecContext<?> getCodecContextNode(final @NonNull YangInstanceIdentifier dom,
+ @Nullable BindingDataObjectCodecTreeNode<?> getCodecContextNode(final @NonNull YangInstanceIdentifier dom,
final @Nullable Collection<InstanceIdentifier.PathArgument> bindingArguments) {
- NodeCodecContext<?> currentNode = root;
+ NodeCodecContext currentNode = root;
ListNodeCodecContext<?> currentList = null;
for (final YangInstanceIdentifier.PathArgument domArg : dom.getPathArguments()) {
checkArgument(currentNode instanceof DataContainerCodecContext,
"Unexpected child of non-container node %s", currentNode);
final DataContainerCodecContext<?,?> previous = (DataContainerCodecContext<?, ?>) currentNode;
- final NodeCodecContext<?> nextNode = previous.yangPathArgumentChild(domArg);
+ final NodeCodecContext nextNode = previous.yangPathArgumentChild(domArg);
/*
* List representation in YANG Instance Identifier consists of two
}
return currentList;
}
- return currentNode;
+ if (currentNode != null) {
+ verify(currentNode instanceof BindingDataObjectCodecTreeNode, "Illegal return node %s for identifier %s",
+ currentNode, dom);
+ return (BindingDataObjectCodecTreeNode<?>) currentNode;
+ }
+ return null;
}
NotificationCodecContext<?> getNotificationContext(final SchemaPath notification) {
}
@Override
- public ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodes(final Class<?> parentClass,
+ public ImmutableMap<String, LeafNodeCodecContext> getLeafNodes(final Class<?> parentClass,
final DataNodeContainer childSchema) {
final Map<String, DataSchemaNode> getterToLeafSchema = new HashMap<>();
for (final DataSchemaNode leaf : childSchema.getChildNodes()) {
return getLeafNodesUsingReflection(parentClass, getterToLeafSchema);
}
- private ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodesUsingReflection(final Class<?> parentClass,
+ private ImmutableMap<String, LeafNodeCodecContext> getLeafNodesUsingReflection(final Class<?> parentClass,
final Map<String, DataSchemaNode> getterToLeafSchema) {
- final Map<String, LeafNodeCodecContext<?>> leaves = new HashMap<>();
+ final Map<String, LeafNodeCodecContext> leaves = new HashMap<>();
for (final Method method : parentClass.getMethods()) {
if (method.getParameterCount() == 0) {
final DataSchemaNode schema = getterToLeafSchema.get(method.getName());
+ if (!(schema instanceof TypedDataSchemaNode)) {
+ // We do not have schema for leaf, so we will ignore it (e.g. getClass).
+ continue;
+ }
+ final TypedDataSchemaNode typedSchema = (TypedDataSchemaNode) schema;
final Class<?> valueType;
if (schema instanceof LeafSchemaNode) {
valueType = method.getReturnType();
} else if (schema instanceof LeafListSchemaNode) {
- final Type genericType = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
+ final Optional<Type> optType = ClassLoaderUtils.getFirstGenericParameter(
+ method.getGenericReturnType());
+ checkState(optType.isPresent(), "Failed to find return type for %s", method);
+ final Type genericType = optType.get();
if (genericType instanceof Class<?>) {
valueType = (Class<?>) genericType;
} else if (genericType instanceof ParameterizedType) {
throw new IllegalStateException("Unexpected return type " + genericType);
}
} else {
- // We do not have schema for leaf, so we will ignore it (e.g. getClass).
- continue;
+ throw new IllegalStateException("Unhandled typed schema " + typedSchema);
}
- final Codec<Object, Object> codec = getCodec(valueType, schema);
- final LeafNodeCodecContext<?> leafNode = new LeafNodeCodecContext<>(schema, codec, method,
+
+ final Codec<Object, Object> codec = getCodec(valueType, typedSchema.getType());
+ final LeafNodeCodecContext leafNode = new LeafNodeCodecContext(typedSchema, codec, method,
context.getSchemaContext());
leaves.put(schema.getQName().getLocalName(), leafNode);
}
return ImmutableMap.copyOf(leaves);
}
- private Codec<Object, Object> getCodec(final Class<?> valueType, final DataSchemaNode schema) {
- checkArgument(schema instanceof TypedDataSchemaNode, "Unsupported leaf node type %s", schema);
- return getCodec(valueType, ((TypedDataSchemaNode)schema).getType());
- }
-
Codec<Object, Object> getCodec(final Class<?> valueType, final TypeDefinition<?> instantiatedType) {
if (Class.class.equals(valueType)) {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked", "rawtypes" })
final Codec<Object, Object> casted = (Codec) instanceIdentifierCodec;
return casted;
- } else if (Boolean.class.equals(valueType)) {
- if (instantiatedType instanceof EmptyTypeDefinition) {
- return ValueTypeCodec.EMPTY_CODEC;
- }
} else if (BindingReflections.isBindingClass(valueType)) {
return getCodecForBindingClass(valueType, instantiatedType);
}
@Override
public IdentifiableItemCodec getPathArgumentCodec(final Class<?> listClz, final ListSchemaNode schema) {
- final Class<? extends Identifier<?>> identifier = ClassLoaderUtils.findFirstGenericArgument(listClz,
+ final Optional<Class<Identifier<?>>> optIdentifier = ClassLoaderUtils.findFirstGenericArgument(listClz,
Identifiable.class);
+ checkState(optIdentifier.isPresent(), "Failed to find identifier for %s", listClz);
+
+ final Class<Identifier<?>> identifier = optIdentifier.get();
final Map<QName, ValueContext> valueCtx = new HashMap<>();
- for (final LeafNodeCodecContext<?> leaf : getLeafNodes(identifier, schema).values()) {
+ for (final LeafNodeCodecContext leaf : getLeafNodes(identifier, schema).values()) {
final QName name = leaf.getDomPathArgument().getNodeType();
valueCtx.put(name, new ValueContext(identifier, leaf));
}
@SuppressWarnings("unchecked")
@Override
- public <T extends DataObject> BindingCodecTreeNode<T> getSubtreeCodec(final InstanceIdentifier<T> path) {
+ public <T extends DataObject> BindingDataObjectCodecTreeNode<T> getSubtreeCodec(final InstanceIdentifier<T> path) {
// TODO Do we need defensive check here?
- return (BindingCodecTreeNode<T>) getCodecContextNode(path, null);
+ return (BindingDataObjectCodecTreeNode<T>) getCodecContextNode(path, null);
}
@Override
- public BindingCodecTreeNode<?> getSubtreeCodec(final YangInstanceIdentifier path) {
+ public BindingCodecTreeNode getSubtreeCodec(final YangInstanceIdentifier path) {
return getCodecContextNode(path, null);
}
@Override
- public BindingCodecTreeNode<?> getSubtreeCodec(final SchemaPath path) {
+ public BindingCodecTreeNode getSubtreeCodec(final SchemaPath path) {
throw new UnsupportedOperationException("Not implemented yet.");
}
}