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