Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.parserutils; |
| 18 | |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 19 | import java.util.Iterator; |
| 20 | import java.util.List; |
| 21 | |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 22 | import org.antlr.v4.runtime.ParserRuleContext; |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 24 | import org.onosproject.yangutils.datamodel.YangList; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
| 26 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.utils.YangConstructType; |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.utils.YangConstructType.getYangConstructType; |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 35 | * Represents a utility to carry out listener validation. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 36 | */ |
| 37 | public final class ListenerValidation { |
| 38 | |
| 39 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 40 | * Creates a new listener validation. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 41 | */ |
| 42 | private ListenerValidation() { |
| 43 | } |
| 44 | |
| 45 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 46 | * Checks parsed data stack is not empty. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 47 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 48 | * @param listener Listener's object |
| 49 | * @param errorType error type needs to be set in error message |
| 50 | * @param yangConstructType type of parsable data in which error occurred |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 51 | * @param parsableDataTypeName name of parsable data type in which error |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 52 | * occurred |
| 53 | * @param errorLocation location where error occurred |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 54 | */ |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 55 | public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 56 | YangConstructType yangConstructType, String parsableDataTypeName, |
| 57 | ListenerErrorLocation errorLocation) { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 58 | |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 59 | if (listener.getParsedDataStack().empty()) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 60 | /* |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 61 | * If stack is empty it indicates error condition, value of |
| 62 | * parsableDataTypeName will be null in case there is no name |
| 63 | * attached to parsable data type. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 64 | */ |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 65 | String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 66 | errorLocation); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 67 | throw new ParserException(message); |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 68 | } |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks parsed data stack is empty. |
| 73 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 74 | * @param listener Listener's object |
| 75 | * @param errorType error type needs to be set in error message |
| 76 | * @param yangConstructType type of parsable data in which error occurred |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 77 | * @param parsableDataTypeName name of parsable data type in which error |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 78 | * occurred |
| 79 | * @param errorLocation location where error occurred |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 80 | */ |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 81 | public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 82 | YangConstructType yangConstructType, String parsableDataTypeName, |
| 83 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 84 | |
| 85 | if (!listener.getParsedDataStack().empty()) { |
| 86 | /* |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 87 | * If stack is empty it indicates error condition, value of |
| 88 | * parsableDataTypeName will be null in case there is no name |
| 89 | * attached to parsable data type. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 90 | */ |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 91 | String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 92 | errorLocation); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 93 | throw new ParserException(message); |
| 94 | } |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 95 | } |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 96 | |
| 97 | /** |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 98 | * Returns parent node config value, if top node does not specify a config |
| 99 | * statement then default value true is returned. |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 100 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 101 | * @param listener listener's object |
| 102 | * @return true/false parent's config value |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 103 | */ |
| 104 | public static boolean getParentNodeConfig(TreeWalkListener listener) { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 106 | YangNode parentNode; |
| 107 | Parsable curData = listener.getParsedDataStack().peek(); |
| 108 | if (curData instanceof YangNode) { |
| 109 | parentNode = ((YangNode) curData).getParent(); |
| 110 | if (parentNode instanceof YangContainer) { |
| 111 | return ((YangContainer) parentNode).isConfig(); |
| 112 | } else if (parentNode instanceof YangList) { |
| 113 | return ((YangList) parentNode).isConfig(); |
| 114 | } |
| 115 | } |
| 116 | return true; |
| 117 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Checks if a rule occurrences is as per the expected YANG grammar's |
| 121 | * cardinality. |
| 122 | * |
| 123 | * @param childContext child's context |
| 124 | * @param yangChildConstruct child construct for whom cardinality is to be |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 125 | * validated |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 126 | * @param yangParentConstruct parent construct |
| 127 | * @param parentName parent name |
| 128 | * @throws ParserException exception if cardinality check fails |
| 129 | */ |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 130 | public static void validateCardinalityMaxOne(List<?> childContext, YangConstructType yangChildConstruct, |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 131 | YangConstructType yangParentConstruct, String parentName) |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 132 | throws ParserException { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 133 | |
| 134 | if (!childContext.isEmpty() && childContext.size() != 1) { |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 135 | ParserException parserException = new ParserException("YANG file error: \"" |
| 136 | + getYangConstructType(yangChildConstruct) + "\" is defined more than once in \"" |
| 137 | + getYangConstructType(yangParentConstruct) + " " + parentName + "\"."); |
| 138 | |
| 139 | Iterator<?> context = childContext.iterator(); |
| 140 | parserException.setLine(((ParserRuleContext) context.next()).getStart().getLine()); |
| 141 | parserException.setCharPosition(((ParserRuleContext) context.next()).getStart().getCharPositionInLine()); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 142 | throw parserException; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Checks if a rule occurrences is exactly 1. |
| 148 | * |
| 149 | * @param childContext child's context |
| 150 | * @param yangChildConstruct child construct for whom cardinality is to be |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 151 | * validated |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 152 | * @param yangParentConstruct parent construct |
| 153 | * @param parentName parent name |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 154 | * @param parentContext parents's context |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 155 | * @throws ParserException exception if cardinality check fails |
| 156 | */ |
| 157 | public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct, |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 158 | YangConstructType yangParentConstruct, String parentName, |
| 159 | ParserRuleContext parentContext) |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 160 | throws ParserException { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 161 | |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 162 | if (childContext.isEmpty()) { |
| 163 | ParserException parserException = new ParserException("YANG file error: Missing \"" |
| 164 | + getYangConstructType(yangChildConstruct) + "\" in \"" + getYangConstructType(yangParentConstruct) |
| 165 | + " " + parentName + "\"."); |
| 166 | parserException.setLine(parentContext.getStart().getLine()); |
| 167 | parserException.setCharPosition(parentContext.getStart().getCharPositionInLine()); |
| 168 | throw parserException; |
| 169 | } else if (!childContext.isEmpty() && childContext.size() != 1) { |
| 170 | Iterator<?> childcontext = childContext.iterator(); |
| 171 | ParserException parserException = new ParserException("YANG file error: \"" |
| 172 | + getYangConstructType(yangChildConstruct) + "\" is present more than once in \"" |
| 173 | + getYangConstructType(yangParentConstruct) + " " + parentName + "\"."); |
| 174 | parserException.setLine(((ParserRuleContext) childcontext.next()).getStart().getLine()); |
| 175 | parserException.setCharPosition(((ParserRuleContext) childcontext.next()).getStart() |
| 176 | .getCharPositionInLine()); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 177 | throw parserException; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Checks if a rule occurrences is minimum 1. |
| 183 | * |
| 184 | * @param childContext child's context |
| 185 | * @param yangChildConstruct child construct for whom cardinality is to be |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 186 | * validated |
| 187 | * @param yangParentConstruct parent construct |
| 188 | * @param parentName parent name |
| 189 | * @param parentContext parents's context |
| 190 | * @throws ParserException exception if cardinality check fails |
| 191 | */ |
| 192 | public static void validateCardinalityNonZero(List<?> childContext, YangConstructType yangChildConstruct, |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 193 | YangConstructType yangParentConstruct, String parentName, |
| 194 | ParserRuleContext parentContext) |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 195 | throws ParserException { |
| 196 | |
| 197 | if (childContext.isEmpty()) { |
| 198 | ParserException parserException = new ParserException("YANG file error: Missing \"" |
| 199 | + getYangConstructType(yangChildConstruct) + "\" in \"" + getYangConstructType(yangParentConstruct) |
| 200 | + " " + parentName + "\"."); |
| 201 | |
| 202 | parserException.setLine(parentContext.getStart().getLine()); |
| 203 | parserException.setCharPosition(parentContext.getStart().getCharPositionInLine()); |
| 204 | throw parserException; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Checks if a either of one construct occurrence. |
| 210 | * |
| 211 | * @param child1Context first optional child's context |
| 212 | * @param yangChild1Construct first child construct for whom cardinality is |
| 213 | * to be validated |
| 214 | * @param child2Context second optional child's context |
| 215 | * @param yangChild2Construct second child construct for whom cardinality is |
| 216 | * to be validated |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 217 | * @param yangParentConstruct parent construct |
| 218 | * @param parentName parent name |
| 219 | * @throws ParserException exception if cardinality check fails |
| 220 | */ |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 221 | public static void validateMutuallyExclusiveChilds(List<?> child1Context, YangConstructType yangChild1Construct, |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 222 | List<?> child2Context, YangConstructType yangChild2Construct, |
| 223 | YangConstructType yangParentConstruct, String parentName) |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 224 | throws ParserException { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 225 | |
Gaurav Agrawal | 78f7240 | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 226 | if (!child1Context.isEmpty() && !child2Context.isEmpty()) { |
| 227 | ParserException parserException = new ParserException("YANG file error: \"" |
| 228 | + getYangConstructType(yangChild1Construct) + "\" & \"" + getYangConstructType(yangChild2Construct) |
| 229 | + "\" should be mutually exclusive in \"" + getYangConstructType(yangParentConstruct) + " " |
| 230 | + parentName + "\"."); |
| 231 | |
| 232 | parserException.setLine(((ParserRuleContext) child2Context).getStart().getLine()); |
| 233 | parserException.setCharPosition(((ParserRuleContext) child2Context).getStart().getCharPositionInLine()); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 234 | throw parserException; |
| 235 | } |
| 236 | } |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 237 | } |