Bug 4506: Honor if-feature during SchemaContext assembly
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.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.spi.meta;
9
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;
17 import java.util.Set;
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;
31
32 public final class StmtContextUtils {
33     public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
34
35     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
36             new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
37         @Override
38         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
39             return input.buildDeclared();
40         }
41     };
42
43     private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
44             new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
45         @Override
46         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
47             return input.buildEffective();
48         }
49     };
50
51     private StmtContextUtils() {
52         throw new UnsupportedOperationException("Utility class");
53     }
54
55     @SuppressWarnings("unchecked")
56     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
57         return Function.class.cast(BUILD_DECLARED);
58     }
59
60     @SuppressWarnings("unchecked")
61     public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
62         return Function.class.cast(BUILD_EFFECTIVE);
63     }
64
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();
71             }
72         }
73         return null;
74     }
75
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;
80     }
81
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;
88             }
89         }
90         return null;
91     }
92
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);
100             }
101         }
102         return listBuilder.build();
103     }
104
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);
112             }
113         }
114         return listBuilder.build();
115     }
116
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();
123     }
124
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;
131             }
132         }
133         return null;
134     }
135
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);
140     }
141
142     @SafeVarargs
143     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
144             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
145         if (startIndex >= types.length) {
146             return null;
147         }
148
149         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
150             if (producesDeclared(subStmtContext,types[startIndex])) {
151                 return startIndex + 1 == types.length ? subStmtContext
152                         : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
153             }
154         }
155         return null;
156     }
157
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;
163             }
164             if (sublevel > 1) {
165                 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
166                     subStmtContext, declaredType, --sublevel);
167                 if (result != null) {
168                     return result;
169                 }
170             }
171         }
172
173         return null;
174     }
175
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;
181             }
182
183             final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
184             if (result != null) {
185                 return result;
186             }
187         }
188
189         return null;
190     }
191
192     public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
193             final Class<? extends DeclaredStatement<?>> type) {
194         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
195     }
196
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)) {
202                 return true;
203             }
204         }
205
206         return false;
207     }
208
209     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
210         return producesDeclared(stmtCtx, UnknownStatementImpl.class);
211     }
212
213     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
214             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
215             final QNameModule newQNameModule) {
216
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));
225                 replaced = true;
226             } else {
227                 builder.add(arg);
228             }
229         }
230
231         // This makes sure we reuse the collection when a grouping is instantiated in the same module
232         return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
233     }
234
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());
241
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())) {
248                     isSupported = true;
249                 } else {
250                     isSupported = false;
251                     break;
252                 }
253             }
254         }
255
256         return !containsIfFeature || isSupported;
257     }
258 }