Adopt yangtools-11.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / LazySerializedContainerNode.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
14 import org.opendaylight.mdsal.binding.dom.codec.spi.AbstractBindingLazyContainerNode;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
20
21 /*
22  * FIXME: This is a bit of functionality which should really live in binding-dom-codec, but for to happen we need
23  *        to extend BindingNormalizedNodeSerializer with the concept of a routing context -- which would be deprecated,
24  *        as we want to move to actions in the long term.
25  *
26  *        Even then, BindingNormalizedNodeCodecRegistry provides background updates to the context used in
27  *        deserialization, which is currently being used.
28  */
29 class LazySerializedContainerNode
30         extends AbstractBindingLazyContainerNode<DataObject, BindingNormalizedNodeSerializer> {
31
32     private LazySerializedContainerNode(final @NonNull NodeIdentifier identifier, final DataObject binding,
33             final BindingNormalizedNodeSerializer codec) {
34         super(identifier, binding, requireNonNull(codec));
35     }
36
37     static ContainerNode create(final @NonNull NodeIdentifier identifier, final DataObject data,
38             final BindingNormalizedNodeSerializer codec) {
39         return data == null ? null : new LazySerializedContainerNode(identifier, data, codec);
40     }
41
42     static ContainerNode withContextRef(final @NonNull NodeIdentifier identifier, final DataObject data,
43             final LeafNode<?> contextRef, final BindingNormalizedNodeSerializer serializer) {
44         return new WithContextRef(identifier, data, contextRef, serializer);
45     }
46
47     @Override
48     protected final ContainerNode computeContainerNode(final BindingNormalizedNodeSerializer context) {
49         return context.toNormalizedNodeRpcData(getDataObject());
50     }
51
52     /**
53      * Lazy Serialized Node with pre-cached serialized leaf holding routing information.
54      */
55     private static final class WithContextRef extends LazySerializedContainerNode {
56         private final LeafNode<?> contextRef;
57
58         protected WithContextRef(final @NonNull NodeIdentifier identifier, final DataObject binding,
59                 final LeafNode<?> contextRef, final BindingNormalizedNodeSerializer codec) {
60             super(identifier, binding, codec);
61             this.contextRef = requireNonNull(contextRef);
62         }
63
64         @Override
65         public DataContainerChild childByArg(final NodeIdentifier child) {
66             // Use pre-cached value of routing field and do not run full serialization if we are accessing it.
67             return contextRef.getIdentifier().equals(child) ? contextRef : super.childByArg(child);
68         }
69     }
70 }