BUG-7052: Move qnameFromArgument to StmtContextUtils
[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 com.google.common.collect.ImmutableSet;
15 import java.net.URI;
16 import java.util.Date;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Objects;
20 import java.util.Set;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
36
37 public final class AugmentEffectiveStatementImpl extends
38         AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement> implements
39         AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
40     private final SchemaPath targetPath;
41     private final URI namespace;
42     private final Date revision;
43     private final int order;
44     private final Set<ActionDefinition> actions;
45     private final Set<NotificationDefinition> notifications;
46     private final List<UnknownSchemaNode> unknownNodes;
47     private final RevisionAwareXPath whenCondition;
48     private final AugmentationSchema copyOf;
49
50     public AugmentEffectiveStatementImpl(
51             final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
52         super(ctx);
53
54         this.targetPath = ctx.getStatementArgument().asSchemaPath();
55
56         final QNameModule rootModuleQName = StmtContextUtils.getRootModuleQName(ctx);
57         this.namespace = rootModuleQName.getNamespace();
58         this.revision = rootModuleQName.getRevision();
59
60         this.order = ctx.getOrder();
61         this.copyOf = ctx.getOriginalCtx() == null ? null : (AugmentationSchema) ctx.getOriginalCtx().buildEffective();
62
63         final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
64         this.whenCondition = whenStmt == null ? null : whenStmt.argument();
65
66         // initSubstatementCollections
67         final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
68         final ImmutableSet.Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
69         final ImmutableList.Builder<UnknownSchemaNode> listBuilder = new ImmutableList.Builder<>();
70         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
71             if (effectiveStatement instanceof ActionDefinition) {
72                 actionsBuilder.add((ActionDefinition) effectiveStatement);
73             } else if (effectiveStatement instanceof NotificationDefinition) {
74                 notificationsBuilder.add((NotificationDefinition) effectiveStatement);
75             } else if (effectiveStatement instanceof UnknownSchemaNode) {
76                 listBuilder.add((UnknownSchemaNode) effectiveStatement);
77             }
78         }
79
80         this.actions = actionsBuilder.build();
81         this.notifications = notificationsBuilder.build();
82         this.unknownNodes = listBuilder.build();
83     }
84
85     @Override
86     public Optional<AugmentationSchema> getOriginalDefinition() {
87         return Optional.fromNullable(this.copyOf);
88     }
89
90     @Override
91     public SchemaPath getTargetPath() {
92         return targetPath;
93     }
94
95     @Override
96     public RevisionAwareXPath getWhenCondition() {
97         return whenCondition;
98     }
99
100     @Nonnull
101     @Override
102     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
103         return unknownNodes;
104     }
105
106     @Override
107     public URI getNamespace() {
108         return namespace;
109     }
110
111     @Override
112     public Set<ActionDefinition> getActions() {
113         return actions;
114     }
115
116     @Override
117     public Set<NotificationDefinition> getNotifications() {
118         return notifications;
119     }
120
121     @Override
122     public Date getRevision() {
123         return revision;
124     }
125
126     @Override
127     public int hashCode() {
128         final int prime = 17;
129         int result = 1;
130         result = prime * result + Objects.hashCode(targetPath);
131         result = prime * result + Objects.hashCode(whenCondition);
132         result = prime * result + getChildNodes().hashCode();
133         return result;
134     }
135
136     @Override
137     public boolean equals(final Object obj) {
138         if (this == obj) {
139             return true;
140         }
141         if (obj == null) {
142             return false;
143         }
144         if (getClass() != obj.getClass()) {
145             return false;
146         }
147         final AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
148         if (!Objects.equals(targetPath, other.targetPath)) {
149             return false;
150         }
151         if (!Objects.equals(whenCondition, other.whenCondition)) {
152             return false;
153         }
154         if (!getChildNodes().equals(other.getChildNodes())) {
155             return false;
156         }
157         return true;
158     }
159
160     @Override
161     public String toString() {
162         return AugmentEffectiveStatementImpl.class.getSimpleName() + "[" + "targetPath=" + targetPath + ", when="
163                 + whenCondition + "]";
164     }
165
166     @Override
167     public int compareTo(@Nonnull final AugmentEffectiveStatementImpl o) {
168         checkNotNull(o);
169         final Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
170         final Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
171         while (thisIt.hasNext()) {
172             if (otherIt.hasNext()) {
173                 final int comp = thisIt.next().compareTo(otherIt.next());
174                 if (comp != 0) {
175                     return comp;
176                 }
177             } else {
178                 return 1;
179             }
180         }
181         if (otherIt.hasNext()) {
182             return -1;
183         }
184         return this.order - o.order;
185     }
186 }