420e213bf5c981fd2786a8fb5459c6857c659b35
[yangtools.git] / data / 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 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         // FIXME: requireNonNull
44         this.schemaContext = schemaContext;
45     }
46
47     @Override
48     public LeafRefContext build() {
49         final LeafRefContext leafRefContext = new LeafRefContext(this);
50
51         // LeafRefContext has made a copy of these
52         referencingChildren.clear();
53         referencedByChildren.clear();
54         referencedByLeafRefCtx.clear();
55
56         return leafRefContext;
57     }
58
59     boolean isReferencedBy() {
60         return isReferencedBy;
61     }
62
63     void setReferencedBy(final boolean referencedBy) {
64         this.isReferencedBy = referencedBy;
65     }
66
67     boolean isReferencing() {
68         return isReferencing;
69     }
70
71     void setReferencing(final boolean referencing) {
72         this.isReferencing = referencing;
73     }
74
75     void addReferencingChild(final LeafRefContext child, final QName childQName) {
76         referencingChildren.put(childQName, child);
77     }
78
79     Map<QName, LeafRefContext> getReferencingChilds() {
80         return referencingChildren;
81     }
82
83     void addReferencedByChild(final LeafRefContext child, final QName childQName) {
84         referencedByChildren.put(childQName, child);
85     }
86
87     Map<QName, LeafRefContext> getReferencedByChilds() {
88         return referencedByChildren;
89     }
90
91     SchemaPath getCurrentNodePath() {
92         return currentNodePath;
93     }
94
95     LeafRefPath getLeafRefTargetPath() {
96         return leafRefTargetPath;
97     }
98
99     void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
100         this.leafRefTargetPath = requireNonNull(leafRefPath);
101     }
102
103     String getLeafRefTargetPathString() {
104         return leafRefTargetPathString;
105     }
106
107     void setLeafRefTargetPathString(final String leafRefPathString) {
108         this.leafRefTargetPathString = requireNonNull(leafRefPathString);
109     }
110
111     QName getCurrentNodeQName() {
112         return currentNodeQName;
113     }
114
115     EffectiveModelContext getSchemaContext() {
116         return schemaContext;
117     }
118
119     LeafRefPath getAbsoluteLeafRefTargetPath() {
120         if (isReferencing && absoluteLeafRefTargetPath == null) {
121             if (leafRefTargetPath.isAbsolute()) {
122                 absoluteLeafRefTargetPath = leafRefTargetPath;
123             } else {
124                 absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
125                     currentNodePath, getLeafRefContextModule());
126             }
127         }
128
129         return absoluteLeafRefTargetPath;
130     }
131
132     Module getLeafRefContextModule() {
133         final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
134         final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
135         return schemaContext.findModule(qnameModule).orElse(null);
136     }
137
138     void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
139         referencedByLeafRefCtx.put(qname, leafRef);
140     }
141
142     LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
143         return referencedByLeafRefCtx.get(qname);
144     }
145
146     Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
147         return referencedByLeafRefCtx;
148     }
149 }