9bc3fcbf8a3d2231026a9a4989d631e74a682f43
[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.Predicate;
12 import com.google.common.base.Splitter;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Set;
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
26     public static final char LIST_KEY_SEPARATOR = ' ';
27     private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
28
29     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
30             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 =
38             new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
39         @Override
40         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
41             return input.buildEffective();
42         }
43     };
44
45     public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
46             new Predicate<StmtContext<?,?,?>>() {
47         @Override
48         public boolean apply(final StmtContext<?, ?, ?> input) {
49             return input.isSupportedToBuildEffective();
50         }
51     };
52
53     private StmtContextUtils() {
54         throw new UnsupportedOperationException("Utility class");
55     }
56
57     @SuppressWarnings("unchecked")
58     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
59         return Function.class.cast(BUILD_DECLARED);
60     }
61
62     @SuppressWarnings("unchecked")
63     public static <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
64         return Function.class.cast(BUILD_EFFECTIVE);
65     }
66
67     @SuppressWarnings("unchecked")
68     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
69             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
70         for (StmtContext<?, ?, ?> ctx : contexts) {
71             if (producesDeclared(ctx, declaredType)) {
72                 return (AT) ctx.getStatementArgument();
73             }
74         }
75         return null;
76     }
77
78     @SuppressWarnings("unchecked")
79     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(final StmtContext<?, ?, ?> ctx,
80             final Class<DT> declaredType) {
81         return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
82     }
83
84     @SuppressWarnings("unchecked")
85     public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
86             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
87         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
88             if (producesDeclared(subStmtContext,declaredType)) {
89                 return (StmtContext<AT, ?, ?>) subStmtContext;
90             }
91         }
92         return null;
93     }
94
95     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
96             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
97         if (startIndex >= types.length) {
98             return null;
99         }
100
101         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
102         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
103             if (producesDeclared(subStmtContext,types[startIndex])) {
104                 if (startIndex + 1 == types.length) {
105                     return subStmtContext;
106                 } else {
107                     return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
108                 }
109             }
110         }
111         return null;
112     }
113
114     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
115             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
116         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
117             if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
118                 return subStmtContext;
119             }
120             if (sublevel > 1) {
121                 StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
122                     subStmtContext, declaredType, --sublevel);
123                 if (result != null) {
124                     return result;
125                 }
126             }
127         }
128
129         return null;
130     }
131
132     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
133             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
134         for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
135             if (producesDeclared(subStmtContext, declaredType)) {
136                 return subStmtContext;
137             }
138
139             StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
140             if (result != null) {
141                 return result;
142             }
143         }
144
145         return null;
146     }
147
148     public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
149             final Class<? extends DeclaredStatement<?>> type) {
150         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
151     }
152
153     public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
154         StmtContext<?,?,?> current = stmtCtx;
155         while(!current.getParentContext().isRootContext()) {
156             current = current.getParentContext();
157             if (producesDeclared(current, UnknownStatementImpl.class)) {
158                 return true;
159             }
160         }
161
162         return false;
163     }
164
165     public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
166         return producesDeclared(stmtCtx, UnknownStatementImpl.class);
167     }
168
169     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
170             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
171             final QNameModule newQNameModule) {
172
173         Set<SchemaNodeIdentifier> newKeys = new HashSet<>();
174         for (String keyToken : KEY_SPLITTER.split(keyStmtCtx.rawStatementArgument())) {
175             QName keyQName = QName.create(newQNameModule, keyToken);
176             SchemaNodeIdentifier keyIdentifier = SchemaNodeIdentifier.create(false, keyQName);
177             newKeys.add(keyIdentifier);
178         }
179
180         return newKeys;
181     }
182 }