002a6af026569ff7dc56df4c715b06310586b2a1
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContext.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 com.google.common.collect.ImmutableMap;
11 import java.util.Map;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.Module;
14 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContextProvider;
17
18 public final class LeafRefContext extends AbstractSchemaContextProvider {
19
20     private final QName currentNodeQName;
21     private final SchemaPath currentNodePath;
22     private final Module module;
23
24     private final LeafRefPath leafRefTargetPath;
25     private final LeafRefPath absoluteLeafRefTargetPath;
26     private final String leafRefTargetPathString;
27
28     private final boolean isReferencedBy;
29     private final boolean isReferencing;
30
31     private final ImmutableMap<QName, LeafRefContext> referencingChilds;
32     private final ImmutableMap<QName, LeafRefContext> referencedByChilds;
33     private final ImmutableMap<QName, LeafRefContext> referencedByLeafRefCtx;
34
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;
39
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();
53     }
54
55     public static LeafRefContext create(final SchemaContext ctx) {
56         try {
57             return new LeafRefContextTreeBuilder(ctx).buildLeafRefContextTree();
58         } catch (LeafRefYangSyntaxErrorException e) {
59             throw new IllegalArgumentException(e);
60         }
61     }
62
63     public boolean hasLeafRefContextChild() {
64         return hasReferencedChild() || hasReferencingChild();
65     }
66
67     public boolean hasReferencedChild() {
68         return !referencedByChilds.isEmpty();
69     }
70
71     public boolean hasReferencingChild() {
72         return !referencingChilds.isEmpty();
73     }
74
75     public boolean isReferenced() {
76         return isReferencedBy;
77     }
78
79     public boolean isReferencing() {
80         return isReferencing;
81     }
82
83     public LeafRefContext getReferencingChildByName(final QName name) {
84         return referencingChilds.get(name);
85     }
86
87     public Map<QName, LeafRefContext> getReferencingChilds() {
88         return referencingChilds;
89     }
90
91     public LeafRefContext getReferencedChildByName(final QName name) {
92         return referencedByChilds.get(name);
93     }
94
95     public Map<QName, LeafRefContext> getReferencedByChilds() {
96         return referencedByChilds;
97     }
98
99     public SchemaPath getCurrentNodePath() {
100         return currentNodePath;
101     }
102
103     public LeafRefPath getLeafRefTargetPath() {
104         return leafRefTargetPath;
105     }
106
107     public String getLeafRefTargetPathString() {
108         return leafRefTargetPathString;
109     }
110
111     public QName getNodeName() {
112         return currentNodeQName;
113     }
114
115     public LeafRefPath getAbsoluteLeafRefTargetPath() {
116         return absoluteLeafRefTargetPath;
117     }
118
119     public Module getLeafRefContextModule() {
120         return module;
121     }
122
123     public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
124         return referencedByLeafRefCtx.get(qname);
125     }
126
127     public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
128         return referencedByLeafRefCtx;
129     }
130
131     LeafRefPath getLeafRefNodePath() {
132         LeafRefPath ret = leafRefNodePath;
133         if (ret == null) {
134             synchronized (this) {
135                 ret = leafRefNodePath;
136                 if (ret == null) {
137                     ret = leafRefNodePath = LeafRefUtils.schemaPathToLeafRefPath(currentNodePath, module);
138                 }
139             }
140         }
141         return ret;
142     }
143 }