2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Objects;
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.TypeUtils;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
30 public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements
31 LeafListSchemaNode, DerivableSchemaNode {
32 private final QName qname;
33 private final SchemaPath path;
35 // FIXME: should be private
37 private boolean addedByUses;
38 private LeafListSchemaNode original;
39 private boolean configuration = true;
40 private final ConstraintDefinition constraintsDef;
41 private TypeDefinition<?> type;
42 private boolean userOrdered;
44 private ImmutableList<UnknownSchemaNode> unknownNodes;
46 public LeafListEffectiveStatementImpl(
47 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
49 this.qname = ctx.getStatementArgument();
50 this.path = Utils.getSchemaPath(ctx);
51 this.constraintsDef = new EffectiveConstraintDefinitionImpl(this);
53 // :TODO init TypeDefinition
55 initSubstatementCollections();
59 private void initCopyType(
60 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
62 List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
64 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
67 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
70 if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
71 addedByUses = augmenting = true;
74 if (ctx.getOriginalCtx() != null) {
75 original = (LeafListSchemaNode) ctx.getOriginalCtx().buildEffective();
78 private void initSubstatementCollections() {
79 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
81 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
83 boolean configurationInit = false;
84 boolean userOrderedInit = false;
85 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
86 if (effectiveStatement instanceof UnknownSchemaNode) {
87 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
89 if (effectiveStatement instanceof TypeDefinition) {
90 type = TypeUtils.getTypeFromEffectiveStatement(effectiveStatement);
92 if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
93 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
94 this.configuration = configStmt.argument();
95 configurationInit = true;
97 if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
98 OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
99 this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
100 userOrderedInit = true;
104 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
108 public QName getQName() {
113 public SchemaPath getPath() {
118 public boolean isAugmenting() {
123 public boolean isAddedByUses() {
128 public Optional<LeafListSchemaNode> getOriginal() {
129 return Optional.fromNullable(original);
133 public boolean isConfiguration() {
134 return configuration;
138 public ConstraintDefinition getConstraints() {
139 return constraintsDef;
143 public TypeDefinition<?> getType() {
148 public boolean isUserOrdered() {
153 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
158 public int hashCode() {
159 final int prime = 31;
161 result = prime * result + Objects.hashCode(qname);
162 result = prime * result + Objects.hashCode(path);
167 public boolean equals(final Object obj) {
174 if (getClass() != obj.getClass()) {
177 LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
178 return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
182 public String toString() {
183 StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
187 return sb.toString();