Bug 3670 (part 2/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 / UnknownEffectiveStatementImpl.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
25
26 public class UnknownEffectiveStatementImpl extends EffectiveStatementBase<String, UnknownStatement<String>> implements
27         UnknownSchemaNode {
28
29     private boolean augmenting;
30     private boolean addedByUses;
31     private UnknownSchemaNode original;
32
33     private final QName qName;
34     private final SchemaPath path;
35     private ExtensionDefinition extension;
36     private String description;
37     private String reference;
38     private Status status = Status.CURRENT;
39     private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
40     private QName nodeType;
41     private String nodeParameter;
42
43     public UnknownEffectiveStatementImpl(final StmtContext<String, UnknownStatement<String>, ?> ctx) {
44         super(ctx);
45
46         final StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>> extensionInit = ctx
47                 .getAllFromNamespace(ExtensionNamespace.class).get(ctx.getPublicDefinition().getStatementName());
48         if (extensionInit == null) {
49             extension = null;
50             nodeType = ctx.getPublicDefinition().getArgumentName();
51
52             if (argument() == null || argument().isEmpty()) {
53                 qName = nodeType;
54             } else {
55                 qName = QName.create(Utils.qNameFromArgument(ctx, ctx.getStatementArgument()).getModule(), argument());
56             }
57         } else {
58             extension = (ExtensionEffectiveStatementImpl) extensionInit.buildEffective();
59             nodeType = extension.getQName();
60
61             if (argument() == null || argument().isEmpty()) {
62                 qName = extension.getQName();
63             } else {
64                 qName = QName.create(Utils.qNameFromArgument(ctx, ctx.getStatementArgument()).getModule(), argument());
65             }
66         }
67
68         path = Utils.getSchemaPath(ctx);
69         nodeParameter = argument();
70
71         // TODO init other fields (see Bug1412Test)
72
73         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
74             if (effectiveStatement instanceof DescriptionEffectiveStatementImpl) {
75                 description = ((DescriptionEffectiveStatementImpl) effectiveStatement).argument();
76             }
77             if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
78                 reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
79             }
80             if (effectiveStatement instanceof UnknownEffectiveStatementImpl) {
81                 unknownNodes.add((UnknownEffectiveStatementImpl) effectiveStatement);
82             }
83         }
84
85         initCopyType(ctx);
86     }
87
88     private void initCopyType(final StmtContext<String, UnknownStatement<String>, ?> ctx) {
89
90         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
91
92         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
93             augmenting = true;
94         }
95         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
96             addedByUses = true;
97         }
98         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
99             addedByUses = augmenting = true;
100         }
101
102         if (ctx.getOriginalCtx() != null) {
103             original = (UnknownSchemaNode) ctx.getOriginalCtx().buildEffective();
104         }
105     }
106
107     @Override
108     public QName getNodeType() {
109         return nodeType;
110     }
111
112     @Override
113     public String getNodeParameter() {
114         return nodeParameter;
115     }
116
117     @Override
118     public boolean isAddedByUses() {
119         return addedByUses;
120     }
121
122     @Override
123     public ExtensionDefinition getExtensionDefinition() {
124         return extension;
125     }
126
127     @Override
128     public QName getQName() {
129         return qName;
130     }
131
132     @Override
133     public SchemaPath getPath() {
134         return path;
135     }
136
137     @Override
138     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
139         return unknownNodes;
140     }
141
142     @Override
143     public String getDescription() {
144         return description;
145     }
146
147     @Override
148     public String getReference() {
149         return reference;
150     }
151
152     @Override
153     public Status getStatus() {
154         return status;
155     }
156
157     @Override
158     public int hashCode() {
159         final int prime = 31;
160         int result = 1;
161         result = prime * result + ((qName == null) ? 0 : qName.hashCode());
162         result = prime * result + ((path == null) ? 0 : path.hashCode());
163         result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());
164         result = prime * result + ((nodeParameter == null) ? 0 : nodeParameter.hashCode());
165         return result;
166     }
167
168     @Override
169     public boolean equals(final Object obj) {
170         if (this == obj) {
171             return true;
172         }
173         if (obj == null) {
174             return false;
175         }
176         if (getClass() != obj.getClass()) {
177             return false;
178         }
179         UnknownEffectiveStatementImpl other = (UnknownEffectiveStatementImpl) obj;
180         if (qName == null) {
181             if (other.qName != null) {
182                 return false;
183             }
184         } else if (!qName.equals(other.qName)) {
185             return false;
186         }
187         if (path == null) {
188             if (other.path != null) {
189                 return false;
190             }
191         } else if (!path.equals(other.path)) {
192             return false;
193         }
194         if (nodeType == null) {
195             if (other.nodeType != null) {
196                 return false;
197             }
198         } else if (!nodeType.equals(other.nodeType)) {
199             return false;
200         }
201         if (nodeParameter == null) {
202             if (other.nodeParameter != null) {
203                 return false;
204             }
205         } else if (!nodeParameter.equals(other.nodeParameter)) {
206             return false;
207         }
208         return true;
209     }
210
211     @Override
212     public String toString() {
213         StringBuilder sb = new StringBuilder();
214         sb.append(nodeType.getNamespace());
215         sb.append(":");
216         sb.append(nodeType.getLocalName());
217         sb.append(" ");
218         sb.append(nodeParameter);
219         return sb.toString();
220     }
221 }