Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[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 org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
11 import com.google.common.collect.ImmutableSet;
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
35     private final QName qname;
36     private final SchemaPath path;
37
38     private boolean presence;
39     boolean augmenting;
40     private boolean addedByUses;
41     private boolean configuration = true;
42     private ContainerSchemaNode original;
43     private ConstraintDefinition constraints;
44
45     private ImmutableSet<AugmentationSchema> augmentations;
46     private ImmutableList<UnknownSchemaNode> unknownNodes;
47
48     public ContainerEffectiveStatementImpl(
49             StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
50         super(ctx);
51
52         qname = ctx.getStatementArgument();
53         path = Utils.getSchemaPath(ctx);
54         this.constraints = new EffectiveConstraintDefinitionImpl(this);
55
56         initCopyType(ctx);
57         initSubstatementCollectionsAndFields();
58     }
59
60     private void initCopyType(
61             StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
62
63         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
64
65         if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
66             augmenting = true;
67         }
68         if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
69             addedByUses = true;
70         }
71         if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
72             addedByUses = augmenting = true;
73         }
74
75         if (ctx.getOriginalCtx() != null) {
76             original = (ContainerSchemaNode) ctx.getOriginalCtx().buildEffective();
77         }
78     }
79
80     private void initSubstatementCollectionsAndFields() {
81         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
82
83         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
84         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
85
86         boolean configurationInit = false;
87         for (EffectiveStatement<?, ?> effectiveSubstatement : effectiveSubstatements) {
88             if (effectiveSubstatement instanceof UnknownSchemaNode) {
89                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveSubstatement;
90                 unknownNodesInit.add(unknownNode);
91             }
92             if (effectiveSubstatement instanceof AugmentationSchema) {
93                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveSubstatement;
94                 augmentationsInit.add(augmentationSchema);
95             }
96             if (effectiveSubstatement instanceof PresenceEffectiveStatementImpl) {
97                 presence = true;
98             }
99             if (!configurationInit
100                     && effectiveSubstatement instanceof ConfigEffectiveStatementImpl) {
101                 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveSubstatement;
102                 this.configuration = configStmt.argument();
103                 configurationInit = true;
104             }
105         }
106
107         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
108         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
109     }
110
111     @Override
112     public QName getQName() {
113         return qname;
114     }
115
116     @Override
117     public SchemaPath getPath() {
118         return path;
119     }
120
121     @Override
122     public boolean isAugmenting() {
123         return augmenting;
124     }
125
126     @Override
127     public boolean isAddedByUses() {
128         return addedByUses;
129     }
130
131     @Override
132     public Optional<ContainerSchemaNode> getOriginal() {
133         return Optional.fromNullable(original);
134     }
135
136     @Override
137     public boolean isConfiguration() {
138         return configuration;
139     }
140
141     @Override
142     public ConstraintDefinition getConstraints() {
143         return constraints;
144     }
145
146     @Override
147     public Set<AugmentationSchema> getAvailableAugmentations() {
148         return augmentations;
149     }
150
151     @Override
152     public boolean isPresenceContainer() {
153         return presence;
154     }
155
156     @Override
157     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
158         return unknownNodes;
159     }
160
161     @Override
162     public int hashCode() {
163         final int prime = 31;
164         int result = 1;
165         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
166         result = prime * result + ((path == null) ? 0 : path.hashCode());
167         return result;
168     }
169
170     @Override
171     public boolean equals(final Object obj) {
172         if (this == obj) {
173             return true;
174         }
175         if (obj == null) {
176             return false;
177         }
178         if (getClass() != obj.getClass()) {
179             return false;
180         }
181         ContainerEffectiveStatementImpl other = (ContainerEffectiveStatementImpl) obj;
182         if (qname == null) {
183             if (other.qname != null) {
184                 return false;
185             }
186         } else if (!qname.equals(other.qname)) {
187             return false;
188         }
189         if (path == null) {
190             if (other.path != null) {
191                 return false;
192             }
193         } else if (!path.equals(other.path)) {
194             return false;
195         }
196         return true;
197     }
198
199     @Override
200     public String toString() {
201         return "container " + qname.getLocalName();
202     }
203 }