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