Introduce SchemaNodeIdentifier.asSchemaPath()
[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 import com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableList;
13 import java.net.URI;
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;
33
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;
44
45     public AugmentEffectiveStatementImpl(
46             final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
47         super(ctx);
48
49         this.targetPath = ctx.getStatementArgument().asSchemaPath();
50         QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
51         this.namespace = rootModuleQName.getNamespace();
52         this.revision = rootModuleQName.getRevision();
53
54         this.order = ctx.getOrder();
55
56         initCopyOf(ctx);
57         initSubstatementCollections();
58     }
59
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();
65         }
66     }
67
68     private void initSubstatementCollections() {
69         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
70
71         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
72
73         boolean initWhen = false;
74         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
75             if (effectiveStatement instanceof UnknownSchemaNode) {
76                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
77                 unknownNodesInit.add(unknownNode);
78             }
79             if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
80                 WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
81                 whenCondition = whenStmt.argument();
82                 initWhen = true;
83             }
84         }
85
86         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
87     }
88
89     public void setCopyOf(final AugmentationSchema build) {
90         this.copyOf = build;
91     }
92
93     @Override
94     public Optional<AugmentationSchema> getOriginalDefinition() {
95         return Optional.fromNullable(this.copyOf);
96     }
97
98     @Override
99     public SchemaPath getTargetPath() {
100         return targetPath;
101     }
102
103     @Override
104     public RevisionAwareXPath getWhenCondition() {
105         return whenCondition;
106     }
107
108     @Override
109     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
110         return unknownNodes;
111     }
112
113     @Override
114     public URI getNamespace() {
115         return namespace;
116     }
117
118     @Override
119     public Date getRevision() {
120         return revision;
121     }
122
123     @Override
124     public int hashCode() {
125         final int prime = 17;
126         int result = 1;
127         result = prime * result + Objects.hashCode(targetPath);
128         result = prime * result + Objects.hashCode(whenCondition);
129         result = prime * result + getChildNodes().hashCode();
130         return result;
131     }
132
133     @Override
134     public boolean equals(final Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (obj == null) {
139             return false;
140         }
141         if (getClass() != obj.getClass()) {
142             return false;
143         }
144         AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
145         if (!Objects.equals(targetPath, other.targetPath)) {
146             return false;
147         }
148         if (!Objects.equals(whenCondition, other.whenCondition)) {
149             return false;
150         }
151         if (!getChildNodes().equals(other.getChildNodes())) {
152             return false;
153         }
154         return true;
155     }
156
157     @Override
158     public String toString() {
159         StringBuilder sb = new StringBuilder(AugmentEffectiveStatementImpl.class.getSimpleName());
160         sb.append("[");
161         sb.append("targetPath=").append(targetPath);
162         sb.append(", when=").append(whenCondition);
163         sb.append("]");
164         return sb.toString();
165     }
166
167     @Override
168     public int compareTo(final AugmentEffectiveStatementImpl o) {
169         checkNotNull(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());
175                 if (comp != 0) {
176                     return comp;
177                 }
178             } else {
179                 return 1;
180             }
181         }
182         if (otherIt.hasNext()) {
183             return -1;
184         }
185         return this.order - o.order;
186     }
187 }