Bug 5059: Do not fail when 'refine' targets an extension.
[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.Collection;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
23
24 public final class StmtContextUtils {
25     public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
26
27     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
28             new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
29         @Override
30         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
31             return input.buildDeclared();
32         }
33     };
34
35     private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
36             new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
37         @Override
38         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
39             return input.buildEffective();
40         }
41     };
42
43     private StmtContextUtils() {
44         throw new UnsupportedOperationException("Utility class");
45     }
46
47     @SuppressWarnings("unchecked")
48     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
49         return Function.class.cast(BUILD_DECLARED);
50     }
51
52     @SuppressWarnings("unchecked")
53     public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
54         return Function.class.cast(BUILD_EFFECTIVE);
55     }
56
57     @SuppressWarnings("unchecked")
58     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
59             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
60         for (StmtContext<?, ?, ?> ctx : contexts) {
61             if (producesDeclared(ctx, declaredType)) {
62                 return (AT) ctx.getStatementArgument();
63             }
64         }
65         return null;
66     }
67
68     @SuppressWarnings("unchecked")
69     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
70             final Class<DT> declaredType) {
71         return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
72     }
73
74     @SuppressWarnings("unchecked")
75     public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
76             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
77         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
78             if (producesDeclared(subStmtContext,declaredType)) {
79                 return (StmtContext<AT, ?, ?>) subStmtContext;
80             }
81         }
82         return null;
83     }
84
85     @SuppressWarnings("unchecked")
86     public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllDeclaredSubstatements(
87             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
88         ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
89         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
90             if (producesDeclared(subStmtContext, declaredType)) {
91                 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
92             }
93         }
94         return listBuilder.build();
95     }
96
97     @SuppressWarnings("unchecked")
98     public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllEffectiveSubstatements(
99             final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
100         ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
101         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
102             if (producesDeclared(subStmtContext, type)) {
103                 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
104             }
105         }
106         return listBuilder.build();
107     }
108
109     public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllSubstatements(
110             final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
111         ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
112         listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type));
113         listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type));
114         return listBuilder.build();
115     }
116
117     @SuppressWarnings("unchecked")
118     public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
119             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
120         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
121             if (producesDeclared(subStmtContext,declaredType)) {
122                 return (StmtContext<AT, ?, ?>) subStmtContext;
123             }
124         }
125         return null;
126     }
127
128     public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
129             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
130         StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
131         return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
132     }
133
134     @SafeVarargs
135     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
136             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
137         if (startIndex >= types.length) {
138             return null;
139         }
140
141         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
142             if (producesDeclared(subStmtContext,types[startIndex])) {
143                 return startIndex + 1 == types.length ? subStmtContext
144                         : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
145             }
146         }
147         return null;
148     }
149
150     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
151             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
152         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
153             if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
154                 return subStmtContext;
155             }
156             if (sublevel > 1) {
157                 final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
158                     subStmtContext, declaredType, --sublevel);
159                 if (result != null) {
160                     return result;
161                 }
162             }
163         }
164
165         return null;
166     }
167
168     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
169             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
170         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
171             if (producesDeclared(subStmtContext, declaredType)) {
172                 return subStmtContext;
173             }
174
175             final StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
176             if (result != null) {
177                 return result;
178             }
179         }
180
181         return null;
182     }
183
184     public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
185             final Class<? extends DeclaredStatement<?>> type) {
186         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
187     }
188
189     public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
190         StmtContext<?,?,?> current = stmtCtx;
191         while (!current.getParentContext().isRootContext()) {
192             current = current.getParentContext();
193             if (producesDeclared(current, UnknownStatementImpl.class)) {
194                 return true;
195             }
196         }
197
198         return false;
199     }
200
201     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
202         return producesDeclared(stmtCtx, UnknownStatementImpl.class);
203     }
204
205     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
206             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
207             final QNameModule newQNameModule) {
208
209         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
210         boolean replaced = false;
211         for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
212             final QName qname = arg.getLastComponent();
213             if (!newQNameModule.equals(qname)) {
214                 final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
215                     QName.create(newQNameModule, qname.getLocalName()));
216                 builder.add(SchemaNodeIdentifier.create(false, newQname));
217                 replaced = true;
218             } else {
219                 builder.add(arg);
220             }
221         }
222
223         // This makes sure we reuse the collection when a grouping is instantiated in the same module
224         return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
225     }
226 }