Bug 6022: Deviation statement is not fully available in the YANG parser output
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / DeviationEffectiveStatementImpl.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.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Objects;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.model.api.DeviateDefinition;
16 import org.opendaylight.yangtools.yang.model.api.Deviation;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23
24 public class DeviationEffectiveStatementImpl extends DeclaredEffectiveStatementBase<SchemaNodeIdentifier, DeviationStatement>
25         implements Deviation, Immutable {
26     private final SchemaPath targetPath;
27     private final String description;
28     private final String reference;
29     private final List<UnknownSchemaNode> unknownSchemaNodes;
30     private final List<DeviateDefinition> deviateDefinitions;
31
32     public DeviationEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, DeviationStatement, ?> ctx) {
33         super(ctx);
34         this.targetPath = ctx.getStatementArgument().asSchemaPath();
35
36         this.deviateDefinitions = ImmutableList.copyOf(allSubstatementsOfType(DeviateDefinition.class));
37
38         DescriptionEffectiveStatementImpl descriptionStmt = firstEffective(DescriptionEffectiveStatementImpl.class);
39         this.description = (descriptionStmt == null) ? null : descriptionStmt.argument();
40
41         ReferenceEffectiveStatementImpl referenceStmt = firstEffective(ReferenceEffectiveStatementImpl.class);
42         this.reference = (referenceStmt == null) ? null : referenceStmt.argument();
43
44         List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
45         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
46             if (effectiveStatement instanceof UnknownSchemaNode) {
47                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
48             }
49         }
50         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
51     }
52
53     @Override
54     public SchemaPath getTargetPath() {
55         return targetPath;
56     }
57
58     @Override
59     public List<DeviateDefinition> getDeviates() {
60         return deviateDefinitions;
61     }
62
63     @Override
64     public String getDescription() {
65         return description;
66     }
67
68     @Override
69     public String getReference() {
70         return reference;
71     }
72
73     @Override
74     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
75         return unknownSchemaNodes;
76     }
77
78     @Override
79     public int hashCode() {
80         final int prime = 31;
81         int result = 1;
82         result = prime * result + Objects.hashCode(targetPath);
83         result = prime * result + Objects.hashCode(deviateDefinitions);
84         result = prime * result + Objects.hashCode(description);
85         result = prime * result + Objects.hashCode(reference);
86         return result;
87     }
88
89     @Override
90     public boolean equals(final Object obj) {
91         if (this == obj) {
92             return true;
93         }
94         if (obj == null) {
95             return false;
96         }
97         if (getClass() != obj.getClass()) {
98             return false;
99         }
100         DeviationEffectiveStatementImpl other = (DeviationEffectiveStatementImpl) obj;
101         if (!Objects.equals(targetPath, other.targetPath)) {
102             return false;
103         }
104         if (!Objects.equals(deviateDefinitions, other.deviateDefinitions)) {
105             return false;
106         }
107         if (!Objects.equals(description, other.description)) {
108             return false;
109         }
110         if (!Objects.equals(reference, other.reference)) {
111             return false;
112         }
113         return true;
114     }
115
116     @Override
117     public String toString() {
118         return DeviationEffectiveStatementImpl.class.getSimpleName() + "[" +
119                 "targetPath=" + targetPath +
120                 ", deviates=" + deviateDefinitions +
121                 ", description=" + description +
122                 ", reference=" + reference +
123                 "]";
124     }
125 }