2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.Date;
17 import java.util.Iterator;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Objects;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
35 public class AugmentEffectiveStatementImpl
37 AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
38 implements AugmentationSchema, NamespaceRevisionAware,
39 Comparable<AugmentEffectiveStatementImpl> {
40 private final int order;
41 private final SchemaPath targetPath;
42 RevisionAwareXPath whenCondition;
46 ImmutableList<UnknownSchemaNode> unknownNodes;
47 private AugmentationSchema copyOf;
49 public AugmentEffectiveStatementImpl(
50 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx){
54 SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
55 this.targetPath = SchemaPath.create(
56 schemaNodeIdentifier.getPathFromRoot(),
57 schemaNodeIdentifier.isAbsolute());
59 QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
60 this.namespace = rootModuleQName.getNamespace();
61 this.revision = rootModuleQName.getRevision();
63 this.order = ctx.getOrder();
66 initSubstatementCollections();
69 private void initCopyOf(
70 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
71 StatementContextBase<?, ?, ?> originalCtx = ctx.getOriginalCtx();
72 if (originalCtx != null) {
73 this.copyOf = (AugmentationSchema) originalCtx.buildEffective();
77 private void initSubstatementCollections() {
78 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
80 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
82 boolean initWhen = false;
83 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
84 if (effectiveStatement instanceof UnknownSchemaNode) {
85 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
86 unknownNodesInit.add(unknownNode);
88 if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
89 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
90 whenCondition = whenStmt.argument();
95 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
98 public void setCopyOf(final AugmentationSchema build) {
103 public Optional<AugmentationSchema> getOriginalDefinition() {
104 return Optional.fromNullable(this.copyOf);
108 public SchemaPath getTargetPath() {
113 public RevisionAwareXPath getWhenCondition() {
114 return whenCondition;
118 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
123 public URI getNamespace() {
128 public Date getRevision() {
133 public int hashCode() {
134 final int prime = 17;
136 result = prime * result + Objects.hashCode(targetPath);
137 result = prime * result + Objects.hashCode(whenCondition);
138 result = prime * result + getChildNodes().hashCode();
143 public boolean equals(final Object obj) {
150 if (getClass() != obj.getClass()) {
153 AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
154 if (targetPath == null) {
155 if (other.targetPath != null) {
158 } else if (!targetPath.equals(other.targetPath)) {
161 if (whenCondition == null) {
162 if (other.whenCondition != null) {
165 } else if (!whenCondition.equals(other.whenCondition)) {
168 if (!getChildNodes().equals(other.getChildNodes())) {
175 public String toString() {
176 StringBuilder sb = new StringBuilder(
177 AugmentEffectiveStatementImpl.class.getSimpleName());
179 sb.append("targetPath=").append(targetPath);
180 sb.append(", when=").append(whenCondition);
182 return sb.toString();
186 public int compareTo(final AugmentEffectiveStatementImpl o) {
188 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
189 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot()
191 while (thisIt.hasNext()) {
192 if (otherIt.hasNext()) {
193 int comp = thisIt.next().compareTo(otherIt.next());
201 if (otherIt.hasNext()) {
204 return this.order - o.order;