2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.serialized;
10 import java.util.Collection;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
15 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 * Serialize operation from binding to DOM.
28 public class LazySerializedContainerNode implements ContainerNode {
30 private final NodeIdentifier identifier;
31 private final TreeNode bindingData;
33 private BindingNormalizedNodeCodecRegistry registry;
34 private ContainerNode domData;
36 private LazySerializedContainerNode(final QName identifier, final TreeNode binding,
37 final BindingNormalizedNodeCodecRegistry registry) {
38 this.identifier = NodeIdentifier.create(identifier);
39 this.bindingData = binding;
40 this.registry = registry;
45 * Prepare serializer of binding data with specific codec.
47 * @param operationName
48 * - qname of operation
52 * - specifc codec for operation
53 * @return instance of lazy serialized container node
55 public static NormalizedNode<?, ?> create(final SchemaPath operationName, final TreeNode data,
56 final BindingNormalizedNodeCodecRegistry codec) {
57 return new LazySerializedContainerNode(operationName.getLastComponent(), data, codec);
61 * Prepare serializer of binding data with specific codec and pre-cached serialized leaf holding routing
64 * @param operationName
69 * - leaf context reference
72 * @return insntance of lazy serialized container node with pre-cached serialized leaf
74 public static NormalizedNode<?, ?> withContextRef(final SchemaPath operationName, final TreeNode data,
75 final LeafNode<?> contextRef, final BindingNormalizedNodeCodecRegistry codec) {
76 return new WithContextRef(operationName.getLastComponent(), data, contextRef, codec);
80 public Map<QName, String> getAttributes() {
81 return delegate().getAttributes();
84 private ContainerNode delegate() {
85 if (domData == null) {
86 domData = registry.toNormalizedNodeOperationData(bindingData);
93 public final QName getNodeType() {
94 return identifier.getNodeType();
98 public final Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
99 return delegate().getValue();
104 public final NodeIdentifier getIdentifier() {
109 public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
110 return delegate().getChild(child);
114 public final Object getAttributeValue(final QName name) {
115 return delegate().getAttributeValue(name);
121 * @return binding data.
123 public final TreeNode bindingData() {
128 * Lazy Serialized Node with pre-cached serialized leaf holding routing information.
131 private static final class WithContextRef extends LazySerializedContainerNode {
133 private final LeafNode<?> contextRef;
135 private WithContextRef(final QName identifier, final TreeNode binding, final LeafNode<?> contextRef,
136 final BindingNormalizedNodeCodecRegistry registry) {
137 super(identifier, binding, registry);
138 this.contextRef = contextRef;
142 public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
144 * Use precached value of routing field and do not run full serialization if we are accessing it.
146 if (contextRef.getIdentifier().equals(child)) {
147 return Optional.of(contextRef);
149 return super.getChild(child);