b9899f17366b92a906f8f8e38112de0b1f9290b6
[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.Map;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.model.api.Module;
15 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17
18 class LeafRefContextBuilder {
19
20     private QName currentNodeQName;
21     private SchemaPath currentNodePath;
22     private SchemaContext schemaContext;
23
24     private LeafRefPath leafRefTargetPath = null;
25     private LeafRefPath absoluteLeafRefTargetPath = null;
26     private String leafRefTargetPathString = "";
27
28     private boolean isReferencedBy = false;
29     private boolean isReferencing = false;
30
31     private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
32     private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
33     private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
34
35     public LeafRefContextBuilder(final QName currentNodeQName,
36             final SchemaPath currentNodePath, final SchemaContext schemaContext) {
37         this.currentNodeQName = currentNodeQName;
38         this.currentNodePath = currentNodePath;
39         this.schemaContext = schemaContext;
40     }
41
42     public LeafRefContext build() {
43         final LeafRefContext leafRefContext = new LeafRefContext(this);
44
45         // LeafRefContext has made a copy of these
46         referencingChildren.clear();
47         referencedByChildren.clear();
48         referencedByLeafRefCtx.clear();
49
50         return leafRefContext;
51     }
52
53     public boolean hasLeafRefContextChild() {
54         return hasReferencedByChild() || hasReferencingChild();
55     }
56
57     public boolean hasReferencedByChild() {
58         return !referencedByChildren.isEmpty();
59     }
60
61     public boolean hasReferencingChild() {
62         return !referencingChildren.isEmpty();
63     }
64
65     public boolean isReferencedBy() {
66         return isReferencedBy;
67     }
68
69     public void setReferencedBy(final boolean isReferencedBy) {
70         this.isReferencedBy = isReferencedBy;
71     }
72
73     public boolean isReferencing() {
74         return isReferencing;
75     }
76
77     public void setReferencing(final boolean isReferencing) {
78         this.isReferencing = isReferencing;
79     }
80
81     public void addReferencingChild(final LeafRefContext child, final QName childQName) {
82         referencingChildren.put(childQName, child);
83     }
84
85     public LeafRefContext getReferencingChildByName(final QName name) {
86         return referencingChildren.get(name);
87     }
88
89     public Map<QName, LeafRefContext> getReferencingChilds() {
90         return referencingChildren;
91     }
92
93     public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
94         referencedByChildren.put(childQName, child);
95     }
96
97     public LeafRefContext getReferencedByChildByName(final QName name) {
98         return referencedByChildren.get(name);
99     }
100
101     public Map<QName, LeafRefContext> getReferencedByChilds() {
102         return referencedByChildren;
103     }
104
105     public SchemaPath getCurrentNodePath() {
106         return currentNodePath;
107     }
108
109     public void setCurrentNodePath(final SchemaPath currentNodePath) {
110         this.currentNodePath = currentNodePath;
111     }
112
113     public LeafRefPath getLeafRefTargetPath() {
114         return leafRefTargetPath;
115     }
116
117     public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
118         this.leafRefTargetPath = leafRefPath;
119     }
120
121     public String getLeafRefTargetPathString() {
122         return leafRefTargetPathString;
123     }
124
125     public void setLeafRefTargetPathString(final String leafRefPathString) {
126         this.leafRefTargetPathString = leafRefPathString;
127     }
128
129     public QName getCurrentNodeQName() {
130         return currentNodeQName;
131     }
132
133     public void setCurrentNodeQName(final QName currentNodeQName) {
134         this.currentNodeQName = currentNodeQName;
135     }
136
137     public SchemaContext getSchemaContext() {
138         return schemaContext;
139     }
140
141     public void setSchemaContext(final SchemaContext schemaContext) {
142         this.schemaContext = schemaContext;
143     }
144
145     public LeafRefPath getAbsoluteLeafRefTargetPath() {
146
147         if (isReferencing && absoluteLeafRefTargetPath == null) {
148             if (leafRefTargetPath.isAbsolute()) {
149                 absoluteLeafRefTargetPath = leafRefTargetPath;
150             } else {
151                 absoluteLeafRefTargetPath = LeafRefUtils
152                         .createAbsoluteLeafRefPath(leafRefTargetPath,
153                                 currentNodePath, getLeafRefContextModule());
154             }
155         }
156
157         return absoluteLeafRefTargetPath;
158     }
159
160     public Module getLeafRefContextModule() {
161         final QNameModule qnameModule = currentNodeQName.getModule();
162
163         return schemaContext.findModuleByNamespaceAndRevision(
164                 qnameModule.getNamespace(), qnameModule.getRevision());
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
179 }