eccbaca07d4bc14093b6a60b8b17f76e9c4280a5
[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.List;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
25
26 public final class StmtContextUtils {
27
28     public static final char LIST_KEY_SEPARATOR = ' ';
29     private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
30
31     private static final Function<StmtContext<?, ?, ?>, DeclaredStatement<?>> BUILD_DECLARED = new Function<StmtContext<?, ?, ?>, DeclaredStatement<?>>() {
32         @Override
33         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
34             return input.buildDeclared();
35         }
36     };
37
38     private static final Function<StmtContext<?, ?, ?>, EffectiveStatement<?, ?>> BUILD_EFFECTIVE = 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 = new Predicate<StmtContext<?, ?, ?>>() {
46         @Override
47         public boolean apply(StmtContext<?, ?, ?> input) {
48             return input.isSupportedToBuildEffective();
49         }
50     };
51
52     private StmtContextUtils() {
53         throw new UnsupportedOperationException("Utility class");
54     }
55
56     @SuppressWarnings("unchecked")
57     public static <D extends DeclaredStatement<?>> Function<StmtContext<?, ? extends D, ?>, D> buildDeclared() {
58         return Function.class.cast(BUILD_DECLARED);
59     }
60
61     @SuppressWarnings("unchecked")
62     public static final <E extends EffectiveStatement<?, ?>> Function<StmtContext<?, ?, ? extends E>, E> buildEffective() {
63         return Function.class.cast(BUILD_EFFECTIVE);
64     }
65
66     @SuppressWarnings("unchecked")
67     public static final <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
68             final Iterable<? extends StmtContext<?, ?, ?>> contexts, 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(final StmtContext<?, ?, ?> ctx,
79             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.declaredSubstatements();
92         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
93             if (producesDeclared(subStmtContext, declaredType)) {
94                 return (StmtContext<AT, ?, ?>) subStmtContext;
95             }
96         }
97         return null;
98     }
99
100     public static final StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
101             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
102
103         if (startIndex >= types.length) {
104             return null;
105         }
106
107         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
108         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
109             if (producesDeclared(subStmtContext, types[startIndex])) {
110                 if (startIndex + 1 == types.length) {
111                     return subStmtContext;
112                 } else {
113                     return findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
114                 }
115             }
116         }
117         return null;
118     }
119
120     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
121             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
122         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
123         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
124             if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
125                 return subStmtContext;
126             } else {
127                 if (sublevel > 1) {
128                     StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, declaredType,
129                             --sublevel);
130                     if (result != null) {
131                         return result;
132                     }
133                 }
134             }
135         }
136         return null;
137     }
138
139     public static final <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
140             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
141
142         Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements = stmtContext.declaredSubstatements();
143
144         for (StmtContext<?, ?, ?> subStmtContext : declaredSubstatements) {
145             if (producesDeclared(subStmtContext, declaredType)) {
146                 return subStmtContext;
147             } else {
148                 StmtContext<?, ?, ?> result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType);
149                 if (result != null) {
150                     return result;
151                 }
152
153             }
154         }
155         return null;
156     }
157
158     public static final boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
159             final Class<? extends DeclaredStatement<?>> type) {
160         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
161     }
162
163     public static boolean isInExtensionBody(StmtContext<?, ?, ?> stmtCtx) {
164
165         StmtContext<?, ?, ?> current = stmtCtx;
166         while (!current.getParentContext().isRootContext()) {
167             current = current.getParentContext();
168             if (producesDeclared(current, UnknownStatementImpl.class)) {
169                 return true;
170             }
171         }
172
173         return false;
174     }
175
176     public static boolean isUnknownStatement(StmtContext<?, ?, ?> stmtCtx) {
177         return producesDeclared(stmtCtx, UnknownStatementImpl.class);
178     }
179
180     public static Set<TypeOfCopy> getCopyTypesFromOriginal(StmtContext<?, ?, ?> ctx) {
181
182         Set<TypeOfCopy> copyTypesFromOriginal = new HashSet<>();
183         StmtContext<?, ?, ?> current = ctx;
184
185         while (current.getOriginalCtx() != null) {
186             copyTypesFromOriginal.add(current.getTypeOfCopy());
187             current = current.getOriginalCtx();
188         }
189
190         return copyTypesFromOriginal;
191     }
192
193     public static Collection<SchemaNodeIdentifier> replaceModuleQNameForKey(
194             StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx, 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.create(false, keyQName);
203             newKeys.add(keyIdentifier);
204         }
205
206         return newKeys;
207     }
208 }