632fd9788934d116e095fc9cc5d33a03f3e8bce3
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / Utils.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.stmt.rfc6020;
9
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;
13
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;
27 import java.util.Set;
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;
69
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("/[^/].*");
80     @RegEx
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);
84
85     private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
86         @Override
87         protected XPathFactory initialValue() {
88             return XPathFactory.newInstance();
89         }
90     };
91
92     private Utils() {
93         throw new UnsupportedOperationException();
94     }
95
96     /**
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.
99      */
100     public static void detachFromCurrentThread() {
101         XPATH_FACTORY.remove();
102     }
103
104     public static Collection<SchemaNodeIdentifier.Relative> transformKeysStringToKeyNodes(final StmtContext<?, ?, ?> ctx,
105             final String value) {
106         final List<String> keyTokens = SPACE_SPLITTER.splitToList(value);
107
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);
112         }
113
114         final Set<SchemaNodeIdentifier.Relative> keyNodes = new HashSet<>();
115
116         for (final String keyToken : keyTokens) {
117
118             final SchemaNodeIdentifier.Relative keyNode = (Relative) SchemaNodeIdentifier.Relative.create(false,
119                     Utils.qNameFromArgument(ctx, keyToken));
120             keyNodes.add(keyNode);
121         }
122
123         return keyNodes;
124     }
125
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);
135         }
136         return ImmutableSet.copyOf(uniqueConstraintNodes);
137     }
138
139     private static String trimSingleLastSlashFromXPath(final String path) {
140         return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
141     }
142
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)));
147
148         final String trimmed = trimSingleLastSlashFromXPath(path);
149         try {
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);
159         }
160
161         return new RevisionAwareXPathImpl(path, PATH_ABS.matcher(path).matches());
162     }
163
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());
173             }
174
175             matcher.appendTail(result);
176             return result.toString();
177         }
178
179         return path;
180     }
181
182     public static QName trimPrefix(final QName identifier) {
183         final String prefixedLocalName = identifier.getLocalName();
184         final String[] namesParts = prefixedLocalName.split(":");
185
186         if (namesParts.length == 2) {
187             final String localName = namesParts[1];
188             return QName.create(identifier.getModule(), localName);
189         }
190
191         return identifier;
192     }
193
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);
198         }
199         return identifier;
200     }
201
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))) {
206             try {
207                 final QName qName = Utils.qNameFromArgument(ctx, nodeName);
208                 qNames.add(qName);
209             } catch (final RuntimeException e) {
210                 throw new SourceException(ctx.getStatementSourceReference(), e,
211                         "Failed to parse node '%s' in path '%s'", nodeName, path);
212             }
213         }
214
215         return SchemaNodeIdentifier.create(qNames, PATH_ABS.matcher(path).matches());
216     }
217
218     public static String stringFromStringContext(final YangStatementParser.ArgumentContext context,
219             final StatementSourceReference ref) {
220         return stringFromStringContext(context, YangVersion.VERSION_1, ref);
221     }
222
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());
229         }
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);
236                 /*
237                  * Unescape escaped double quotes, tabs, new line and backslash
238                  * in the inner string and trim the result.
239                  */
240                 checkDoubleQuotedString(innerStr, yangVersion, ref);
241                 sb.append(innerStr.replace("\\\"", "\"").replace("\\\\", "\\").replace("\\n", "\n")
242                         .replace("\\t", "\t"));
243             } else if (firstChar == '\'' && lastChar == '\'') {
244                 /*
245                  * According to RFC6020 a single quote character cannot occur in
246                  * a single-quoted string, even when preceded by a backslash.
247                  */
248                 sb.append(str.substring(1, str.length() - 1));
249             } else {
250                 checkUnquotedString(str, yangVersion, ref);
251                 sb.append(str);
252             }
253         }
254         return sb.toString();
255     }
256
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)) {
262                 case '"':
263                 case '\'':
264                     throw new SourceException(ref, "Yang 1.1: unquoted string (%s) contains illegal characters", str);
265                 }
266             }
267         }
268     }
269
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)) {
276                     case 'n':
277                     case 't':
278                     case '\\':
279                     case '\"':
280                         i++;
281                         break;
282                     default:
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,
286                                 str.charAt(i + 1));
287                     }
288                 }
289             }
290         }
291     }
292
293     public static QName qNameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
294         if (Strings.isNullOrEmpty(value)) {
295             return ctx.getPublicDefinition().getStatementName();
296         }
297
298         String prefix;
299         QNameModule qNameModule = null;
300         String localName = null;
301
302         final String[] namesParts = value.split(":");
303         switch (namesParts.length) {
304         case 1:
305             localName = namesParts[0];
306             qNameModule = getRootModuleQName(ctx);
307             break;
308         default:
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)) {
316                 localName = value;
317                 qNameModule = getRootModuleQName(ctx);
318             }
319             if (qNameModule == null
320                     && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
321                 ctx = ctx.getOriginalCtx();
322                 qNameModule = getModuleQNameByPrefix(ctx, prefix);
323             }
324             break;
325         }
326
327         qNameModule = InferenceException.throwIfNull(qNameModule, ctx.getStatementSourceReference(),
328             "Cannot resolve QNameModule for '%s'", value);
329
330         final QNameModule resultQNameModule;
331         if (qNameModule.getRevision() == null) {
332             resultQNameModule = QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV)
333                 .intern();
334         } else {
335             resultQNameModule = qNameModule;
336         }
337
338         return ctx.getFromNamespace(QNameCacheNamespace.class, QName.create(resultQNameModule, localName));
339     }
340
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);
344
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);
348         }
349         return qNameModule;
350     }
351
352     public static QNameModule getRootModuleQName(final StmtContext<?, ?, ?> ctx) {
353         if (ctx == null) {
354             return null;
355         }
356
357         final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
358         final QNameModule qNameModule;
359
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);
366         } else {
367             qNameModule = null;
368         }
369
370         Preconditions.checkArgument(qNameModule != null, "Failed to look up root QNameModule for %s", ctx);
371         if (qNameModule.getRevision() != null) {
372             return qNameModule;
373         }
374
375         return QNameModule.create(qNameModule.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV).intern();
376     }
377
378     @Nullable
379     public static StatementContextBase<?, ?, ?> findNode(final StmtContext<?, ?, ?> rootStmtCtx,
380             final SchemaNodeIdentifier node) {
381         return (StatementContextBase<?, ?, ?>) rootStmtCtx.getFromNamespace(SchemaNodeIdentifierBuildNamespace.class, node);
382     }
383
384     public static boolean isUnknownNode(final StmtContext<?, ?, ?> stmtCtx) {
385         return stmtCtx != null && stmtCtx.getPublicDefinition().getDeclaredRepresentationClass()
386                 .isAssignableFrom(UnknownStatementImpl.class);
387     }
388
389     static String internBoolean(final String input) {
390         if ("true".equals(input)) {
391             return "true";
392         } else if ("false".equals(input)) {
393             return "false";
394         } else {
395             return input;
396         }
397     }
398
399     public static Status parseStatus(final String value) {
400         switch (value) {
401             case "current":
402                 return Status.CURRENT;
403             case "deprecated":
404                 return Status.DEPRECATED;
405             case "obsolete":
406                 return Status.OBSOLETE;
407             default:
408                 LOG.warn("Invalid 'status' statement: {}", value);
409                 return null;
410         }
411     }
412
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
417                     .class)) {
418                 if (revision == null && subStmt.getStatementArgument() != null) {
419                     revision = (Date) subStmt.getStatementArgument();
420                 } else if (subStmt.getStatementArgument() != null && ((Date) subStmt.getStatementArgument()).compareTo
421                         (revision) > 0) {
422                     revision = (Date) subStmt.getStatementArgument();
423                 }
424             }
425         }
426         return revision;
427     }
428
429     /**
430      * Replaces illegal characters of QName by the name of the character (e.g.
431      * '?' is replaced by "QuestionMark" etc.).
432      *
433      * @param string
434      *            input String
435      * @return result String
436      */
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");
442
443         return string;
444     }
445
446     public static boolean belongsToTheSameModule(final QName targetStmtQName, final QName sourceStmtQName) {
447         return targetStmtQName.getModule().equals(sourceStmtQName.getModule());
448     }
449
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());
456         }
457
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);
463     }
464 }