0772ef501a01cdd47690e21ca35d2b1fafbf8900
[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.ArrayList;
15 import java.util.HashMap;
16 import java.util.LinkedHashSet;
17 import java.util.List;
18 import java.util.Map;
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.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;
36
37 public final class UsesEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, UsesStatement>
38         implements UsesNode {
39     private final SchemaPath groupingPath;
40     private final boolean addedByUses;
41     private final Map<SchemaPath, SchemaNode> refines;
42     private final Set<AugmentationSchema> augmentations;
43     private final List<UnknownSchemaNode> unknownNodes;
44     private final RevisionAwareXPath whenCondition;
45
46     public UsesEffectiveStatementImpl(
47             final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
48         super(ctx);
49
50         // initGroupingPath
51         final StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx =
52                 ctx.getFromNamespace(GroupingNamespace.class, ctx.getStatementArgument());
53         this.groupingPath = grpCtx.getSchemaPath().get();
54
55         // initCopyType
56         addedByUses = ctx.getCopyHistory().contains(CopyType.ADDED_BY_USES);
57
58         // initSubstatementCollections
59         final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
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     @Nonnull
86     @Override
87     public SchemaPath getGroupingPath() {
88         return groupingPath;
89     }
90
91     @Nonnull
92     @Override
93     public Set<AugmentationSchema> getAugmentations() {
94         return augmentations;
95     }
96
97     @Override
98     public boolean isAugmenting() {
99         return false;
100     }
101
102     @Override
103     public boolean isAddedByUses() {
104         return addedByUses;
105     }
106
107     @Nonnull
108     @Override
109     public Map<SchemaPath, SchemaNode> getRefines() {
110         return refines;
111     }
112
113     @Nonnull
114     @Override
115     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
116         return unknownNodes;
117     }
118
119     @Nonnull
120     @Override
121     public Optional<RevisionAwareXPath> getWhenCondition() {
122         return Optional.fromNullable(whenCondition);
123     }
124
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = 1;
129         result = prime * result + Objects.hashCode(groupingPath);
130         result = prime * result + Objects.hashCode(augmentations);
131         return result;
132     }
133
134     @Override
135     public boolean equals(final Object obj) {
136         if (this == obj) {
137             return true;
138         }
139         if (obj == null) {
140             return false;
141         }
142         if (getClass() != obj.getClass()) {
143             return false;
144         }
145         final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
146         return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
147     }
148
149     @Override
150     public String toString() {
151         return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" + groupingPath + "]";
152     }
153 }