4349bd5396bafd717f09f6467c8d36198158ecf4
[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 Map<QName, LeafRefContext> referencingChilds = new HashMap<QName, LeafRefContext>();
32     private Map<QName, LeafRefContext> referencedByChilds = new HashMap<QName, LeafRefContext>();
33     private Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<QName, LeafRefContext>();
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         referencingChilds = new HashMap<QName, LeafRefContext>();
46         referencedByChilds = new HashMap<QName, LeafRefContext>();
47         referencedByLeafRefCtx = new HashMap<QName, LeafRefContext>();
48
49         return leafRefContext;
50     }
51
52     public boolean hasLeafRefContextChild() {
53         return hasReferencedByChild() || hasReferencingChild();
54     }
55
56     public boolean hasReferencedByChild() {
57         return !referencedByChilds.isEmpty();
58     }
59
60     public boolean hasReferencingChild() {
61         return !referencingChilds.isEmpty();
62     }
63
64     public boolean isReferencedBy() {
65         return isReferencedBy;
66     }
67
68     public void setReferencedBy(final boolean isReferencedBy) {
69         this.isReferencedBy = isReferencedBy;
70     }
71
72     public boolean isReferencing() {
73         return isReferencing;
74     }
75
76     public void setReferencing(final boolean isReferencing) {
77         this.isReferencing = isReferencing;
78     }
79
80     public void addReferencingChild(final LeafRefContext child, final QName childQName) {
81         referencingChilds.put(childQName, child);
82     }
83
84     public LeafRefContext getReferencingChildByName(final QName name) {
85         return referencingChilds.get(name);
86     }
87
88     public Map<QName, LeafRefContext> getReferencingChilds() {
89         return referencingChilds;
90     }
91
92     public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
93         referencedByChilds.put(childQName, child);
94     }
95
96     public LeafRefContext getReferencedByChildByName(final QName name) {
97         return referencedByChilds.get(name);
98     }
99
100     public Map<QName, LeafRefContext> getReferencedByChilds() {
101         return referencedByChilds;
102     }
103
104     public SchemaPath getCurrentNodePath() {
105         return currentNodePath;
106     }
107
108     public void setCurrentNodePath(final SchemaPath currentNodePath) {
109         this.currentNodePath = currentNodePath;
110     }
111
112     public LeafRefPath getLeafRefTargetPath() {
113         return leafRefTargetPath;
114     }
115
116     public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
117         this.leafRefTargetPath = leafRefPath;
118     }
119
120     public String getLeafRefTargetPathString() {
121         return leafRefTargetPathString;
122     }
123
124     public void setLeafRefTargetPathString(final String leafRefPathString) {
125         this.leafRefTargetPathString = leafRefPathString;
126     }
127
128     public QName getCurrentNodeQName() {
129         return currentNodeQName;
130     }
131
132     public void setCurrentNodeQName(final QName currentNodeQName) {
133         this.currentNodeQName = currentNodeQName;
134     }
135
136     public SchemaContext getSchemaContext() {
137         return schemaContext;
138     }
139
140     public void setSchemaContext(final SchemaContext schemaContext) {
141         this.schemaContext = schemaContext;
142     }
143
144     public LeafRefPath getAbsoluteLeafRefTargetPath() {
145
146         if (isReferencing && absoluteLeafRefTargetPath == null) {
147             if (leafRefTargetPath.isAbsolute()) {
148                 absoluteLeafRefTargetPath = leafRefTargetPath;
149             } else {
150                 absoluteLeafRefTargetPath = LeafRefUtils
151                         .createAbsoluteLeafRefPath(leafRefTargetPath,
152                                 currentNodePath, getLeafRefContextModule());
153             }
154         }
155
156         return absoluteLeafRefTargetPath;
157     }
158
159     public Module getLeafRefContextModule() {
160         final QNameModule qnameModule = currentNodeQName.getModule();
161
162         return schemaContext.findModuleByNamespaceAndRevision(
163                 qnameModule.getNamespace(), qnameModule.getRevision());
164     }
165
166     public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
167         referencedByLeafRefCtx.put(qname, leafRef);
168     }
169
170     public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
171         return referencedByLeafRefCtx.get(qname);
172     }
173
174     public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
175         return referencedByLeafRefCtx;
176     }
177
178 }