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