cb212de724f0c1766b6887ef58d8f4aa84567842
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / UsesEffectiveStatementImpl.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 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;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Set;
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;
36
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;
44
45     public UsesEffectiveStatementImpl(
46             final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
47         super(ctx);
48
49         // initGroupingPath
50         final StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx = ctx.getFromNamespace(
51                 GroupingNamespace.class, ctx.getStatementArgument());
52         this.groupingPath = grpCtx.getSchemaPath().get();
53
54         // initCopyType
55         addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
56
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);
66             }
67             if (effectiveStatement instanceof AugmentationSchema) {
68                 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
69                 augmentationsInit.add(augmentationSchema);
70             }
71             if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
72                 final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
73                 final SchemaNodeIdentifier identifier = refineStmt.argument();
74                 refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
75             }
76         }
77         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
78         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
79         this.refines = ImmutableMap.copyOf(refinesInit);
80
81         final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
82         this.whenCondition = (whenStmt == null) ? null : whenStmt.argument();
83     }
84
85     @Override
86     public SchemaPath getGroupingPath() {
87         return groupingPath;
88     }
89
90     @Override
91     public Set<AugmentationSchema> getAugmentations() {
92         return augmentations;
93     }
94
95     @Override
96     public boolean isAugmenting() {
97         return false;
98     }
99
100     @Override
101     public boolean isAddedByUses() {
102         return addedByUses;
103     }
104
105     @Override
106     public Map<SchemaPath, SchemaNode> getRefines() {
107         return refines;
108     }
109
110     @Override
111     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
112         return unknownNodes;
113     }
114
115     @Override
116     public Optional<RevisionAwareXPath> getWhenCondition() {
117         return Optional.fromNullable(whenCondition);
118     }
119
120     @Override
121     public int hashCode() {
122         final int prime = 31;
123         int result = 1;
124         result = prime * result + Objects.hashCode(groupingPath);
125         result = prime * result + Objects.hashCode(augmentations);
126         return result;
127     }
128
129     @Override
130     public boolean equals(final Object obj) {
131         if (this == obj) {
132             return true;
133         }
134         if (obj == null) {
135             return false;
136         }
137         if (getClass() != obj.getClass()) {
138             return false;
139         }
140         final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
141         return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
142     }
143
144     @Override
145     public String toString() {
146         return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" +
147                 groupingPath +
148                 "]";
149     }
150 }