2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_NAMESPACE;
11 import static org.opendaylight.yangtools.yang.common.YangConstants.YANG_XPATH_FUNCTIONS_PREFIX;
12 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
14 import com.google.common.base.CharMatcher;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Splitter;
17 import com.google.common.base.Strings;
18 import com.google.common.collect.ImmutableBiMap;
19 import com.google.common.collect.ImmutableSet;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Optional;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import javax.annotation.Nullable;
31 import javax.annotation.RegEx;
32 import javax.xml.xpath.XPath;
33 import javax.xml.xpath.XPathExpressionException;
34 import javax.xml.xpath.XPathFactory;
35 import org.antlr.v4.runtime.tree.TerminalNode;
36 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.QNameModule;
39 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
40 import org.opendaylight.yangtools.yang.common.YangVersion;
41 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
42 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
43 import org.opendaylight.yangtools.yang.model.api.Status;
44 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
48 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
49 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
50 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
51 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
52 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
56 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
57 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
58 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
59 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
60 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
61 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName;
62 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
63 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
64 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
65 import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
66 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
70 public final class Utils {
71 private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
72 private static final CharMatcher LEFT_PARENTHESIS_MATCHER = CharMatcher.is('(');
73 private static final CharMatcher RIGHT_PARENTHESIS_MATCHER = CharMatcher.is(')');
74 private static final CharMatcher AMPERSAND_MATCHER = CharMatcher.is('&');
75 private static final CharMatcher QUESTION_MARK_MATCHER = CharMatcher.is('?');
76 private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings().trimResults();
77 private static final Splitter SPACE_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
78 private static final Splitter COLON_SPLITTER = Splitter.on(":").omitEmptyStrings().trimResults();
79 private static final Pattern PATH_ABS = Pattern.compile("/[^/].*");
81 private static final String YANG_XPATH_FUNCTIONS_STRING =
82 "(re-match|deref|derived-from(-or-self)?|enum-value|bit-is-set)(\\()";
83 private static final Pattern YANG_XPATH_FUNCTIONS_PATTERN = Pattern.compile(YANG_XPATH_FUNCTIONS_STRING);
85 private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
87 protected XPathFactory initialValue() {
88 return XPathFactory.newInstance();
93 throw new UnsupportedOperationException();
97 * Cleanup any resources attached to the current thread. Threads interacting with this class can cause thread-local
98 * caches to them. Invoke this method if you want to detach those resources.
100 public static void detachFromCurrentThread() {
101 XPATH_FACTORY.remove();
104 public static Collection<SchemaNodeIdentifier.Relative> transformKeysStringToKeyNodes(final StmtContext<?, ?, ?> ctx,
105 final String value) {
106 final List<String> keyTokens = SPACE_SPLITTER.splitToList(value);
108 // to detect if key contains duplicates
109 if (new HashSet<>(keyTokens).size() < keyTokens.size()) {
110 // FIXME: report all duplicate keys
111 throw new SourceException(ctx.getStatementSourceReference(), "Duplicate value in list key: %s", value);
114 final Set<SchemaNodeIdentifier.Relative> keyNodes = new HashSet<>();
116 for (final String keyToken : keyTokens) {
118 final SchemaNodeIdentifier.Relative keyNode = (Relative) SchemaNodeIdentifier.Relative.create(false,
119 Utils.qNameFromArgument(ctx, keyToken));
120 keyNodes.add(keyNode);
126 static Collection<SchemaNodeIdentifier.Relative> parseUniqueConstraintArgument(final StmtContext<?, ?, ?> ctx,
127 final String argumentValue) {
128 final Set<SchemaNodeIdentifier.Relative> uniqueConstraintNodes = new HashSet<>();
129 for (final String uniqueArgToken : SPACE_SPLITTER.split(argumentValue)) {
130 final SchemaNodeIdentifier nodeIdentifier = Utils.nodeIdentifierFromPath(ctx, uniqueArgToken);
131 SourceException.throwIf(nodeIdentifier.isAbsolute(), ctx.getStatementSourceReference(),
132 "Unique statement argument '%s' contains schema node identifier '%s' "
133 + "which is not in the descendant node identifier form.", argumentValue, uniqueArgToken);
134 uniqueConstraintNodes.add((SchemaNodeIdentifier.Relative) nodeIdentifier);
136 return ImmutableSet.copyOf(uniqueConstraintNodes);
139 private static String trimSingleLastSlashFromXPath(final String path) {
140 return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
143 static RevisionAwareXPath parseXPath(final StmtContext<?, ?, ?> ctx, final String path) {
144 final XPath xPath = XPATH_FACTORY.get().newXPath();
145 xPath.setNamespaceContext(StmtNamespaceContext.create(ctx,
146 ImmutableBiMap.of(RFC6020_YANG_NAMESPACE.toString(), YANG_XPATH_FUNCTIONS_PREFIX)));
148 final String trimmed = trimSingleLastSlashFromXPath(path);
150 // XPath extension functions have to be prefixed
151 // yang-specific XPath functions are in fact extended functions, therefore we have to add
152 // "yang" prefix to them so that they can be properly validated with the XPath.compile() method
153 // the "yang" prefix is bound to RFC6020 YANG namespace
154 final String prefixedXPath = addPrefixToYangXPathFunctions(trimmed, ctx);
155 // TODO: we could capture the result and expose its 'evaluate' method
156 xPath.compile(prefixedXPath);
157 } catch (final XPathExpressionException e) {
158 LOG.warn("Argument \"{}\" is not valid XPath string at \"{}\"", path, ctx.getStatementSourceReference(), e);
161 return new RevisionAwareXPathImpl(path, PATH_ABS.matcher(path).matches());
164 private static String addPrefixToYangXPathFunctions(final String path, final StmtContext<?, ?, ?> ctx) {
165 if (ctx.getRootVersion() == YangVersion.VERSION_1_1) {
166 // FIXME once Java 9 is available, change this to StringBuilder as Matcher.appendReplacement() and
167 // Matcher.appendTail() will accept StringBuilder parameter in Java 9
168 final StringBuffer result = new StringBuffer();
169 final String prefix = YANG_XPATH_FUNCTIONS_PREFIX + ":";
170 final Matcher matcher = YANG_XPATH_FUNCTIONS_PATTERN.matcher(path);
171 while (matcher.find()) {
172 matcher.appendReplacement(result, prefix + matcher.group());
175 matcher.appendTail(result);
176 return result.toString();
182 public static QName trimPrefix(final QName identifier) {
183 final String prefixedLocalName = identifier.getLocalName();
184 final String[] namesParts = prefixedLocalName.split(":");
186 if (namesParts.length == 2) {
187 final String localName = namesParts[1];
188 return QName.create(identifier.getModule(), localName);
194 public static String trimPrefix(final String identifier) {
195 final List<String> namesParts = COLON_SPLITTER.splitToList(identifier);
196 if (namesParts.size() == 2) {
197 return namesParts.get(1);
202 static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
203 // FIXME: is the path trimming really necessary??
204 final List<QName> qNames = new ArrayList<>();
205 for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
207 final QName qName = Utils.qNameFromArgument(ctx, nodeName);
209 } catch (final RuntimeException e) {
210 throw new SourceException(ctx.getStatementSourceReference(), e,
211 "Failed to parse node '%s' in path '%s'", nodeName, path);
215 return SchemaNodeIdentifier.create(qNames, PATH_ABS.matcher(path).matches());
218 public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
219 final StatementSourceReference ref) {
220 return stringFromStringContext(context, YangVersion.VERSION_1, ref);
223 public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
224 final YangVersion yangVersion, final StatementSourceReference ref) {
225 final StringBuilder sb = new StringBuilder();
226 List<TerminalNode> strings = context.STRING();
227 if (strings.isEmpty()) {
228 strings = Collections.singletonList(context.IDENTIFIER());
230 for (final TerminalNode stringNode : strings) {
231 final String str = stringNode.getText();
232 final char firstChar = str.charAt(0);
233 final char lastChar = str.charAt(str.length() - 1);
234 if (firstChar == '"' && lastChar == '"') {
235 final String innerStr = str.substring(1, str.length() - 1);
237 * Unescape escaped double quotes, tabs, new line and backslash
238 * in the inner string and trim the result.
240 checkDoubleQuotedString(innerStr, yangVersion, ref);
241 sb.append(innerStr.replace("\\\"", "\"").replace("\\\\", "\\").replace("\\n", "\n")
242 .replace("\\t", "\t"));
243 } else if (firstChar == '\'' && lastChar == '\'') {
245 * According to RFC6020 a single quote character cannot occur in
246 * a single-quoted string, even when preceded by a backslash.
248 sb.append(str.substring(1, str.length() - 1));
250 checkUnquotedString(str, yangVersion, ref);
254 return sb.toString();
257 private static void checkUnquotedString(final String str, final YangVersion yangVersion,
258 final StatementSourceReference ref) {
259 if (yangVersion == YangVersion.VERSION_1_1) {
260 for (int i = 0; i < str.length(); i++) {
261 switch (str.charAt(i)) {
264 throw new SourceException(ref, "Yang 1.1: unquoted string (%s) contains illegal characters", str);
270 private static void checkDoubleQuotedString(final String str, final YangVersion yangVersion,
271 final StatementSourceReference ref) {
272 if (yangVersion == YangVersion.VERSION_1_1) {
273 for (int i = 0; i < str.length() - 1; i++) {
274 if (str.charAt(i) == '\\') {
275 switch (str.charAt(i + 1)) {
283 throw new SourceException(ref,
284 "Yang 1.1: illegal double quoted string (%s). In double quoted string the backslash must be followed "
285 + "by one of the following character [n,t,\",\\], but was '%s'.", str,
293 public static QName qNameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
294 if (Strings.isNullOrEmpty(value)) {
295 return ctx.getPublicDefinition().getStatementName();
299 QNameModule qNameModule = null;
300 String localName = null;
302 final String[] namesParts = value.split(":");
303 switch (namesParts.length) {
305 localName = namesParts[0];
306 qNameModule = getRootModuleQName(ctx);
309 prefix = namesParts[0];
310 localName = namesParts[1];
311 qNameModule = getModuleQNameByPrefix(ctx, prefix);
312 // in case of unknown statement argument, we're not going to parse it
313 if (qNameModule == null
314 && ctx.getPublicDefinition().getDeclaredRepresentationClass()
315 .isAssignableFrom(UnknownStatementImpl.class)) {
317 qNameModule = getRootModuleQName(ctx);
319 if (qNameModule == null
320 && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
321 ctx = ctx.getOriginalCtx();
322 qNameModule = getModuleQNameByPrefix(ctx, prefix);
327 qNameModule = InferenceException.throwIfNull(qNameModule, ctx.getStatementSourceReference(),
328 "Cannot resolve QNameModule for '%s'", value);
330 final QNameModule resultQNameModule;
331 if (qNameModule.getRevision() == null) {
332 resultQNameModule = QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV)
335 resultQNameModule = qNameModule;
338 return ctx.getFromNamespace(QNameCacheNamespace.class, QName.create(resultQNameModule, localName));
341 public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
342 final ModuleIdentifier modId = ctx.getRoot().getFromNamespace(ImpPrefixToModuleIdentifier.class, prefix);
343 final QNameModule qNameModule = ctx.getFromNamespace(ModuleIdentifierToModuleQName.class, modId);
345 if (qNameModule == null && StmtContextUtils.producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) {
346 final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
347 return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
352 public static QNameModule getRootModuleQName(final StmtContext<?, ?, ?> ctx) {
357 final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
358 final QNameModule qNameModule;
360 if (StmtContextUtils.producesDeclared(rootCtx, ModuleStatement.class)) {
361 qNameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx);
362 } else if (StmtContextUtils.producesDeclared(rootCtx, SubmoduleStatement.class)) {
363 final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(),
364 BelongsToStatement.class);
365 qNameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
370 Preconditions.checkArgument(qNameModule != null, "Failed to look up root QNameModule for %s", ctx);
371 if (qNameModule.getRevision() != null) {
375 return QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV).intern();
379 public static StatementContextBase<?, ?, ?> findNode(final StmtContext<?, ?, ?> rootStmtCtx,
380 final SchemaNodeIdentifier node) {
381 return (StatementContextBase<?, ?, ?>) rootStmtCtx.getFromNamespace(SchemaNodeIdentifierBuildNamespace.class, node);
384 public static boolean isUnknownNode(final StmtContext<?, ?, ?> stmtCtx) {
385 return stmtCtx != null && stmtCtx.getPublicDefinition().getDeclaredRepresentationClass()
386 .isAssignableFrom(UnknownStatementImpl.class);
389 static String internBoolean(final String input) {
390 if ("true".equals(input)) {
392 } else if ("false".equals(input)) {
399 public static Status parseStatus(final String value) {
402 return Status.CURRENT;
404 return Status.DEPRECATED;
406 return Status.OBSOLETE;
408 LOG.warn("Invalid 'status' statement: {}", value);
413 public static Date getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
414 Date revision = null;
415 for (final StmtContext<?, ?, ?> subStmt : subStmts) {
416 if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(RevisionStatement
418 if (revision == null && subStmt.getStatementArgument() != null) {
419 revision = (Date) subStmt.getStatementArgument();
420 } else if (subStmt.getStatementArgument() != null && ((Date) subStmt.getStatementArgument()).compareTo
422 revision = (Date) subStmt.getStatementArgument();
430 * Replaces illegal characters of QName by the name of the character (e.g.
431 * '?' is replaced by "QuestionMark" etc.).
435 * @return result String
437 public static String replaceIllegalCharsForQName(String string) {
438 string = LEFT_PARENTHESIS_MATCHER.replaceFrom(string, "LeftParenthesis");
439 string = RIGHT_PARENTHESIS_MATCHER.replaceFrom(string, "RightParenthesis");
440 string = AMPERSAND_MATCHER.replaceFrom(string, "Ampersand");
441 string = QUESTION_MARK_MATCHER.replaceFrom(string, "QuestionMark");
446 public static boolean belongsToTheSameModule(final QName targetStmtQName, final QName sourceStmtQName) {
447 return targetStmtQName.getModule().equals(sourceStmtQName.getModule());
450 public static SourceIdentifier createSourceIdentifier(final RootStatementContext<?, ?, ?> root) {
451 final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
452 if (qNameModule != null) {
453 // creates SourceIdentifier for a module
454 return RevisionSourceIdentifier.create((String) root.getStatementArgument(),
455 qNameModule.getFormattedRevision());
458 // creates SourceIdentifier for a submodule
459 final Date revision = Optional.ofNullable(Utils.getLatestRevision(root.declaredSubstatements()))
460 .orElse(SimpleDateFormatUtil.DEFAULT_DATE_REV);
461 final String formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
462 return RevisionSourceIdentifier.create((String) root.getStatementArgument(), formattedRevision);