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 com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
16 import java.util.Date;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Objects;
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.stmt.rfc6020.Utils;
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;
50 public AugmentEffectiveStatementImpl(
51 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
54 this.targetPath = ctx.getStatementArgument().asSchemaPath();
56 final QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
57 this.namespace = rootModuleQName.getNamespace();
58 this.revision = rootModuleQName.getRevision();
60 this.order = ctx.getOrder();
61 this.copyOf = ctx.getOriginalCtx() == null ? null : (AugmentationSchema) ctx.getOriginalCtx().buildEffective();
63 final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
64 this.whenCondition = whenStmt == null ? null : whenStmt.argument();
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);
80 this.actions = actionsBuilder.build();
81 this.notifications = notificationsBuilder.build();
82 this.unknownNodes = listBuilder.build();
86 public Optional<AugmentationSchema> getOriginalDefinition() {
87 return Optional.fromNullable(this.copyOf);
91 public SchemaPath getTargetPath() {
96 public RevisionAwareXPath getWhenCondition() {
102 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
107 public URI getNamespace() {
112 public Set<ActionDefinition> getActions() {
117 public Set<NotificationDefinition> getNotifications() {
118 return notifications;
122 public Date getRevision() {
127 public int hashCode() {
128 final int prime = 17;
130 result = prime * result + Objects.hashCode(targetPath);
131 result = prime * result + Objects.hashCode(whenCondition);
132 result = prime * result + getChildNodes().hashCode();
137 public boolean equals(final Object obj) {
144 if (getClass() != obj.getClass()) {
147 final AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
148 if (!Objects.equals(targetPath, other.targetPath)) {
151 if (!Objects.equals(whenCondition, other.whenCondition)) {
154 if (!getChildNodes().equals(other.getChildNodes())) {
161 public String toString() {
162 return AugmentEffectiveStatementImpl.class.getSimpleName() + "[" + "targetPath=" + targetPath + ", when="
163 + whenCondition + "]";
167 public int compareTo(@Nonnull final AugmentEffectiveStatementImpl 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());
181 if (otherIt.hasNext()) {
184 return this.order - o.order;