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;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.Objects;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
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.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
34 public class AugmentEffectiveStatementImpl
36 AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
37 implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
38 private final SchemaPath targetPath;
39 private final URI namespace;
40 private final Date revision;
41 private final int order;
42 private ImmutableList<UnknownSchemaNode> unknownNodes;
43 private RevisionAwareXPath whenCondition;
44 private AugmentationSchema copyOf;
46 public AugmentEffectiveStatementImpl(
47 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
50 SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
51 this.targetPath = SchemaPath.create(
52 schemaNodeIdentifier.getPathFromRoot(),
53 schemaNodeIdentifier.isAbsolute());
55 QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
56 this.namespace = rootModuleQName.getNamespace();
57 this.revision = rootModuleQName.getRevision();
59 this.order = ctx.getOrder();
62 initSubstatementCollections();
65 private void initCopyOf(
66 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
67 StatementContextBase<?, ?, ?> originalCtx = ctx.getOriginalCtx();
68 if (originalCtx != null) {
69 this.copyOf = (AugmentationSchema) originalCtx.buildEffective();
73 private void initSubstatementCollections() {
74 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
76 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
78 boolean initWhen = false;
79 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
80 if (effectiveStatement instanceof UnknownSchemaNode) {
81 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
82 unknownNodesInit.add(unknownNode);
84 if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
85 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
86 whenCondition = whenStmt.argument();
91 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
94 public void setCopyOf(final AugmentationSchema build) {
99 public Optional<AugmentationSchema> getOriginalDefinition() {
100 return Optional.fromNullable(this.copyOf);
104 public SchemaPath getTargetPath() {
109 public RevisionAwareXPath getWhenCondition() {
110 return whenCondition;
114 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
119 public URI getNamespace() {
124 public Date getRevision() {
129 public int hashCode() {
130 final int prime = 17;
132 result = prime * result + Objects.hashCode(targetPath);
133 result = prime * result + Objects.hashCode(whenCondition);
134 result = prime * result + getChildNodes().hashCode();
139 public boolean equals(final Object obj) {
146 if (getClass() != obj.getClass()) {
149 AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
150 if (!Objects.equals(targetPath, other.targetPath)) {
153 if (!Objects.equals(whenCondition, other.whenCondition)) {
156 if (!getChildNodes().equals(other.getChildNodes())) {
163 public String toString() {
164 StringBuilder sb = new StringBuilder(
165 AugmentEffectiveStatementImpl.class.getSimpleName());
167 sb.append("targetPath=").append(targetPath);
168 sb.append(", when=").append(whenCondition);
170 return sb.toString();
174 public int compareTo(final AugmentEffectiveStatementImpl o) {
176 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
177 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
178 while (thisIt.hasNext()) {
179 if (otherIt.hasNext()) {
180 int comp = thisIt.next().compareTo(otherIt.next());
188 if (otherIt.hasNext()) {
191 return this.order - o.order;