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