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