Define a feature-parent
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / leafref / LeafRefContextBuilder.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.yangtools.yang.data.tree.leafref;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21
22 final class LeafRefContextBuilder implements Mutable {
23     private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
24     private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
25     private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
26
27     private final QName currentNodeQName;
28     private final ImmutableList<QName> currentNodePath;
29     private final EffectiveModelContext modelContext;
30
31     private LeafRefPath leafRefTargetPath = null;
32     private LeafRefPath absoluteLeafRefTargetPath = null;
33     private String leafRefTargetPathString = "";
34
35     private boolean isReferencedBy = false;
36     private boolean isReferencing = false;
37
38     LeafRefContextBuilder(final QName currentNodeQName, final ImmutableList<QName> currentNodePath,
39             final EffectiveModelContext modelContext) {
40         this.currentNodeQName = requireNonNull(currentNodeQName);
41         this.currentNodePath = requireNonNull(currentNodePath);
42         this.modelContext = requireNonNull(modelContext);
43     }
44
45     @NonNull LeafRefContext build() {
46         final LeafRefContext leafRefContext = new LeafRefContext(this);
47
48         // LeafRefContext has made a copy of these
49         referencingChildren.clear();
50         referencedByChildren.clear();
51         referencedByLeafRefCtx.clear();
52
53         return leafRefContext;
54     }
55
56     boolean isReferencedBy() {
57         return isReferencedBy;
58     }
59
60     void setReferencedBy(final boolean referencedBy) {
61         isReferencedBy = referencedBy;
62     }
63
64     boolean isReferencing() {
65         return isReferencing;
66     }
67
68     void setReferencing(final boolean referencing) {
69         isReferencing = referencing;
70     }
71
72     void addReferencingChild(final LeafRefContext child, final QName childQName) {
73         referencingChildren.put(childQName, child);
74     }
75
76     Map<QName, LeafRefContext> getReferencingChilds() {
77         return referencingChildren;
78     }
79
80     void addReferencedByChild(final LeafRefContext child, final QName childQName) {
81         referencedByChildren.put(childQName, child);
82     }
83
84     Map<QName, LeafRefContext> getReferencedByChilds() {
85         return referencedByChildren;
86     }
87
88     ImmutableList<QName> getCurrentNodePath() {
89         return currentNodePath;
90     }
91
92     LeafRefPath getLeafRefTargetPath() {
93         return leafRefTargetPath;
94     }
95
96     void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
97         leafRefTargetPath = requireNonNull(leafRefPath);
98     }
99
100     String getLeafRefTargetPathString() {
101         return leafRefTargetPathString;
102     }
103
104     void setLeafRefTargetPathString(final String leafRefPathString) {
105         leafRefTargetPathString = requireNonNull(leafRefPathString);
106     }
107
108     QName getCurrentNodeQName() {
109         return currentNodeQName;
110     }
111
112     EffectiveModelContext modelContext() {
113         return modelContext;
114     }
115
116     LeafRefPath getAbsoluteLeafRefTargetPath() {
117         if (isReferencing && absoluteLeafRefTargetPath == null) {
118             if (leafRefTargetPath.isAbsolute()) {
119                 absoluteLeafRefTargetPath = leafRefTargetPath;
120             } else {
121                 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
122                     currentNodePath, getLeafRefContextModule());
123             }
124         }
125
126         return absoluteLeafRefTargetPath;
127     }
128
129     Module getLeafRefContextModule() {
130         final QNameModule qnameModule = currentNodePath.isEmpty() ? currentNodeQName.getModule()
131             : currentNodePath.get(0).getModule();
132         return modelContext.findModule(qnameModule).orElse(null);
133     }
134
135     void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
136         referencedByLeafRefCtx.put(qname, leafRef);
137     }
138
139     LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
140         return referencedByLeafRefCtx.get(qname);
141     }
142
143     Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
144         return referencedByLeafRefCtx;
145     }
146 }