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