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 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;
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;
34 public class AugmentEffectiveStatementImpl
36 AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
37 implements AugmentationSchema, NamespaceRevisionAware,
38 Comparable<AugmentEffectiveStatementImpl> {
39 private final int order;
40 private final SchemaPath targetPath;
41 RevisionAwareXPath whenCondition;
45 ImmutableList<UnknownSchemaNode> unknownNodes;
46 private AugmentationSchema copyOf;
48 public AugmentEffectiveStatementImpl(
49 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx){
53 SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
54 this.targetPath = SchemaPath.create(
55 schemaNodeIdentifier.getPathFromRoot(),
56 schemaNodeIdentifier.isAbsolute());
58 QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
59 this.namespace = rootModuleQName.getNamespace();
60 this.revision = rootModuleQName.getRevision();
62 this.order = ctx.getOrder();
65 initSubstatementCollections();
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();
76 private void initSubstatementCollections() {
77 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
79 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
81 boolean initWhen = false;
82 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
83 if (effectiveStatement instanceof UnknownSchemaNode) {
84 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
85 unknownNodesInit.add(unknownNode);
87 if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
88 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
89 whenCondition = whenStmt.argument();
94 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
97 public void setCopyOf(final AugmentationSchema build) {
102 public Optional<AugmentationSchema> getOriginalDefinition() {
103 return Optional.fromNullable(this.copyOf);
107 public SchemaPath getTargetPath() {
112 public RevisionAwareXPath getWhenCondition() {
113 return whenCondition;
117 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
122 public URI getNamespace() {
127 public Date getRevision() {
132 public int hashCode() {
133 final int prime = 17;
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();
144 public boolean equals(final Object obj) {
151 if (getClass() != obj.getClass()) {
154 AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
155 if (targetPath == null) {
156 if (other.targetPath != null) {
159 } else if (!targetPath.equals(other.targetPath)) {
162 if (whenCondition == null) {
163 if (other.whenCondition != null) {
166 } else if (!whenCondition.equals(other.whenCondition)) {
169 if (!getChildNodes().equals(other.getChildNodes())) {
176 public String toString() {
177 StringBuilder sb = new StringBuilder(
178 AugmentEffectiveStatementImpl.class.getSimpleName());
180 sb.append("targetPath=").append(targetPath);
181 sb.append(", when=").append(whenCondition);
183 return sb.toString();
187 public int compareTo(final AugmentEffectiveStatementImpl o) {
189 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
190 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot()
192 while (thisIt.hasNext()) {
193 if (otherIt.hasNext()) {
194 int comp = thisIt.next().compareTo(otherIt.next());
202 if (otherIt.hasNext()) {
205 return this.order - o.order;