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 com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.LinkedHashSet;
17 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.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 public final class UsesEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, UsesStatement> implements UsesNode {
38 private final SchemaPath groupingPath;
39 private final boolean addedByUses;
40 private final Map<SchemaPath, SchemaNode> refines;
41 private final Set<AugmentationSchema> augmentations;
42 private final List<UnknownSchemaNode> unknownNodes;
43 private final RevisionAwareXPath whenCondition;
45 public UsesEffectiveStatementImpl(
46 final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
50 final StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx = ctx.getFromNamespace(
51 GroupingNamespace.class, ctx.getStatementArgument());
52 this.groupingPath = grpCtx.getSchemaPath().get();
55 addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
57 // initSubstatementCollections
58 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
59 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
60 final Map<SchemaPath, SchemaNode> refinesInit = new HashMap<>();
61 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
62 if (effectiveStatement instanceof UnknownSchemaNode) {
63 final UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
64 unknownNodesInit.add(unknownNode);
66 if (effectiveStatement instanceof AugmentationSchema) {
67 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
68 augmentationsInit.add(augmentationSchema);
70 if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
71 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
72 final SchemaNodeIdentifier identifier = refineStmt.argument();
73 refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
76 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
77 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
78 this.refines = ImmutableMap.copyOf(refinesInit);
80 final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
81 this.whenCondition = whenStmt == null ? null : whenStmt.argument();
86 public SchemaPath getGroupingPath() {
92 public Set<AugmentationSchema> getAugmentations() {
97 public boolean isAugmenting() {
102 public boolean isAddedByUses() {
108 public Map<SchemaPath, SchemaNode> getRefines() {
114 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
120 public Optional<RevisionAwareXPath> getWhenCondition() {
121 return Optional.fromNullable(whenCondition);
125 public int hashCode() {
126 final int prime = 31;
128 result = prime * result + Objects.hashCode(groupingPath);
129 result = prime * result + Objects.hashCode(augmentations);
134 public boolean equals(final Object obj) {
141 if (getClass() != obj.getClass()) {
144 final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
145 return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
149 public String toString() {
150 return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" +