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