YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / 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.impl.leafref;
9
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.Map;
13 import org.opendaylight.yangtools.concepts.Builder;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19
20 class LeafRefContextBuilder implements Builder<LeafRefContext> {
21
22     private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
23     private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
24     private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
25
26     private QName currentNodeQName;
27     private SchemaPath currentNodePath;
28     private SchemaContext schemaContext;
29
30     private LeafRefPath leafRefTargetPath = null;
31     private LeafRefPath absoluteLeafRefTargetPath = null;
32     private String leafRefTargetPathString = "";
33
34     private boolean isReferencedBy = false;
35     private boolean isReferencing = false;
36
37     LeafRefContextBuilder(final QName currentNodeQName, final SchemaPath currentNodePath,
38         final SchemaContext schemaContext) {
39         this.currentNodeQName = currentNodeQName;
40         this.currentNodePath = currentNodePath;
41         this.schemaContext = schemaContext;
42     }
43
44     @Override
45     public 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     public boolean hasLeafRefContextChild() {
57         return hasReferencedByChild() || hasReferencingChild();
58     }
59
60     public boolean hasReferencedByChild() {
61         return !referencedByChildren.isEmpty();
62     }
63
64     public boolean hasReferencingChild() {
65         return !referencingChildren.isEmpty();
66     }
67
68     public boolean isReferencedBy() {
69         return isReferencedBy;
70     }
71
72     public void setReferencedBy(final boolean isReferencedBy) {
73         this.isReferencedBy = isReferencedBy;
74     }
75
76     public boolean isReferencing() {
77         return isReferencing;
78     }
79
80     public void setReferencing(final boolean isReferencing) {
81         this.isReferencing = isReferencing;
82     }
83
84     public void addReferencingChild(final LeafRefContext child, final QName childQName) {
85         referencingChildren.put(childQName, child);
86     }
87
88     public LeafRefContext getReferencingChildByName(final QName name) {
89         return referencingChildren.get(name);
90     }
91
92     public Map<QName, LeafRefContext> getReferencingChilds() {
93         return referencingChildren;
94     }
95
96     public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
97         referencedByChildren.put(childQName, child);
98     }
99
100     public LeafRefContext getReferencedByChildByName(final QName name) {
101         return referencedByChildren.get(name);
102     }
103
104     public Map<QName, LeafRefContext> getReferencedByChilds() {
105         return referencedByChildren;
106     }
107
108     public SchemaPath getCurrentNodePath() {
109         return currentNodePath;
110     }
111
112     public void setCurrentNodePath(final SchemaPath currentNodePath) {
113         this.currentNodePath = currentNodePath;
114     }
115
116     public LeafRefPath getLeafRefTargetPath() {
117         return leafRefTargetPath;
118     }
119
120     public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
121         this.leafRefTargetPath = leafRefPath;
122     }
123
124     public String getLeafRefTargetPathString() {
125         return leafRefTargetPathString;
126     }
127
128     public void setLeafRefTargetPathString(final String leafRefPathString) {
129         this.leafRefTargetPathString = leafRefPathString;
130     }
131
132     public QName getCurrentNodeQName() {
133         return currentNodeQName;
134     }
135
136     public void setCurrentNodeQName(final QName currentNodeQName) {
137         this.currentNodeQName = currentNodeQName;
138     }
139
140     public SchemaContext getSchemaContext() {
141         return schemaContext;
142     }
143
144     public void setSchemaContext(final SchemaContext schemaContext) {
145         this.schemaContext = schemaContext;
146     }
147
148     public LeafRefPath getAbsoluteLeafRefTargetPath() {
149         if (isReferencing && absoluteLeafRefTargetPath == null) {
150             if (leafRefTargetPath.isAbsolute()) {
151                 absoluteLeafRefTargetPath = leafRefTargetPath;
152             } else {
153                 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
154                     currentNodePath, getLeafRefContextModule());
155             }
156         }
157
158         return absoluteLeafRefTargetPath;
159     }
160
161     public Module getLeafRefContextModule() {
162         final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
163         final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
164         return schemaContext.findModule(qnameModule).orElse(null);
165     }
166
167     public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
168         referencedByLeafRefCtx.put(qname, leafRef);
169     }
170
171     public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
172         return referencedByLeafRefCtx.get(qname);
173     }
174
175     public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
176         return referencedByLeafRefCtx;
177     }
178 }