Bug 3670 (part 1/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 / AugmentEffectiveStatementImpl.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 static com.google.common.base.Preconditions.checkNotNull;
11
12 import org.opendaylight.yangtools.yang.common.QNameModule;
13 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
14 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
15 import java.util.Collection;
16 import java.util.LinkedList;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import com.google.common.base.Optional;
22 import com.google.common.collect.ImmutableList;
23 import java.net.URI;
24 import java.util.Date;
25 import java.util.Iterator;
26 import java.util.List;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
29 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
30 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
33
34 public class AugmentEffectiveStatementImpl
35         extends
36         AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
37         implements AugmentationSchema, NamespaceRevisionAware,
38         Comparable<AugmentEffectiveStatementImpl> {
39     private final int order;
40     private final SchemaPath targetPath;
41     RevisionAwareXPath whenCondition;
42
43     URI namespace;
44     Date revision;
45     ImmutableList<UnknownSchemaNode> unknownNodes;
46     private AugmentationSchema copyOf;
47
48     public AugmentEffectiveStatementImpl(
49             StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx){
50
51         super(ctx);
52
53         SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
54         this.targetPath = SchemaPath.create(
55                 schemaNodeIdentifier.getPathFromRoot(),
56                 schemaNodeIdentifier.isAbsolute());
57
58         QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
59         this.namespace = rootModuleQName.getNamespace();
60         this.revision = rootModuleQName.getRevision();
61
62         this.order = ctx.getOrder();
63
64         initCopyOf(ctx);
65         initSubstatementCollections();
66     }
67
68     private void initCopyOf(
69             StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
70         StatementContextBase<?, ?, ?> originalCtx = ctx.getOriginalCtx();
71         if (originalCtx != null) {
72             this.copyOf = (AugmentationSchema) originalCtx.buildEffective();
73         }
74     }
75
76     private void initSubstatementCollections() {
77         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
78
79         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
80
81         boolean initWhen = false;
82         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
83             if (effectiveStatement instanceof UnknownSchemaNode) {
84                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
85                 unknownNodesInit.add(unknownNode);
86             }
87             if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
88                 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
89                 whenCondition = whenStmt.argument();
90                 initWhen = true;
91             }
92         }
93
94         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
95     }
96
97     public void setCopyOf(final AugmentationSchema build) {
98         this.copyOf = build;
99     }
100
101     @Override
102     public Optional<AugmentationSchema> getOriginalDefinition() {
103         return Optional.fromNullable(this.copyOf);
104     }
105
106     @Override
107     public SchemaPath getTargetPath() {
108         return targetPath;
109     }
110
111     @Override
112     public RevisionAwareXPath getWhenCondition() {
113         return whenCondition;
114     }
115
116     @Override
117     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
118         return unknownNodes;
119     }
120
121     @Override
122     public URI getNamespace() {
123         return namespace;
124     }
125
126     @Override
127     public Date getRevision() {
128         return revision;
129     }
130
131     @Override
132     public int hashCode() {
133         final int prime = 17;
134         int result = 1;
135         result = prime * result
136                 + ((targetPath == null) ? 0 : targetPath.hashCode());
137         result = prime * result
138                 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
139         result = prime * result + getChildNodes().hashCode();
140         return result;
141     }
142
143     @Override
144     public boolean equals(final Object obj) {
145         if (this == obj) {
146             return true;
147         }
148         if (obj == null) {
149             return false;
150         }
151         if (getClass() != obj.getClass()) {
152             return false;
153         }
154         AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
155         if (targetPath == null) {
156             if (other.targetPath != null) {
157                 return false;
158             }
159         } else if (!targetPath.equals(other.targetPath)) {
160             return false;
161         }
162         if (whenCondition == null) {
163             if (other.whenCondition != null) {
164                 return false;
165             }
166         } else if (!whenCondition.equals(other.whenCondition)) {
167             return false;
168         }
169         if (!getChildNodes().equals(other.getChildNodes())) {
170             return false;
171         }
172         return true;
173     }
174
175     @Override
176     public String toString() {
177         StringBuilder sb = new StringBuilder(
178                 AugmentEffectiveStatementImpl.class.getSimpleName());
179         sb.append("[");
180         sb.append("targetPath=").append(targetPath);
181         sb.append(", when=").append(whenCondition);
182         sb.append("]");
183         return sb.toString();
184     }
185
186     @Override
187     public int compareTo(final AugmentEffectiveStatementImpl o) {
188         checkNotNull(o);
189         Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
190         Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot()
191                 .iterator();
192         while (thisIt.hasNext()) {
193             if (otherIt.hasNext()) {
194                 int comp = thisIt.next().compareTo(otherIt.next());
195                 if (comp != 0) {
196                     return comp;
197                 }
198             } else {
199                 return 1;
200             }
201         }
202         if (otherIt.hasNext()) {
203             return -1;
204         }
205         return this.order - o.order;
206     }
207 }