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;
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;
18 class LeafRefContextBuilder {
20 private QName currentNodeQName;
21 private SchemaPath currentNodePath;
22 private SchemaContext schemaContext;
24 private LeafRefPath leafRefTargetPath = null;
25 private LeafRefPath absoluteLeafRefTargetPath = null;
26 private String leafRefTargetPathString = "";
28 private boolean isReferencedBy = false;
29 private boolean isReferencing = false;
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>();
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;
42 public LeafRefContext build() {
43 final LeafRefContext leafRefContext = new LeafRefContext(this);
45 referencingChilds = new HashMap<QName, LeafRefContext>();
46 referencedByChilds = new HashMap<QName, LeafRefContext>();
47 referencedByLeafRefCtx = new HashMap<QName, LeafRefContext>();
49 return leafRefContext;
52 public boolean hasLeafRefContextChild() {
53 return hasReferencedByChild() || hasReferencingChild();
56 public boolean hasReferencedByChild() {
57 return !referencedByChilds.isEmpty();
60 public boolean hasReferencingChild() {
61 return !referencingChilds.isEmpty();
64 public boolean isReferencedBy() {
65 return isReferencedBy;
68 public void setReferencedBy(final boolean isReferencedBy) {
69 this.isReferencedBy = isReferencedBy;
72 public boolean isReferencing() {
76 public void setReferencing(final boolean isReferencing) {
77 this.isReferencing = isReferencing;
80 public void addReferencingChild(final LeafRefContext child, final QName childQName) {
81 referencingChilds.put(childQName, child);
84 public LeafRefContext getReferencingChildByName(final QName name) {
85 return referencingChilds.get(name);
88 public Map<QName, LeafRefContext> getReferencingChilds() {
89 return referencingChilds;
92 public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
93 referencedByChilds.put(childQName, child);
96 public LeafRefContext getReferencedByChildByName(final QName name) {
97 return referencedByChilds.get(name);
100 public Map<QName, LeafRefContext> getReferencedByChilds() {
101 return referencedByChilds;
104 public SchemaPath getCurrentNodePath() {
105 return currentNodePath;
108 public void setCurrentNodePath(final SchemaPath currentNodePath) {
109 this.currentNodePath = currentNodePath;
112 public LeafRefPath getLeafRefTargetPath() {
113 return leafRefTargetPath;
116 public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
117 this.leafRefTargetPath = leafRefPath;
120 public String getLeafRefTargetPathString() {
121 return leafRefTargetPathString;
124 public void setLeafRefTargetPathString(final String leafRefPathString) {
125 this.leafRefTargetPathString = leafRefPathString;
128 public QName getCurrentNodeQName() {
129 return currentNodeQName;
132 public void setCurrentNodeQName(final QName currentNodeQName) {
133 this.currentNodeQName = currentNodeQName;
136 public SchemaContext getSchemaContext() {
137 return schemaContext;
140 public void setSchemaContext(final SchemaContext schemaContext) {
141 this.schemaContext = schemaContext;
144 public LeafRefPath getAbsoluteLeafRefTargetPath() {
146 if (isReferencing && absoluteLeafRefTargetPath == null) {
147 if (leafRefTargetPath.isAbsolute()) {
148 absoluteLeafRefTargetPath = leafRefTargetPath;
150 absoluteLeafRefTargetPath = LeafRefUtils
151 .createAbsoluteLeafRefPath(leafRefTargetPath,
152 currentNodePath, getLeafRefContextModule());
156 return absoluteLeafRefTargetPath;
159 public Module getLeafRefContextModule() {
160 final QNameModule qnameModule = currentNodeQName.getModule();
162 return schemaContext.findModuleByNamespaceAndRevision(
163 qnameModule.getNamespace(), qnameModule.getRevision());
166 public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
167 referencedByLeafRefCtx.put(qname, leafRef);
170 public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
171 return referencedByLeafRefCtx.get(qname);
174 public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
175 return referencedByLeafRefCtx;