import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;
+import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeWriterFactory;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingStreamEventWriter;
import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
-import org.opendaylight.mdsal.binding.dom.codec.impl.loader.CodecClassLoader;
import org.opendaylight.mdsal.binding.dom.codec.spi.AbstractBindingNormalizedNodeSerializer;
import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
import org.opendaylight.mdsal.binding.dom.codec.spi.BindingSchemaMapping;
+import org.opendaylight.mdsal.binding.loader.BindingClassLoader;
import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.concepts.Delegator;
-import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.util.ClassLoaderUtils;
import org.opendaylight.yangtools.yang.binding.Action;
}
private static final Logger LOG = LoggerFactory.getLogger(BindingCodecContext.class);
+ private static final File BYTECODE_DIRECTORY;
+
+ static {
+ final String dir = System.getProperty("org.opendaylight.mdsal.binding.dom.codec.loader.bytecodeDumpDirectory");
+ BYTECODE_DIRECTORY = Strings.isNullOrEmpty(dir) ? null : new File(dir);
+ }
private final LoadingCache<Class<?>, DataObjectStreamer<?>> streamers = CacheBuilder.newBuilder().build(
new CacheLoader<Class<?>, DataObjectStreamer<?>>() {
}
});
- private final @NonNull CodecClassLoader loader = CodecClassLoader.create();
+ private final @NonNull BindingClassLoader loader =
+ BindingClassLoader.create(BindingCodecContext.class, BYTECODE_DIRECTORY);
private final @NonNull InstanceIdentifierCodec instanceIdentifierCodec;
private final @NonNull IdentityCodec identityCodec;
private final @NonNull BindingRuntimeContext context;
}
@Override
- public CodecClassLoader getLoader() {
+ public BindingClassLoader getLoader() {
return loader;
}
@Override
public Entry<YangInstanceIdentifier, BindingStreamEventWriter> newWriterAndIdentifier(
final InstanceIdentifier<?> path, final NormalizedNodeStreamWriter domWriter) {
- final List<YangInstanceIdentifier.PathArgument> yangArgs = new LinkedList<>();
- final DataContainerCodecContext<?,?> codecContext = getCodecContextNode(path, yangArgs);
- return Map.entry(YangInstanceIdentifier.create(yangArgs), codecContext.createWriter(domWriter));
+ final var yangArgs = new ArrayList<YangInstanceIdentifier.PathArgument>();
+ final var codecContext = getCodecContextNode(path, yangArgs);
+ return Map.entry(YangInstanceIdentifier.create(yangArgs),
+ new BindingToNormalizedStreamWriter(codecContext, domWriter));
}
@Override
public BindingStreamEventWriter newWriter(final InstanceIdentifier<?> path,
final NormalizedNodeStreamWriter domWriter) {
- return getCodecContextNode(path, null).createWriter(domWriter);
+ return new BindingToNormalizedStreamWriter(getCodecContextNode(path, null), domWriter);
}
@Override
public BindingStreamEventWriter newRpcWriter(final Class<? extends DataContainer> rpcInputOrOutput,
final NormalizedNodeStreamWriter domWriter) {
- return root.getRpc(rpcInputOrOutput).createWriter(domWriter);
+ return new BindingToNormalizedStreamWriter(root.getRpc(rpcInputOrOutput), domWriter);
}
@Override
public BindingStreamEventWriter newNotificationWriter(final Class<? extends Notification<?>> notification,
final NormalizedNodeStreamWriter domWriter) {
- return root.getNotification(notification).createWriter(domWriter);
+ return new BindingToNormalizedStreamWriter(root.getNotification(notification), domWriter);
}
@Override
public BindingStreamEventWriter newActionInputWriter(final Class<? extends Action<?, ?, ?>> action,
final NormalizedNodeStreamWriter domWriter) {
- return getActionCodec(action).input().createWriter(domWriter);
+ return new BindingToNormalizedStreamWriter(getActionCodec(action).input(), domWriter);
}
@Override
public BindingStreamEventWriter newActionOutputWriter(final Class<? extends Action<?, ?, ?>> action,
final NormalizedNodeStreamWriter domWriter) {
- return getActionCodec(action).output().createWriter(domWriter);
+ return new BindingToNormalizedStreamWriter(getActionCodec(action).output(), domWriter);
}
DataContainerCodecContext<?,?> getCodecContextNode(final InstanceIdentifier<?> binding,
NodeCodecContext currentNode = root;
ListNodeCodecContext<?> currentList = null;
- for (final YangInstanceIdentifier.PathArgument domArg : dom.getPathArguments()) {
+ for (var 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 var previous = (DataContainerCodecContext<?, ?>) currentNode;
+ final var nextNode = previous.yangPathArgumentChild(domArg);
/*
* List representation in YANG Instance Identifier consists of two
}
currentList = null;
currentNode = nextNode;
- } else if (nextNode instanceof ListNodeCodecContext) {
+ } else if (nextNode instanceof ListNodeCodecContext<?> listNode) {
// We enter list, we do not update current Node yet,
// since we need to verify
- currentList = (ListNodeCodecContext<?>) nextNode;
+ currentList = listNode;
} else if (nextNode instanceof ChoiceNodeCodecContext) {
// 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<?, ?> containerNode) {
if (bindingArguments != null) {
- bindingArguments.add(((DataContainerCodecContext<?, ?>) nextNode).getBindingPathArgument(domArg));
+ bindingArguments.add(containerNode.getBindingPathArgument(domArg));
}
currentNode = nextNode;
} else if (nextNode instanceof ValueNodeCodecContext) {
@Override
public ImmutableMap<Method, ValueNodeCodecContext> getLeafNodes(final Class<?> type,
final EffectiveStatement<?, ?> schema) {
- final Map<String, DataSchemaNode> getterToLeafSchema = new HashMap<>();
+ final var getterToLeafSchema = new HashMap<String, DataSchemaNode>();
for (var stmt : schema.effectiveSubstatements()) {
- if (stmt instanceof TypedDataSchemaNode) {
- putLeaf(getterToLeafSchema, (TypedDataSchemaNode) stmt);
- } else if (stmt instanceof AnydataSchemaNode) {
- putLeaf(getterToLeafSchema, (AnydataSchemaNode) stmt);
- } else if (stmt instanceof AnyxmlSchemaNode) {
- putLeaf(getterToLeafSchema, (AnyxmlSchemaNode) stmt);
+ if (stmt instanceof TypedDataSchemaNode typedSchema) {
+ putLeaf(getterToLeafSchema, typedSchema);
+ } else if (stmt instanceof AnydataSchemaNode anydataSchema) {
+ putLeaf(getterToLeafSchema, anydataSchema);
+ } else if (stmt instanceof AnyxmlSchemaNode anyxmlSchema) {
+ putLeaf(getterToLeafSchema, anyxmlSchema);
}
}
return getLeafNodesUsingReflection(type, getterToLeafSchema);
private ImmutableMap<Method, ValueNodeCodecContext> getLeafNodesUsingReflection(
final Class<?> parentClass, final Map<String, DataSchemaNode> getterToLeafSchema) {
- final Map<Method, ValueNodeCodecContext> leaves = new HashMap<>();
- for (final Method method : parentClass.getMethods()) {
+ final var leaves = new HashMap<Method, ValueNodeCodecContext>();
+ for (var method : parentClass.getMethods()) {
// Only consider non-bridge methods with no arguments
if (method.getParameterCount() == 0 && !method.isBridge()) {
final DataSchemaNode schema = getterToLeafSchema.get(method.getName());
final ValueNodeCodecContext valueNode;
- if (schema instanceof LeafSchemaNode) {
- final LeafSchemaNode leafSchema = (LeafSchemaNode) schema;
-
+ if (schema instanceof LeafSchemaNode leafSchema) {
// FIXME: MDSAL-670: this is not right as we need to find a concrete type, but this may return
// Object.class
final Class<?> valueType = method.getReturnType();
- final IllegalArgumentCodec<Object, Object> codec = getCodec(valueType, leafSchema.getType());
+ final ValueCodec<Object, Object> codec = getCodec(valueType, leafSchema.getType());
valueNode = LeafNodeCodecContext.of(leafSchema, codec, method.getName(), valueType,
context.getEffectiveModelContext());
- } else if (schema instanceof LeafListSchemaNode) {
+ } else if (schema instanceof LeafListSchemaNode leafListSchema) {
final Optional<Type> optType = ClassLoaderUtils.getFirstGenericParameter(
method.getGenericReturnType());
checkState(optType.isPresent(), "Failed to find return type for %s", method);
final Class<?> valueType;
- final Type genericType = optType.get();
- if (genericType instanceof Class<?>) {
- valueType = (Class<?>) genericType;
- } else if (genericType instanceof ParameterizedType) {
- valueType = (Class<?>) ((ParameterizedType) genericType).getRawType();
+ final Type genericType = optType.orElseThrow();
+ if (genericType instanceof Class<?> clazz) {
+ valueType = clazz;
+ } else if (genericType instanceof ParameterizedType parameterized) {
+ valueType = (Class<?>) parameterized.getRawType();
} else if (genericType instanceof WildcardType) {
// FIXME: MDSAL-670: this is not right as we need to find a concrete type
valueType = Object.class;
throw new IllegalStateException("Unexpected return type " + genericType);
}
- final LeafListSchemaNode leafListSchema = (LeafListSchemaNode) schema;
- final IllegalArgumentCodec<Object, Object> codec = getCodec(valueType, leafListSchema.getType());
+ final ValueCodec<Object, Object> codec = getCodec(valueType, leafListSchema.getType());
valueNode = new LeafSetNodeCodecContext(leafListSchema, codec, method.getName());
- } else if (schema instanceof AnyxmlSchemaNode) {
- valueNode = new OpaqueNodeCodecContext.Anyxml<>((AnyxmlSchemaNode) schema, method.getName(),
+ } else if (schema instanceof AnyxmlSchemaNode anyxmlSchema) {
+ valueNode = new OpaqueNodeCodecContext.Anyxml<>(anyxmlSchema, method.getName(),
opaqueReturnType(method), loader);
- } else if (schema instanceof AnydataSchemaNode) {
- valueNode = new OpaqueNodeCodecContext.Anydata<>((AnydataSchemaNode) schema, method.getName(),
+ } else if (schema instanceof AnydataSchemaNode anydataSchema) {
+ valueNode = new OpaqueNodeCodecContext.Anydata<>(anydataSchema, method.getName(),
opaqueReturnType(method), loader);
} else {
verify(schema == null, "Unhandled schema %s for method %s", schema, method);
}
// FIXME: this is probably not right w.r.t. nulls
- IllegalArgumentCodec<Object, Object> getCodec(final Class<?> valueType, final TypeDefinition<?> instantiatedType) {
+ ValueCodec<Object, Object> getCodec(final Class<?> valueType, final TypeDefinition<?> instantiatedType) {
if (BaseIdentity.class.isAssignableFrom(valueType)) {
@SuppressWarnings({ "unchecked", "rawtypes" })
- final IllegalArgumentCodec<Object, Object> casted = (IllegalArgumentCodec) identityCodec;
+ final ValueCodec<Object, Object> casted = (ValueCodec) identityCodec;
return casted;
} else if (InstanceIdentifier.class.equals(valueType)) {
@SuppressWarnings({ "unchecked", "rawtypes" })
- final IllegalArgumentCodec<Object, Object> casted = (IllegalArgumentCodec) instanceIdentifierCodec;
+ final ValueCodec<Object, Object> casted = (ValueCodec) instanceIdentifierCodec;
return casted;
} else if (BindingReflections.isBindingClass(valueType)) {
return getCodecForBindingClass(valueType, instantiatedType);
@SuppressWarnings("checkstyle:illegalCatch")
// FIXME: this is probably not right w.r.t. nulls
- private IllegalArgumentCodec<Object, Object> getCodecForBindingClass(final Class<?> valueType,
+ private ValueCodec<Object, Object> getCodecForBindingClass(final Class<?> valueType,
final TypeDefinition<?> typeDef) {
if (typeDef instanceof IdentityrefTypeDefinition) {
return new CompositeValueCodec.OfIdentity(valueType, identityCodec);
} else if (typeDef instanceof InstanceIdentifierTypeDefinition) {
return new CompositeValueCodec.OfInstanceIdentifier(valueType, instanceIdentifierCodec);
- } else if (typeDef instanceof UnionTypeDefinition) {
+ } else if (typeDef instanceof UnionTypeDefinition unionType) {
try {
- return UnionTypeCodec.of(valueType, (UnionTypeDefinition) typeDef, this);
+ return UnionTypeCodec.of(valueType, unionType, this);
} catch (Exception e) {
throw new IllegalStateException("Unable to load codec for " + valueType, e);
}
final var typeWithSchema = context.getTypeWithSchema(valueType);
final var schema = typeWithSchema.statement();
final TypeDefinition<?> def;
- if (schema instanceof TypeDefinitionAware) {
- def = ((TypeDefinitionAware) schema).getTypeDefinition();
- } else if (schema instanceof TypeAware) {
- def = ((TypeAware) schema).getType();
+ if (schema instanceof TypeDefinitionAware typeDefAware) {
+ def = typeDefAware.getTypeDefinition();
+ } else if (schema instanceof TypeAware typeAware) {
+ def = typeAware.getType();
} else {
throw new IllegalStateException("Unexpected schema " + schema);
}
Identifiable.class);
checkState(optIdentifier.isPresent(), "Failed to find identifier for %s", listClz);
- final Class<Identifier<?>> identifier = optIdentifier.get();
+ final Class<Identifier<?>> identifier = optIdentifier.orElseThrow();
final Map<QName, ValueContext> valueCtx = new HashMap<>();
for (final ValueNodeCodecContext leaf : getLeafNodes(identifier, type.statement()).values()) {
final QName name = leaf.getDomPathArgument().getNodeType();