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