Cleanup leafref path parsing
[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
17 public final class LeafRefContext {
18
19     private final QName currentNodeQName;
20     private final SchemaPath currentNodePath;
21     private final SchemaContext schemaContext;
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 Map<QName, LeafRefContext> referencingChilds;
32     private final Map<QName, LeafRefContext> referencedByChilds;
33     private final Map<QName, LeafRefContext> referencedByLeafRefCtx;
34
35     LeafRefContext(final LeafRefContextBuilder leafRefContextBuilder) {
36         this.currentNodeQName = leafRefContextBuilder.getCurrentNodeQName();
37         this.currentNodePath = leafRefContextBuilder.getCurrentNodePath();
38         this.schemaContext = leafRefContextBuilder.getSchemaContext();
39         this.leafRefTargetPath = leafRefContextBuilder.getLeafRefTargetPath();
40         this.absoluteLeafRefTargetPath = leafRefContextBuilder.getAbsoluteLeafRefTargetPath();
41         this.leafRefTargetPathString = leafRefContextBuilder.getLeafRefTargetPathString();
42         this.isReferencedBy = leafRefContextBuilder.isReferencedBy();
43         this.isReferencing = leafRefContextBuilder.isReferencing();
44         this.referencingChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencingChilds());
45         this.referencedByChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencedByChilds());
46         this.referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder.getAllReferencedByLeafRefCtxs());
47         this.module = leafRefContextBuilder.getLeafRefContextModule();
48     }
49
50     public static LeafRefContext create(final SchemaContext ctx) {
51         try {
52             return new LeafRefContextTreeBuilder(ctx).buildLeafRefContextTree();
53         } catch (LeafRefYangSyntaxErrorException e) {
54             throw new IllegalArgumentException(e);
55         }
56     }
57
58     public boolean hasLeafRefContextChild() {
59         return hasReferencedChild() || hasReferencingChild();
60     }
61
62     public boolean hasReferencedChild() {
63         return !referencedByChilds.isEmpty();
64     }
65
66     public boolean hasReferencingChild() {
67         return !referencingChilds.isEmpty();
68     }
69
70     public boolean isReferenced() {
71         return isReferencedBy;
72     }
73
74     public boolean isReferencing() {
75         return isReferencing;
76     }
77
78     public LeafRefContext getReferencingChildByName(final QName name) {
79         return referencingChilds.get(name);
80     }
81
82     public Map<QName, LeafRefContext> getReferencingChilds() {
83         return referencingChilds;
84     }
85
86     public LeafRefContext getReferencedChildByName(final QName name) {
87         return referencedByChilds.get(name);
88     }
89
90     public Map<QName, LeafRefContext> getReferencedByChilds() {
91         return referencedByChilds;
92     }
93
94     public SchemaPath getCurrentNodePath() {
95         return currentNodePath;
96     }
97
98     public LeafRefPath getLeafRefTargetPath() {
99         return leafRefTargetPath;
100     }
101
102     public String getLeafRefTargetPathString() {
103         return leafRefTargetPathString;
104     }
105
106     public QName getNodeName() {
107         return currentNodeQName;
108     }
109
110     SchemaContext getSchemaContext() {
111         return schemaContext;
112     }
113
114     public LeafRefPath getAbsoluteLeafRefTargetPath() {
115         return absoluteLeafRefTargetPath;
116     }
117
118     public Module getLeafRefContextModule() {
119         return module;
120     }
121
122     public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
123         return referencedByLeafRefCtx.get(qname);
124     }
125
126     public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
127         return referencedByLeafRefCtx;
128     }
129
130 }