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