fcf06813cabb490b5c35635518afac42c6a9b110
[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 public final 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.getAbsoluteLeafRefTargetPath();
42         this.leafRefTargetPathString = leafRefContextBuilder.getLeafRefTargetPathString();
43         this.isReferencedBy = leafRefContextBuilder.isReferencedBy();
44         this.isReferencing = leafRefContextBuilder.isReferencing();
45         this.referencingChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencingChilds());
46         this.referencedByChilds = ImmutableMap.copyOf(leafRefContextBuilder.getReferencedByChilds());
47         this.referencedByLeafRefCtx = ImmutableMap.copyOf(leafRefContextBuilder.getAllReferencedByLeafRefCtxs());
48         this.module = leafRefContextBuilder.getLeafRefContextModule();
49     }
50
51     public static LeafRefContext create(final SchemaContext ctx) {
52         try {
53             return new LeafRefContextTreeBuilder(ctx).buildLeafRefContextTree();
54         } catch (IOException | LeafRefYangSyntaxErrorException e) {
55             throw new RuntimeException(e);
56         }
57     }
58
59     public boolean hasLeafRefContextChild() {
60         return hasReferencedChild() || hasReferencingChild();
61     }
62
63     public boolean hasReferencedChild() {
64         return !referencedByChilds.isEmpty();
65     }
66
67     public boolean hasReferencingChild() {
68         return !referencingChilds.isEmpty();
69     }
70
71     public boolean isReferenced() {
72         return isReferencedBy;
73     }
74
75     public boolean isReferencing() {
76         return isReferencing;
77     }
78
79     public LeafRefContext getReferencingChildByName(final QName name) {
80         return referencingChilds.get(name);
81     }
82
83     public Map<QName, LeafRefContext> getReferencingChilds() {
84         return referencingChilds;
85     }
86
87     public LeafRefContext getReferencedChildByName(final QName name) {
88         return referencedByChilds.get(name);
89     }
90
91     public Map<QName, LeafRefContext> getReferencedByChilds() {
92         return referencedByChilds;
93     }
94
95     public SchemaPath getCurrentNodePath() {
96         return currentNodePath;
97     }
98
99     public LeafRefPath getLeafRefTargetPath() {
100         return leafRefTargetPath;
101     }
102
103     public String getLeafRefTargetPathString() {
104         return leafRefTargetPathString;
105     }
106
107     public QName getNodeName() {
108         return currentNodeQName;
109     }
110
111     SchemaContext getSchemaContext() {
112         return schemaContext;
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 }