package org.opendaylight.mdsal.binding.dom.codec.impl;
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 com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.common.base.Verify;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
+import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
+import java.util.Optional;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.util.ClassLoaderUtils;
import org.opendaylight.yangtools.yang.binding.Action;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
import org.opendaylight.yangtools.yang.binding.ChoiceIn;
import org.opendaylight.yangtools.yang.binding.DataContainer;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedListAction;
import org.opendaylight.yangtools.yang.binding.Notification;
import org.opendaylight.yangtools.yang.binding.RpcInput;
import org.opendaylight.yangtools.yang.binding.RpcOutput;
}
ActionCodecContext createActionContext(final Class<? extends Action<?, ?, ?>> action) {
- final Type[] args = ClassLoaderUtils.findParameterizedType(action, Action.class).getActualTypeArguments();
- checkArgument(args.length == 3, "Unexpected (%s) Action generatic arguments", args.length);
+ if (KeyedListAction.class.isAssignableFrom(action)) {
+ return prepareActionContext(2, 3, 4, action, KeyedListAction.class);
+ } else if (Action.class.isAssignableFrom(action)) {
+ return prepareActionContext(1, 2, 3, action, Action.class);
+ }
+ throw new IllegalArgumentException("The specific action type does not exist for action " + action.getName());
+ }
+ private ActionCodecContext prepareActionContext(final int inputOffset, final int outputOffset,
+ final int expectedArgsLength, final Class<? extends Action<?, ?, ?>> action, final Class<?> actionType) {
+ final Optional<ParameterizedType> optParamType = ClassLoaderUtils.findParameterizedType(action, actionType);
+ checkState(optParamType.isPresent(), "%s does not specialize %s", action, actionType);
+
+ final ParameterizedType paramType = optParamType.get();
+ final Type[] args = paramType.getActualTypeArguments();
+ checkArgument(args.length == expectedArgsLength, "Unexpected (%s) Action generatic arguments", args.length);
final ActionDefinition schema = factory().getRuntimeContext().getActionDefinition(action);
return new ActionCodecContext(
- DataContainerCodecPrototype.from(asClass(args[1], RpcInput.class), schema.getInput(), factory()).get(),
- DataContainerCodecPrototype.from(asClass(args[2], RpcOutput.class), schema.getOutput(), factory()).get());
+ DataContainerCodecPrototype.from(asClass(args[inputOffset], RpcInput.class), schema.getInput(),
+ factory()).get(),
+ DataContainerCodecPrototype.from(asClass(args[outputOffset], RpcOutput.class), schema.getOutput(),
+ factory()).get());
}
private static <T extends DataObject> Class<? extends T> asClass(final Type type, final Class<T> target) {
.orElseThrow(() -> new IllegalArgumentException("Failed to find module for " + qnameModule));
final String className = BindingMapping.getClassName(qname);
- RpcDefinition rpc = null;
for (final RpcDefinition potential : module.getRpcs()) {
final QName potentialQName = potential.getQName();
/*
- * Check if rpc and class represents data from same module and then
- * checks if rpc local name produces same class name as class name
- * appended with Input/Output based on QName associated with bidning
- * class.
+ * Check if rpc and class represents data from same module and then checks if rpc local name produces same
+ * class name as class name appended with Input/Output based on QName associated with binding class.
*
- * FIXME: Rework this to have more precise logic regarding Binding
- * Specification.
+ * FIXME: Rework this to have more precise logic regarding Binding Specification.
*/
if (key.getSimpleName().equals(BindingMapping.getClassName(potentialQName) + className)) {
- rpc = potential;
- break;
+ final ContainerSchemaNode schema = SchemaNodeUtils.getRpcDataSchema(potential, qname);
+ checkArgument(schema != null, "Schema for %s does not define input / output.", potential.getQName());
+ return (ContainerNodeCodecContext<?>) DataContainerCodecPrototype.from(key, schema, factory()).get();
}
}
- checkArgument(rpc != null, "Supplied class %s is not valid RPC class.", key);
- final ContainerSchemaNode schema = SchemaNodeUtils.getRpcDataSchema(rpc, qname);
- checkArgument(schema != null, "Schema for %s does not define input / output.", rpc.getQName());
- return (ContainerNodeCodecContext<?>) DataContainerCodecPrototype.from(key, schema, factory()).get();
+
+ throw new IllegalArgumentException("Supplied class " + key + " is not valid RPC class.");
}
NotificationCodecContext<?> createNotificationDataContext(final Class<?> notificationType) {
@Override
public DataContainerCodecContext<?, ?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
final List<PathArgument> builder) {
- final java.util.Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
+ final Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
if (caseType.isPresent()) {
- final Class<? extends DataObject> type = caseType.get();
- final ChoiceNodeCodecContext<?> choice = choicesByClass.getUnchecked(type);
+ // XXX: we use two caseType.get()s because of https://bugs.openjdk.java.net/browse/JDK-8144185,
+ // which makes JaCoCo blow up if we try using @NonNull on the local variable.
+ final ChoiceNodeCodecContext<?> choice = choicesByClass.getUnchecked(caseType.get());
choice.addYangPathArgument(arg, builder);
- final DataContainerCodecContext<?, ?> caze = choice.streamChild(type);
+ final DataContainerCodecContext<?, ?> caze = choice.streamChild(caseType.get());
caze.addYangPathArgument(arg, builder);
return caze.bindingPathArgumentChild(arg, builder);
}