* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.mdsal.binding.dom.adapter;
-import java.util.Collection;
-import java.util.Map;
+import static java.util.Objects.requireNonNull;
+
import java.util.Optional;
import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.mdsal.binding.dom.codec.util.AbstractBindingLazyContainerNode;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
/**
- * FIXME: Should this be moved to binding-data-codec.
+ * FIXME: This is a bit of functionality which should really live in binding-dom-codec, but for to happen we need
+ * to extends BindingNormalizedNodeCodecRegistry with the concept of a routing context -- which would be
+ * deprecated, as we want to move to actions in the long term.
*/
-class LazySerializedContainerNode implements ContainerNode, BindingDataAware {
-
- private final NodeIdentifier identifier;
- private final DataObject bindingData;
-
- private BindingNormalizedNodeCodecRegistry registry;
- private ContainerNode domData;
+class LazySerializedContainerNode extends AbstractBindingLazyContainerNode<DataObject,
+ BindingNormalizedNodeCodecRegistry> implements BindingDataAware {
private LazySerializedContainerNode(final QName identifier, final DataObject binding,
final BindingNormalizedNodeCodecRegistry registry) {
- this.identifier = NodeIdentifier.create(identifier);
- this.bindingData = binding;
- this.registry = registry;
- this.domData = null;
+ super(NodeIdentifier.create(identifier), binding, requireNonNull(registry));
}
- static NormalizedNode<?, ?> create(final SchemaPath rpcName, final DataObject data,
+ static ContainerNode create(final SchemaPath rpcName, final DataObject data,
final BindingNormalizedNodeCodecRegistry codec) {
- return new LazySerializedContainerNode(rpcName.getLastComponent(), data, codec);
+ return data == null ? null : new LazySerializedContainerNode(rpcName.getLastComponent(), data, codec);
}
- static NormalizedNode<?, ?> withContextRef(final SchemaPath rpcName, final DataObject data,
+ static ContainerNode withContextRef(final SchemaPath rpcName, final DataObject data,
final LeafNode<?> contextRef, final BindingNormalizedNodeCodecRegistry codec) {
return new WithContextRef(rpcName.getLastComponent(), data, contextRef, codec);
}
@Override
- public Map<QName, String> getAttributes() {
- return delegate().getAttributes();
- }
-
- private ContainerNode delegate() {
- if (domData == null) {
- domData = registry.toNormalizedNodeRpcData(bindingData);
- registry = null;
- }
- return domData;
- }
-
- @Override
- public final QName getNodeType() {
- return identifier.getNodeType();
- }
-
- @Override
- public final Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
- return delegate().getValue();
- }
-
- @Override
- public final NodeIdentifier getIdentifier() {
- return identifier;
- }
-
- @Override
- public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
- return delegate().getChild(child);
- }
-
- @Override
- public final Object getAttributeValue(final QName name) {
- return delegate().getAttributeValue(name);
+ public final DataObject bindingData() {
+ return getDataObject();
}
@Override
- public final DataObject bindingData() {
- return bindingData;
+ protected final ContainerNode computeContainerNode(final BindingNormalizedNodeCodecRegistry context) {
+ return context.toNormalizedNodeRpcData(getDataObject());
}
/**
* Lazy Serialized Node with pre-cached serialized leaf holding routing information.
- *
*/
private static final class WithContextRef extends LazySerializedContainerNode {
-
private final LeafNode<?> contextRef;
protected WithContextRef(final QName identifier, final DataObject binding, final LeafNode<?> contextRef,
final BindingNormalizedNodeCodecRegistry registry) {
super(identifier, binding, registry);
- this.contextRef = contextRef;
+ this.contextRef = requireNonNull(contextRef);
}
@Override
public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
- /*
- * Use precached value of routing field and do not run full serialization if we are
- * accessing it.
- */
- if (contextRef.getIdentifier().equals(child)) {
- return Optional.of(contextRef);
- }
- return super.getChild(child);
+ // Use pre-cached value of routing field and do not run full serialization if we are accessing it.
+ return contextRef.getIdentifier().equals(child) ? Optional.of(contextRef) : super.getChild(child);
}
}
-
}
return invoke0(rpcName, serialize(input));
}
- abstract NormalizedNode<?, ?> serialize(DataObject input);
+ abstract ContainerNode serialize(DataObject input);
final ListenableFuture<RpcResult<?>> invokeEmpty() {
return invoke0(rpcName, null);
return rpcName;
}
- ListenableFuture<RpcResult<?>> invoke0(final SchemaPath schemaPath, final NormalizedNode<?, ?> input) {
+ ListenableFuture<RpcResult<?>> invoke0(final SchemaPath schemaPath, final ContainerNode input) {
final ListenableFuture<DOMRpcResult> result = delegate.invokeRpc(schemaPath, input);
if (result instanceof BindingRpcFutureAware) {
return ((BindingRpcFutureAware) result).getBindingFuture();
}
@Override
- NormalizedNode<?, ?> serialize(final DataObject input) {
+ ContainerNode serialize(final DataObject input) {
return LazySerializedContainerNode.create(getRpcName(), input, codec.getCodecRegistry());
}
}
@Override
- NormalizedNode<?, ?> serialize(final DataObject input) {
+ ContainerNode serialize(final DataObject input) {
final InstanceIdentifier<?> bindingII = refExtractor.extract(input);
if (bindingII != null) {
final YangInstanceIdentifier yangII = codec.toYangInstanceIdentifierCached(bindingII);
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
rpcName = ((RpcDefinition) biMap.values().iterator().next()).getPath();
final LeafNode<?> leafNode = ImmutableLeafNodeBuilder.create().withNodeIdentifier(NodeIdentifier
.create(QName.create("", "test"))).withValue("").build();
- final NormalizedNode<?, ?> normalizedNode = LazySerializedContainerNode.create(rpcName, dataObject, codec);
+ final ContainerNode normalizedNode = LazySerializedContainerNode.create(rpcName, dataObject, codec);
assertNotNull(normalizedNode);
final LazySerializedContainerNode lazySerializedContainerNode =
(LazySerializedContainerNode) LazySerializedContainerNode.withContextRef(rpcName, dataObject, leafNode,
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.codec.api;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+
+/**
+ * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
+ *
+ * @param <T> Binding DataObject type
+ * @author Robert Varga
+ */
+@Beta
+@NonNullByDefault
+public interface BindingLazyContainerNode<T> extends ContainerNode, Delegator<ContainerNode> {
+ /**
+ * Returns the underlying DataObject.
+ *
+ * @return underlying DataObject.
+ */
+ T getDataObject();
+}
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.codec.util;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ForwardingObject;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Optional;
+import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+
+/**
+ * A {@link ContainerNode} backed by a binding {@link DataObject}, with lazy instantiation of the ContainerNode view.
+ *
+ * @param <T> Binding DataObject type
+ * @author Robert Varga
+ */
+@Beta
+@ThreadSafe
+public abstract class AbstractBindingLazyContainerNode<T extends DataObject, C> extends ForwardingObject
+ implements BindingLazyContainerNode<T> {
+ private final @NonNull NodeIdentifier identifier;
+ private final @NonNull T bindingData;
+
+ private volatile @Nullable ContainerNode delegate;
+ @GuardedBy("this")
+ private @Nullable C context;
+
+ protected AbstractBindingLazyContainerNode(final @NonNull NodeIdentifier identifier, final @NonNull T bindingData,
+ final C context) {
+ this.identifier = requireNonNull(identifier);
+ this.bindingData = requireNonNull(bindingData);
+ this.context = context;
+ }
+
+ public final @NonNull T getDataObject() {
+ return bindingData;
+ }
+
+ @Override
+ public final @NonNull NodeIdentifier getIdentifier() {
+ return identifier;
+ }
+
+ @Override
+ public final QName getNodeType() {
+ return identifier.getNodeType();
+ }
+
+ @Override
+ public final ContainerNode getDelegate() {
+ return delegate();
+ }
+
+ @Override
+ public Map<QName, String> getAttributes() {
+ return delegate().getAttributes();
+ }
+
+ @Override
+ public Object getAttributeValue(final QName name) {
+ return delegate().getAttributeValue(name);
+ }
+
+ @Override
+ public Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
+ return delegate().getValue();
+ }
+
+ @Override
+ public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
+ return delegate().getChild(child);
+ }
+
+ @Override
+ protected final @NonNull ContainerNode delegate() {
+ ContainerNode local = delegate;
+ if (local == null) {
+ synchronized (this) {
+ local = delegate;
+ if (local == null) {
+ local = delegate = requireNonNull(computeContainerNode(context));
+ context = null;
+ }
+ }
+ }
+
+ return local;
+ }
+
+ @GuardedBy("this")
+ protected abstract @NonNull ContainerNode computeContainerNode(C context);
+}
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
/**
* Interface defining a result of an operation invocation.
* @return Invocation result, absent if the operation has not produced a result. This might be the case if the
* operation does not produce a result, or if it failed.
*/
- Optional<NormalizedNode<?, ?>> getOutput();
+ Optional<ContainerNode> getOutput();
}