2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
10 import java.util.HashMap;
11 import java.util.Iterator;
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;
19 class LeafRefContextBuilder {
21 private QName currentNodeQName;
22 private SchemaPath currentNodePath;
23 private SchemaContext schemaContext;
25 private LeafRefPath leafRefTargetPath = null;
26 private LeafRefPath absoluteLeafRefTargetPath = null;
27 private String leafRefTargetPathString = "";
29 private boolean isReferencedBy = false;
30 private boolean isReferencing = false;
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<>();
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;
43 public LeafRefContext build() {
44 final LeafRefContext leafRefContext = new LeafRefContext(this);
46 // LeafRefContext has made a copy of these
47 referencingChildren.clear();
48 referencedByChildren.clear();
49 referencedByLeafRefCtx.clear();
51 return leafRefContext;
54 public boolean hasLeafRefContextChild() {
55 return hasReferencedByChild() || hasReferencingChild();
58 public boolean hasReferencedByChild() {
59 return !referencedByChildren.isEmpty();
62 public boolean hasReferencingChild() {
63 return !referencingChildren.isEmpty();
66 public boolean isReferencedBy() {
67 return isReferencedBy;
70 public void setReferencedBy(final boolean isReferencedBy) {
71 this.isReferencedBy = isReferencedBy;
74 public boolean isReferencing() {
78 public void setReferencing(final boolean isReferencing) {
79 this.isReferencing = isReferencing;
82 public void addReferencingChild(final LeafRefContext child, final QName childQName) {
83 referencingChildren.put(childQName, child);
86 public LeafRefContext getReferencingChildByName(final QName name) {
87 return referencingChildren.get(name);
90 public Map<QName, LeafRefContext> getReferencingChilds() {
91 return referencingChildren;
94 public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
95 referencedByChildren.put(childQName, child);
98 public LeafRefContext getReferencedByChildByName(final QName name) {
99 return referencedByChildren.get(name);
102 public Map<QName, LeafRefContext> getReferencedByChilds() {
103 return referencedByChildren;
106 public SchemaPath getCurrentNodePath() {
107 return currentNodePath;
110 public void setCurrentNodePath(final SchemaPath currentNodePath) {
111 this.currentNodePath = currentNodePath;
114 public LeafRefPath getLeafRefTargetPath() {
115 return leafRefTargetPath;
118 public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
119 this.leafRefTargetPath = leafRefPath;
122 public String getLeafRefTargetPathString() {
123 return leafRefTargetPathString;
126 public void setLeafRefTargetPathString(final String leafRefPathString) {
127 this.leafRefTargetPathString = leafRefPathString;
130 public QName getCurrentNodeQName() {
131 return currentNodeQName;
134 public void setCurrentNodeQName(final QName currentNodeQName) {
135 this.currentNodeQName = currentNodeQName;
138 public SchemaContext getSchemaContext() {
139 return schemaContext;
142 public void setSchemaContext(final SchemaContext schemaContext) {
143 this.schemaContext = schemaContext;
146 public LeafRefPath getAbsoluteLeafRefTargetPath() {
148 if (isReferencing && absoluteLeafRefTargetPath == null) {
149 if (leafRefTargetPath.isAbsolute()) {
150 absoluteLeafRefTargetPath = leafRefTargetPath;
152 absoluteLeafRefTargetPath = LeafRefUtils
153 .createAbsoluteLeafRefPath(leafRefTargetPath,
154 currentNodePath, getLeafRefContextModule());
158 return absoluteLeafRefTargetPath;
161 public Module getLeafRefContextModule() {
162 final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
163 final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
165 return schemaContext.findModuleByNamespaceAndRevision(qnameModule.getNamespace(), qnameModule.getRevision());
168 public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
169 referencedByLeafRefCtx.put(qname, leafRef);
172 public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
173 return referencedByLeafRefCtx.get(qname);
176 public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
177 return referencedByLeafRefCtx;