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