2 * Copyright (c) 2013 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/eplv10.html
8 package org.opendaylight.controller.yang.validator;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.antlr.v4.runtime.tree.ParseTree;
17 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_stmtContext;
18 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Submodule_stmtContext;
19 import org.opendaylight.controller.yang.parser.util.ParserListenerUtils;
20 import org.opendaylight.controller.yang.parser.util.YangValidationException;
23 * Validation utilities
25 final class ValidationUtil {
27 static void ex(String message) {
28 throw new YangValidationException(message);
31 static Set<String> getDuplicates(Collection<String> keyList) {
32 Set<String> all = new HashSet<String>();
33 Set<String> duplicates = new HashSet<String>();
35 for (String key : keyList) {
42 static List<String> listKeysFromId(String keys) {
43 return Arrays.asList(keys.split(" "));
46 static String getRootParentName(ParseTree ctx) {
47 ParseTree root = getRootParent(ctx);
48 return ValidationUtil.getName(root);
51 private static ParseTree getRootParent(ParseTree ctx) {
53 while (root.getParent() != null) {
54 if (root.getClass().equals(Module_stmtContext.class)
55 || root.getClass().equals(Submodule_stmtContext.class))
57 root = root.getParent();
62 static String getName(ParseTree child) {
63 return ParserListenerUtils.stringFromNode(child);
66 static String f(String base, Object... args) {
67 return String.format(base, args);
71 * Get simple name from statement class e.g. Module from Module_stmt_context
73 static String getSimpleStatementName(
74 Class<? extends ParseTree> typeOfStatement) {
76 String className = typeOfStatement.getSimpleName();
77 int lastIndexOf = className.indexOf('$');
78 className = lastIndexOf == -1 ? className : className
79 .substring(lastIndexOf + 1);
80 int indexOfStmt = className.indexOf("_stmt");
81 int index = indexOfStmt == -1 ? className.indexOf("_arg") : indexOfStmt;
82 return className.substring(0, index).replace('_', '-');
85 static int countPresentChildrenOfType(ParseTree parent,
86 Set<Class<? extends ParseTree>> expectedChildTypes) {
87 int foundChildrenOfType = 0;
89 for (Class<? extends ParseTree> type : expectedChildTypes) {
90 foundChildrenOfType += countPresentChildrenOfType(parent, type);
92 return foundChildrenOfType;
95 static int countPresentChildrenOfType(ParseTree parent,
96 Class<? extends ParseTree> expectedChildType) {
97 int foundChildrenOfType = 0;
99 for (int i = 0; i < parent.getChildCount(); i++) {
100 ParseTree child = parent.getChild(i);
101 if (expectedChildType.isInstance(child))
102 foundChildrenOfType++;
104 return foundChildrenOfType;