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.Collection;
15 import java.util.HashMap;
16 import java.util.LinkedHashSet;
17 import java.util.LinkedList;
18 import java.util.List;
20 import java.util.Objects;
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.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
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 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
59 final List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
60 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
61 final Map<SchemaPath, SchemaNode> refinesInit = new HashMap<>();
62 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
63 if (effectiveStatement instanceof UnknownSchemaNode) {
64 final UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
65 unknownNodesInit.add(unknownNode);
67 if (effectiveStatement instanceof AugmentationSchema) {
68 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
69 augmentationsInit.add(augmentationSchema);
71 if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
72 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
73 final SchemaNodeIdentifier identifier = refineStmt.argument();
74 refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
77 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
78 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
79 this.refines = ImmutableMap.copyOf(refinesInit);
81 final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
82 this.whenCondition = (whenStmt == null) ? null : whenStmt.argument();
86 public SchemaPath getGroupingPath() {
91 public Set<AugmentationSchema> getAugmentations() {
96 public boolean isAugmenting() {
101 public boolean isAddedByUses() {
106 public Map<SchemaPath, SchemaNode> getRefines() {
111 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
116 public Optional<RevisionAwareXPath> getWhenCondition() {
117 return Optional.fromNullable(whenCondition);
121 public int hashCode() {
122 final int prime = 31;
124 result = prime * result + Objects.hashCode(groupingPath);
125 result = prime * result + Objects.hashCode(augmentations);
130 public boolean equals(final Object obj) {
137 if (getClass() != obj.getClass()) {
140 final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
141 return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
145 public String toString() {
146 return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" +