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.spi.meta;
10 import com.google.common.base.Function;
11 import com.google.common.base.Splitter;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSet.Builder;
15 import java.util.ArrayList;
16 import java.util.Collection;
18 import java.util.function.Predicate;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
32 public final class StmtContextUtils {
33 public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
35 private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
36 new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
38 public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
39 return input.buildDeclared();
43 private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
44 new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
46 public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
47 return input.buildEffective();
51 private StmtContextUtils() {
52 throw new UnsupportedOperationException("Utility class");
55 @SuppressWarnings("unchecked")
56 public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
57 return Function.class.cast(BUILD_DECLARED);
60 @SuppressWarnings("unchecked")
61 public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
62 return Function.class.cast(BUILD_EFFECTIVE);
65 @SuppressWarnings("unchecked")
66 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
67 final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
68 for (StmtContext<?, ?, ?> ctx : contexts) {
69 if (producesDeclared(ctx, declaredType)) {
70 return (AT) ctx.getStatementArgument();
76 @SuppressWarnings("unchecked")
77 public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
78 final Class<DT> declaredType) {
79 return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
82 @SuppressWarnings("unchecked")
83 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
84 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
85 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
86 if (producesDeclared(subStmtContext,declaredType)) {
87 return (StmtContext<AT, ?, ?>) subStmtContext;
93 @SuppressWarnings("unchecked")
94 public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllDeclaredSubstatements(
95 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
96 ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
97 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
98 if (producesDeclared(subStmtContext, declaredType)) {
99 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
102 return listBuilder.build();
105 @SuppressWarnings("unchecked")
106 public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllEffectiveSubstatements(
107 final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
108 ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
109 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
110 if (producesDeclared(subStmtContext, type)) {
111 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
114 return listBuilder.build();
117 public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllSubstatements(
118 final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
119 ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
120 listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type));
121 listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type));
122 return listBuilder.build();
125 @SuppressWarnings("unchecked")
126 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
127 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
128 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
129 if (producesDeclared(subStmtContext,declaredType)) {
130 return (StmtContext<AT, ?, ?>) subStmtContext;
136 public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
137 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
138 StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
139 return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
143 public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
144 int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
145 if (startIndex >= types.length) {
149 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
150 if (producesDeclared(subStmtContext,types[startIndex])) {
151 return startIndex + 1 == types.length ? subStmtContext
152 : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
158 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
159 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
160 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
161 if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
162 return subStmtContext;
165 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
166 subStmtContext, declaredType, --sublevel);
167 if (result != null) {
176 public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
177 final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
178 for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
179 if (producesDeclared(subStmtContext, declaredType)) {
180 return subStmtContext;
183 final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
184 if (result != null) {
192 public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
193 final Class<? extends DeclaredStatement<?>> type) {
194 return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
197 public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
198 StmtContext<?,?,?> current = stmtCtx;
199 while (!current.getParentContext().isRootContext()) {
200 current = current.getParentContext();
201 if (producesDeclared(current, UnknownStatementImpl.class)) {
209 public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
210 return producesDeclared(stmtCtx, UnknownStatementImpl.class);
213 public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
214 final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
215 final QNameModule newQNameModule) {
217 final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
218 boolean replaced = false;
219 for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
220 final QName qname = arg.getLastComponent();
221 if (!newQNameModule.equals(qname)) {
222 final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
223 QName.create(newQNameModule, qname.getLocalName()));
224 builder.add(SchemaNodeIdentifier.create(false, newQname));
231 // This makes sure we reuse the collection when a grouping is instantiated in the same module
232 return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
235 public static boolean areFeaturesSupported(final StmtContext<?, ?, ?> stmtContext) {
236 Predicate<QName> isFeatureSupported = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class,
237 SupportedFeatures.SUPPORTED_FEATURES);
238 Collection<StatementContextBase<?, ?, ?>> substatements = new ArrayList<>();
239 substatements.addAll(stmtContext.declaredSubstatements());
240 substatements.addAll(stmtContext.effectiveSubstatements());
242 boolean isSupported = false;
243 boolean containsIfFeature = false;
244 for (StatementContextBase<?, ?, ?> stmt: substatements) {
245 if (stmt.getPublicDefinition().equals(Rfc6020Mapping.IF_FEATURE)) {
246 containsIfFeature = true;
247 if (isFeatureSupported.test((QName) stmt.getStatementArgument())) {
256 return !containsIfFeature || isSupported;