Bug 4656: Yang parser does not determine configuration true or false properly
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / CaseEffectiveStatementImpl.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 java.util.Collection;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
15 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.CaseStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
20
21 public final class CaseEffectiveStatementImpl extends AbstractEffectiveSimpleDataNodeContainer<CaseStatement> implements
22         ChoiceCaseNode, DerivableSchemaNode {
23
24     private final ChoiceCaseNode original;
25     private final boolean configuration;
26
27     public CaseEffectiveStatementImpl(
28             final StmtContext<QName, CaseStatement, EffectiveStatement<QName, CaseStatement>> ctx) {
29         super(ctx);
30         this.original = ctx.getOriginalCtx() == null ? null : (ChoiceCaseNode) ctx.getOriginalCtx().buildEffective();
31
32         if (ctx.isConfiguration()) {
33             configuration = isAtLeastOneChildConfiguration(ctx.declaredSubstatements()) ||
34                     isAtLeastOneChildConfiguration(ctx.effectiveSubstatements());
35         } else {
36             configuration = false;
37         }
38     }
39
40     private boolean isAtLeastOneChildConfiguration(Collection<StatementContextBase<?, ?, ?>> substatements) {
41         for (StatementContextBase<?, ?, ?> substatement : substatements) {
42             if (substatement.isConfiguration()) {
43                 return true;
44             }
45         }
46         return false;
47     }
48
49     @Override
50     public Optional<ChoiceCaseNode> getOriginal() {
51         return Optional.fromNullable(original);
52     }
53
54     @Override
55     public boolean isConfiguration() {
56         return configuration;
57     }
58
59     @Override
60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63         result = prime * result + Objects.hashCode(getQName());
64         result = prime * result + Objects.hashCode(getPath());
65         return result;
66     }
67
68     @Override
69     public boolean equals(final Object obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (obj == null) {
74             return false;
75         }
76         if (getClass() != obj.getClass()) {
77             return false;
78         }
79         CaseEffectiveStatementImpl other = (CaseEffectiveStatementImpl) obj;
80         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
81     }
82
83     @Override
84     public String toString() {
85         StringBuilder sb = new StringBuilder(CaseEffectiveStatementImpl.class.getSimpleName());
86         sb.append("[");
87         sb.append("qname=");
88         sb.append(getQName());
89         sb.append("]");
90         return sb.toString();
91     }
92 }