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
35 extends AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
36 implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
37 private final SchemaPath targetPath;
38 private final URI namespace;
39 private final Date revision;
40 private final int order;
41 private ImmutableList<UnknownSchemaNode> unknownNodes;
42 private RevisionAwareXPath whenCondition;
43 private AugmentationSchema copyOf;
45 public AugmentEffectiveStatementImpl(
46 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
49 this.targetPath = ctx.getStatementArgument().asSchemaPath();
50 QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
51 this.namespace = rootModuleQName.getNamespace();
52 this.revision = rootModuleQName.getRevision();
54 this.order = ctx.getOrder();
57 initSubstatementCollections();
60 private void initCopyOf(
61 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
62 StatementContextBase<?, ?, ?> originalCtx = ctx.getOriginalCtx();
63 if (originalCtx != null) {
64 this.copyOf = (AugmentationSchema) originalCtx.buildEffective();
68 private void initSubstatementCollections() {
69 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
71 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
73 boolean initWhen = false;
74 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
75 if (effectiveStatement instanceof UnknownSchemaNode) {
76 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
77 unknownNodesInit.add(unknownNode);
79 if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
80 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
81 whenCondition = whenStmt.argument();
86 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
89 public void setCopyOf(final AugmentationSchema build) {
94 public Optional<AugmentationSchema> getOriginalDefinition() {
95 return Optional.fromNullable(this.copyOf);
99 public SchemaPath getTargetPath() {
104 public RevisionAwareXPath getWhenCondition() {
105 return whenCondition;
109 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
114 public URI getNamespace() {
119 public Date getRevision() {
124 public int hashCode() {
125 final int prime = 17;
127 result = prime * result + Objects.hashCode(targetPath);
128 result = prime * result + Objects.hashCode(whenCondition);
129 result = prime * result + getChildNodes().hashCode();
134 public boolean equals(final Object obj) {
141 if (getClass() != obj.getClass()) {
144 AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
145 if (!Objects.equals(targetPath, other.targetPath)) {
148 if (!Objects.equals(whenCondition, other.whenCondition)) {
151 if (!getChildNodes().equals(other.getChildNodes())) {
158 public String toString() {
159 StringBuilder sb = new StringBuilder(AugmentEffectiveStatementImpl.class.getSimpleName());
161 sb.append("targetPath=").append(targetPath);
162 sb.append(", when=").append(whenCondition);
164 return sb.toString();
168 public int compareTo(final AugmentEffectiveStatementImpl o) {
170 Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
171 Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
172 while (thisIt.hasNext()) {
173 if (otherIt.hasNext()) {
174 int comp = thisIt.next().compareTo(otherIt.next());
182 if (otherIt.hasNext()) {
185 return this.order - o.order;