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 com.google.common.collect.ImmutableMap;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
14 import org.opendaylight.yangtools.yang.model.api.Module;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.util.AbstractEffectiveModelContextProvider;
18 public final class LeafRefContext extends AbstractEffectiveModelContextProvider {
20 private final QName currentNodeQName;
21 private final SchemaPath currentNodePath;
22 private final Module module;
24 private final LeafRefPath leafRefTargetPath;
25 private final LeafRefPath absoluteLeafRefTargetPath;
26 private final String leafRefTargetPathString;
28 private final boolean isReferencedBy;
29 private final boolean isReferencing;
31 private final ImmutableMap<QName, LeafRefContext> referencingChilds;
32 private final ImmutableMap<QName, LeafRefContext> referencedByChilds;
33 private final ImmutableMap<QName, LeafRefContext> referencedByLeafRefCtx;
35 // FIXME: this looks like it's related to absoluteLeafRefTargetPath, but the original use in LeafRefValidation
36 // fast path did not make it clear. Analyze the relationship between this field and
37 // absoluteLeafRefTargetPath.
38 private volatile LeafRefPath leafRefNodePath = null;
40 LeafRefContext(final LeafRefContextBuilder leafRefContextBuilder) {
41 super(leafRefContextBuilder.getSchemaContext());
42 this.currentNodeQName = leafRefContextBuilder.getCurrentNodeQName();
43 this.currentNodePath = leafRefContextBuilder.getCurrentNodePath();
44 this.leafRefTargetPath = leafRefContextBuilder.getLeafRefTargetPath();
45 this.absoluteLeafRefTargetPath = leafRefContextBuilder.getAbsoluteLeafRefTargetPath();
46 this.leafRefTargetPathString = leafRefContextBuilder.getLeafRefTargetPathString();
47 this.isReferencedBy = leafRefContextBuilder.isReferencedBy();
48 this.isReferencing = leafRefContextBuilder.isReferencing();
49 this.referencingChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencingChilds());
50 this.referencedByChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencedByChilds());
51 this.referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder.getAllReferencedByLeafRefCtxs());
52 this.module = leafRefContextBuilder.getLeafRefContextModule();
55 public static LeafRefContext create(final EffectiveModelContext ctx) {
57 return new LeafRefContextTreeBuilder(ctx).buildLeafRefContextTree();
58 } catch (LeafRefYangSyntaxErrorException e) {
59 throw new IllegalArgumentException(e);
63 public boolean hasLeafRefContextChild() {
64 return hasReferencedChild() || hasReferencingChild();
67 public boolean hasReferencedChild() {
68 return !referencedByChilds.isEmpty();
71 public boolean hasReferencingChild() {
72 return !referencingChilds.isEmpty();
75 public boolean isReferenced() {
76 return isReferencedBy;
79 public boolean isReferencing() {
83 public LeafRefContext getReferencingChildByName(final QName name) {
84 return referencingChilds.get(name);
87 public Map<QName, LeafRefContext> getReferencingChilds() {
88 return referencingChilds;
91 public LeafRefContext getReferencedChildByName(final QName name) {
92 return referencedByChilds.get(name);
95 public Map<QName, LeafRefContext> getReferencedByChilds() {
96 return referencedByChilds;
99 public SchemaPath getCurrentNodePath() {
100 return currentNodePath;
103 public LeafRefPath getLeafRefTargetPath() {
104 return leafRefTargetPath;
107 public String getLeafRefTargetPathString() {
108 return leafRefTargetPathString;
111 public QName getNodeName() {
112 return currentNodeQName;
115 public LeafRefPath getAbsoluteLeafRefTargetPath() {
116 return absoluteLeafRefTargetPath;
119 public Module getLeafRefContextModule() {
123 public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
124 return referencedByLeafRefCtx.get(qname);
127 public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
128 return referencedByLeafRefCtx;
131 LeafRefPath getLeafRefNodePath() {
132 LeafRefPath ret = leafRefNodePath;
134 synchronized (this) {
135 ret = leafRefNodePath;
137 ret = leafRefNodePath = LeafRefUtils.schemaPathToLeafRefPath(currentNodePath, module);