a0e45302e091674a239ef7919174a288c219a536
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / 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
9 package org.opendaylight.controller.md.sal.binding.impl;
10
11 import com.google.common.base.Optional;
12 import java.util.Collection;
13 import java.util.Map;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
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;
24
25 /**
26  *
27  * FIXME: Should be this moved to binding-data-codec?
28  *
29  */
30 class LazySerializedContainerNode implements ContainerNode {
31
32     private final NodeIdentifier identifier;
33     private final DataObject bindingData;
34
35     private BindingNormalizedNodeSerializer registry;
36     private ContainerNode domData;
37
38     private LazySerializedContainerNode(final QName identifier, final DataObject binding,
39             final BindingNormalizedNodeSerializer registry) {
40         this.identifier = new NodeIdentifier(identifier);
41         this.bindingData = binding;
42         this.registry = registry;
43         this.domData = null;
44     }
45
46     static NormalizedNode<?, ?> create(final SchemaPath rpcName, final DataObject data,
47             final BindingNormalizedNodeSerializer codec) {
48         return new LazySerializedContainerNode(rpcName.getLastComponent(), data, codec);
49     }
50
51     static NormalizedNode<?, ?> withContextRef(final SchemaPath rpcName, final DataObject data,
52             final LeafNode<?> contextRef, final BindingNormalizedNodeSerializer codec) {
53         return new WithContextRef(rpcName.getLastComponent(), data, contextRef, codec);
54     }
55
56     @Override
57     public Map<QName, String> getAttributes() {
58         return delegate().getAttributes();
59     }
60
61     private ContainerNode delegate() {
62         if (domData == null) {
63             domData = registry.toNormalizedNodeRpcData(bindingData);
64             registry = null;
65         }
66         return domData;
67     }
68
69     @Override
70     public final QName getNodeType() {
71         return identifier.getNodeType();
72     }
73
74     @Override
75     public final Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
76         return delegate().getValue();
77     }
78
79     @Override
80     public final NodeIdentifier getIdentifier() {
81         return identifier;
82     }
83
84     @Override
85     public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
86         return delegate().getChild(child);
87     }
88
89     @Override
90     public final Object getAttributeValue(final QName name) {
91         return delegate().getAttributeValue(name);
92     }
93
94     final DataObject bindingData() {
95         return bindingData;
96     }
97
98     /**
99      * Lazy Serialized Node with pre-cached serialized leaf holding routing information.
100      *
101      */
102     private static final class WithContextRef extends LazySerializedContainerNode {
103
104         private final LeafNode<?> contextRef;
105
106         protected WithContextRef(final QName identifier, final DataObject binding, final LeafNode<?> contextRef,
107                 final BindingNormalizedNodeSerializer registry) {
108             super(identifier, binding, registry);
109             this.contextRef = contextRef;
110         }
111
112         @Override
113         public Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
114             /*
115              * Use precached value of routing field and do not run full serialization if we are
116              * accessing it.
117              */
118             if (contextRef.getIdentifier().equals(child)) {
119                 return Optional.<DataContainerChild<? extends PathArgument, ?>>of(contextRef);
120             }
121             return super.getChild(child);
122         }
123     }
124
125 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.