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