A race condition occurs between ARPHandler and HostTracker if the ARP
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / validator / ValidationUtil.java
1 /*
2  * Copyright (c) 2013 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/eplv10.html
7  */
8 package org.opendaylight.controller.yang.validator;
9
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
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;
21
22 /**
23  * Validation utilities
24  */
25 final class ValidationUtil {
26
27     static void ex(String message) {
28         throw new YangValidationException(message);
29     }
30
31     static Set<String> getDuplicates(Collection<String> keyList) {
32         Set<String> all = new HashSet<String>();
33         Set<String> duplicates = new HashSet<String>();
34
35         for (String key : keyList) {
36             if (!all.add(key))
37                 duplicates.add(key);
38         }
39         return duplicates;
40     }
41
42     static List<String> listKeysFromId(String keys) {
43         return Arrays.asList(keys.split(" "));
44     }
45
46     static String getRootParentName(ParseTree ctx) {
47         ParseTree root = getRootParent(ctx);
48         return ValidationUtil.getName(root);
49     }
50
51     private static ParseTree getRootParent(ParseTree ctx) {
52         ParseTree root = ctx;
53         while (root.getParent() != null) {
54             if (root.getClass().equals(Module_stmtContext.class)
55                     || root.getClass().equals(Submodule_stmtContext.class))
56                 break;
57             root = root.getParent();
58         }
59         return root;
60     }
61
62     static String getName(ParseTree child) {
63         return ParserListenerUtils.stringFromNode(child);
64     }
65
66     static String f(String base, Object... args) {
67         return String.format(base, args);
68     }
69
70     /**
71      * Get simple name from statement class e.g. Module from Module_stmt_context
72      */
73     static String getSimpleStatementName(
74             Class<? extends ParseTree> typeOfStatement) {
75
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('_', '-');
83     }
84
85     static int countPresentChildrenOfType(ParseTree parent,
86             Set<Class<? extends ParseTree>> expectedChildTypes) {
87         int foundChildrenOfType = 0;
88
89         for (Class<? extends ParseTree> type : expectedChildTypes) {
90             foundChildrenOfType += countPresentChildrenOfType(parent, type);
91         }
92         return foundChildrenOfType;
93     }
94
95     static int countPresentChildrenOfType(ParseTree parent,
96             Class<? extends ParseTree> expectedChildType) {
97         int foundChildrenOfType = 0;
98
99         for (int i = 0; i < parent.getChildCount(); i++) {
100             ParseTree child = parent.getChild(i);
101             if (expectedChildType.isInstance(child))
102                 foundChildrenOfType++;
103         }
104         return foundChildrenOfType;
105     }
106
107 }