Merge "Add hooking into NormalizedNodeParsers."
[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
12 import java.util.HashSet;
13 import java.util.LinkedList;
14 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.List;
18 import java.util.Set;
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.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import java.util.Collection;
26 import org.opendaylight.yangtools.yang.model.api.stmt.OutputStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
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;
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         // :TODO init other fields
56
57         initSubstatementCollections();
58         initCopyType(ctx);
59     }
60
61     private void initCopyType(
62             StmtContext<QName, OutputStatement, EffectiveStatement<QName, OutputStatement>> ctx) {
63
64         TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
65         switch (typeOfCopy) {
66         case ADDED_BY_AUGMENTATION:
67             augmenting = true;
68             original = (ContainerSchemaNode) ctx.getOriginalCtx()
69                     .buildEffective();
70             break;
71         case ADDED_BY_USES:
72             addedByUses = true;
73             original = (ContainerSchemaNode) ctx.getOriginalCtx()
74                     .buildEffective();
75             break;
76         default:
77             break;
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         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         }
97
98         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
99         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
100     }
101
102     @Override
103     public QName getQName() {
104         return qname;
105     }
106
107     @Override
108     public SchemaPath getPath() {
109         return path;
110     }
111
112     @Override
113     public boolean isAugmenting() {
114         return augmenting;
115     }
116
117     @Override
118     public boolean isAddedByUses() {
119         return addedByUses;
120     }
121
122     @Override
123     public boolean isConfiguration() {
124         return configuration;
125     }
126
127     @Override
128     public ConstraintDefinition getConstraints() {
129         return constraints;
130     }
131
132     @Override
133     public Set<AugmentationSchema> getAvailableAugmentations() {
134         return augmentations;
135     }
136
137     @Override
138     public boolean isPresenceContainer() {
139         return presence;
140     }
141
142     @Override
143     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
144         return unknownNodes;
145     }
146
147     @Override
148     public int hashCode() {
149         final int prime = 31;
150         int result = 1;
151         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
152         result = prime * result + ((path == null) ? 0 : path.hashCode());
153         return result;
154     }
155
156     @Override
157     public boolean equals(final Object obj) {
158         if (this == obj) {
159             return true;
160         }
161         if (obj == null) {
162             return false;
163         }
164         if (getClass() != obj.getClass()) {
165             return false;
166         }
167         OutputEffectiveStatementImpl other = (OutputEffectiveStatementImpl) obj;
168         if (qname == null) {
169             if (other.qname != null) {
170                 return false;
171             }
172         } else if (!qname.equals(other.qname)) {
173             return false;
174         }
175         if (path == null) {
176             if (other.path != null) {
177                 return false;
178             }
179         } else if (!path.equals(other.path)) {
180             return false;
181         }
182         return true;
183     }
184
185     @Override
186     public String toString() {
187         return "RPC Output " + qname.getLocalName();
188     }
189
190 }