Bug 2366 - Effective statements impl for new yang parser.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ContainerEffectiveStatementImpl.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.parser.stmt.rfc6020.effective;
9
10 import com.google.common.collect.ImmutableSet;
11
12 import com.google.common.collect.ImmutableList;
13 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
14 import java.util.HashSet;
15 import java.util.LinkedList;
16 import java.util.Collection;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import com.google.common.base.Optional;
21 import java.util.List;
22 import java.util.Set;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30
31 public class ContainerEffectiveStatementImpl extends
32         AbstractEffectiveDocumentedDataNodeContainer<QName, ContainerStatement>
33         implements ContainerSchemaNode, DerivableSchemaNode {
34     private final QName qname;
35     private final SchemaPath path;
36     private final boolean presence;
37
38     boolean augmenting;
39     boolean addedByUses;
40     boolean configuration;
41     ContainerSchemaNode original;
42     ConstraintDefinition constraints;
43
44     private ImmutableSet<AugmentationSchema> augmentations;
45     private ImmutableList<UnknownSchemaNode> unknownNodes;
46
47     public ContainerEffectiveStatementImpl(
48             StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
49         super(ctx);
50
51         qname = ctx.getStatementArgument();
52         path = Utils.getSchemaPath(ctx);
53         presence = (firstEffective(PresenceEffectiveStatementImpl.class) == null) ? false
54                 : true;
55         // :TODO init other fields
56
57         initSubstatementCollections();
58     }
59
60     private void initSubstatementCollections() {
61         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
62
63         LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
64         HashSet<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
65
66         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
67             if (effectiveStatement instanceof UnknownSchemaNode) {
68                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
69                 unknownNodes.add(unknownNode);
70             }
71             if (effectiveStatement instanceof AugmentationSchema) {
72                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
73                 augmentations.add(augmentationSchema);
74             }
75         }
76
77         this.unknownNodes = ImmutableList.copyOf(unknownNodes);
78         this.augmentations = ImmutableSet.copyOf(augmentations);
79     }
80
81     @Override
82     public QName getQName() {
83         return qname;
84     }
85
86     @Override
87     public SchemaPath getPath() {
88         return path;
89     }
90
91     @Override
92     public boolean isAugmenting() {
93         return augmenting;
94     }
95
96     @Override
97     public boolean isAddedByUses() {
98         return addedByUses;
99     }
100
101     @Override
102     public Optional<ContainerSchemaNode> getOriginal() {
103         return Optional.fromNullable(original);
104     }
105
106     @Override
107     public boolean isConfiguration() {
108         return configuration;
109     }
110
111     @Override
112     public ConstraintDefinition getConstraints() {
113         return constraints;
114     }
115
116     @Override
117     public Set<AugmentationSchema> getAvailableAugmentations() {
118         return augmentations;
119     }
120
121     @Override
122     public boolean isPresenceContainer() {
123         return presence;
124     }
125
126     @Override
127     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
128         return unknownNodes;
129     }
130
131     @Override
132     public int hashCode() {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
136         result = prime * result + ((path == null) ? 0 : path.hashCode());
137         return result;
138     }
139
140     @Override
141     public boolean equals(final Object obj) {
142         if (this == obj) {
143             return true;
144         }
145         if (obj == null) {
146             return false;
147         }
148         if (getClass() != obj.getClass()) {
149             return false;
150         }
151         ContainerEffectiveStatementImpl other = (ContainerEffectiveStatementImpl) obj;
152         if (qname == null) {
153             if (other.qname != null) {
154                 return false;
155             }
156         } else if (!qname.equals(other.qname)) {
157             return false;
158         }
159         if (path == null) {
160             if (other.path != null) {
161                 return false;
162             }
163         } else if (!path.equals(other.path)) {
164             return false;
165         }
166         return true;
167     }
168
169     @Override
170     public String toString() {
171         return "container " + qname.getLocalName();
172     }
173 }