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