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 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;
29 public class LeafListEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, LeafListStatement> implements
30 LeafListSchemaNode, DerivableSchemaNode {
31 private final QName qname;
32 private final SchemaPath path;
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;
42 private ImmutableList<UnknownSchemaNode> unknownNodes;
44 public LeafListEffectiveStatementImpl(
45 StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
47 this.qname = ctx.getStatementArgument();
48 this.path = Utils.getSchemaPath(ctx);
49 this.constraintsDef = new EffectiveConstraintDefinitionImpl(this);
51 // :TODO init TypeDefinition
53 initSubstatementCollections();
57 private void initCopyType(
58 StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
60 List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
62 if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
65 if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
68 if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
69 addedByUses = augmenting = true;
72 if (ctx.getOriginalCtx() != null) {
73 original = (LeafListSchemaNode) ctx.getOriginalCtx().buildEffective();
76 private void initSubstatementCollections() {
77 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
79 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
81 boolean configurationInit = false;
82 boolean userOrderedInit = false;
83 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
84 if (effectiveStatement instanceof UnknownSchemaNode) {
85 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
87 if (effectiveStatement instanceof TypeDefinition) {
88 type = TypeUtils.getTypeFromEffectiveStatement(effectiveStatement);
90 if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
91 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
92 this.configuration = configStmt.argument();
93 configurationInit = true;
95 if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
96 OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
97 this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
98 userOrderedInit = true;
102 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
106 public QName getQName() {
111 public SchemaPath getPath() {
116 public boolean isAugmenting() {
121 public boolean isAddedByUses() {
126 public Optional<LeafListSchemaNode> getOriginal() {
127 return Optional.fromNullable(original);
131 public boolean isConfiguration() {
132 return configuration;
136 public ConstraintDefinition getConstraints() {
137 return constraintsDef;
141 public TypeDefinition<?> getType() {
146 public boolean isUserOrdered() {
151 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
156 public int hashCode() {
157 final int prime = 31;
159 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
160 result = prime * result + ((path == null) ? 0 : path.hashCode());
165 public boolean equals(final Object obj) {
172 if (getClass() != obj.getClass()) {
175 LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
177 if (other.qname != null) {
180 } else if (!qname.equals(other.qname)) {
184 if (other.path != null) {
187 } else if (!path.equals(other.path)) {
194 public String toString() {
195 StringBuilder sb = new StringBuilder(LeafListEffectiveStatementImpl.class.getSimpleName());
199 return sb.toString();