Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[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 org.opendaylight.yangtools.yang.common.QName;
11 import com.google.common.base.Splitter;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
16 import java.util.HashSet;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
19 import com.google.common.base.Predicate;
20 import java.util.Collection;
21 import com.google.common.base.Function;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24
25 public final class StmtContextUtils {
26
27     public static final char LIST_KEY_SEPARATOR = ' ';
28     private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
29
30     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED = new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
31         @Override
32         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
33             return input.buildDeclared();
34         }
35     };
36
37     private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE = new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
38         @Override
39         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
40             return input.buildEffective();
41         }
42     };
43
44     public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate<StmtContext<?,?,?>>() {
45         @Override
46         public boolean apply(StmtContext<?, ?, ?> input) {
47             return input.isSupportedToBuildEffective();
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 final <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
62         return Function.class.cast(BUILD_EFFECTIVE);
63     }
64
65     @SuppressWarnings("unchecked")
66     public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
67             final Iterable<? extends StmtContext<?, ?, ?>> contexts,
68             final Class<DT> declaredType) {
69         for (StmtContext<?, ?, ?> ctx : contexts) {
70             if (producesDeclared(ctx, declaredType)) {
71                 return (AT) ctx.getStatementArgument();
72             }
73         }
74         return null;
75     }
76
77     @SuppressWarnings("unchecked")
78     public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
79             final StmtContext<?, ?, ?> ctx, final Class<DT> declaredType) {
80
81         if (producesDeclared(ctx, declaredType)) {
82             return (AT) ctx.getStatementArgument();
83         }
84
85         return null;
86     }
87
88     @SuppressWarnings("unchecked")
89     public static final <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
90             StmtContext<?, ?, ?> stmtContext, Class<DT> declaredType) {
91         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
92                 .declaredSubstatements();
93         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
94             if (producesDeclared(subStmtContext,declaredType)) {
95                 return (StmtContext<AT, ?, ?>) subStmtContext;
96             }
97         }
98         return null;
99     }
100
101     public static final StmtContext<?, ?, ?> findFirstDeclaredSubstatement(
102             final StmtContext<?, ?, ?> stmtContext, int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
103
104         if (startIndex >= types.length) {
105             return null;
106         }
107
108         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
109                 .declaredSubstatements();
110         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
111             if (producesDeclared(subStmtContext,types[startIndex])) {
112                 if (startIndex + 1 == types.length) {
113                     return subStmtContext;
114                 } else {
115                     return findFirstDeclaredSubstatement(subStmtContext,
116                             ++startIndex, types);
117                 }
118             }
119         }
120         return null;
121     }
122
123     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
124             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType,
125             int sublevel) {
126         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
127                 .declaredSubstatements();
128         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
129             if (sublevel == 1 && producesDeclared(subStmtContext,declaredType)) {
130                 return subStmtContext;
131             } else {
132                 if (sublevel > 1) {
133                     StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
134                             subStmtContext, declaredType, --sublevel);
135                     if (result != null) {
136                         return result;
137                     }
138                 }
139             }
140         }
141         return null;
142     }
143
144     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
145             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
146
147         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext
148                 .declaredSubstatements();
149
150         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
151             if (producesDeclared(subStmtContext,declaredType)) {
152                 return subStmtContext;
153             } else {
154                 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(
155                         subStmtContext, declaredType);
156                 if (result != null) {
157                     return result;
158                 }
159
160             }
161         }
162         return null;
163     }
164
165     public static final boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
166             final Class<? extends DeclaredStatement<?>> type) {
167         return type.isAssignableFrom(ctx.getPublicDefinition()
168                 .getDeclaredRepresentationClass());
169     }
170
171     public static boolean isInExtensionBody(
172             StmtContext<?,?,?> stmtCtx) {
173
174         StmtContext<?,?,?> current = stmtCtx;
175         while(!current.getParentContext().isRootContext()) {
176             current = current.getParentContext();
177             if(producesDeclared(current, UnknownStatementImpl.class)) {
178                 return true;
179             }
180         }
181
182         return false;
183     }
184
185     public static boolean isUnknownStatement(StmtContext<?, ?, ?> stmtCtx) {
186         if (producesDeclared(stmtCtx, UnknownStatementImpl.class)) {
187             return true;
188         } else {
189             return false;
190         }
191     }
192
193     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
194             QNameModule newQNameModule) {
195
196         List<String> keyTokens = KEY_SPLITTER.splitToList(keyStmtCtx.rawStatementArgument());
197
198         Set<SchemaNodeIdentifier> newKeys = new HashSet<>();
199
200         for (String keyToken : keyTokens) {
201             QName keyQName = QName.create(newQNameModule, keyToken);
202             SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier
203                     .create(false, keyQName);
204             newKeys.add(keyIdentifier);
205         }
206
207         return newKeys;
208     }
209 }