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