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 | |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 20 | import org.onosproject.yangutils.datamodel.YangList; |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 26 | import org.onosproject.yangutils.utils.YangConstructType; |
| 27 | import static org.onosproject.yangutils.utils.YangConstructType.getYangConstructType; |
| 28 | |
| 29 | import java.util.List; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 32 | * It's a utility to carry out listener validation. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 33 | */ |
| 34 | public final class ListenerValidation { |
| 35 | |
| 36 | /** |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 37 | * Creates a new listener validation. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 38 | */ |
| 39 | private ListenerValidation() { |
| 40 | } |
| 41 | |
| 42 | /** |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 43 | * Checks parsed data stack is not empty. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 44 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 45 | * @param listener Listener's object |
| 46 | * @param errorType error type needs to be set in error message |
| 47 | * @param yangConstructType type of parsable data in which error occurred |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 48 | * @param parsableDataTypeName name of parsable data type in which error |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 49 | * occurred |
| 50 | * @param errorLocation location where error occurred |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 51 | */ |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 52 | public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 53 | YangConstructType yangConstructType, String parsableDataTypeName, |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 54 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 55 | if (listener.getParsedDataStack().empty()) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 56 | /* |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 57 | * If stack is empty it indicates error condition, value of |
| 58 | * parsableDataTypeName will be null in case there is no name |
| 59 | * attached to parsable data type. |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 60 | */ |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 61 | String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName, |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 62 | errorLocation); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 63 | throw new ParserException(message); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 64 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Checks parsed data stack is empty. |
| 69 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 70 | * @param listener Listener's object |
| 71 | * @param errorType error type needs to be set in error message |
| 72 | * @param yangConstructType type of parsable data in which error occurred |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 73 | * @param parsableDataTypeName name of parsable data type in which error |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 74 | * occurred |
| 75 | * @param errorLocation location where error occurred |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 76 | */ |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 77 | public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 78 | YangConstructType yangConstructType, String parsableDataTypeName, |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 79 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 80 | |
| 81 | if (!listener.getParsedDataStack().empty()) { |
| 82 | /* |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 83 | * If stack is empty it indicates error condition, value of |
| 84 | * parsableDataTypeName will be null in case there is no name |
| 85 | * attached to parsable data type. |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 86 | */ |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 87 | String message = constructListenerErrorMessage(errorType, yangConstructType, parsableDataTypeName, |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 88 | errorLocation); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 89 | throw new ParserException(message); |
| 90 | } |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 91 | } |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Returns parent node config value, if top node does not specify a config statement |
| 95 | * then default value true is returned. |
| 96 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 97 | * @param listener listener's object |
| 98 | * @return true/false parent's config value |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 99 | */ |
| 100 | public static boolean getParentNodeConfig(TreeWalkListener listener) { |
| 101 | YangNode parentNode; |
| 102 | Parsable curData = listener.getParsedDataStack().peek(); |
| 103 | if (curData instanceof YangNode) { |
| 104 | parentNode = ((YangNode) curData).getParent(); |
| 105 | if (parentNode instanceof YangContainer) { |
| 106 | return ((YangContainer) parentNode).isConfig(); |
| 107 | } else if (parentNode instanceof YangList) { |
| 108 | return ((YangList) parentNode).isConfig(); |
| 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame^] | 113 | |
| 114 | /** |
| 115 | * Checks if a rule occurrences is as per the expected YANG grammar's |
| 116 | * cardinality. |
| 117 | * |
| 118 | * @param childContext child's context |
| 119 | * @param yangChildConstruct child construct for whom cardinality is to be |
| 120 | * validated |
| 121 | * @param yangParentConstruct parent construct |
| 122 | * @param parentName parent name |
| 123 | * @throws ParserException exception if cardinality check fails |
| 124 | */ |
| 125 | public static void validateCardinality(List<?> childContext, YangConstructType yangChildConstruct, |
| 126 | YangConstructType yangParentConstruct, String parentName) |
| 127 | throws ParserException { |
| 128 | |
| 129 | if (!childContext.isEmpty() && childContext.size() != 1) { |
| 130 | ParserException parserException = new ParserException("YANG file error: Invalid cardinality of " |
| 131 | + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct) |
| 132 | + " \"" + parentName + "\"."); |
| 133 | throw parserException; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Checks if a rule occurrences is exactly 1. |
| 139 | * |
| 140 | * @param childContext child's context |
| 141 | * @param yangChildConstruct child construct for whom cardinality is to be |
| 142 | * validated |
| 143 | * @param yangParentConstruct parent construct |
| 144 | * @param parentName parent name |
| 145 | * @throws ParserException exception if cardinality check fails |
| 146 | */ |
| 147 | public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct, |
| 148 | YangConstructType yangParentConstruct, String parentName) |
| 149 | throws ParserException { |
| 150 | |
| 151 | if (childContext.isEmpty() || childContext.size() != 1) { |
| 152 | ParserException parserException = new ParserException("YANG file error: Invalid cardinality of " |
| 153 | + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct) |
| 154 | + " \"" + parentName + "\"."); |
| 155 | throw parserException; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Checks if a rule occurrences is minimum 1. |
| 161 | * |
| 162 | * @param childContext child's context |
| 163 | * @param yangChildConstruct child construct for whom cardinality is to be |
| 164 | * validated |
| 165 | * @param yangParentConstruct parent construct |
| 166 | * @param parentName parent name |
| 167 | * @throws ParserException exception if cardinality check fails |
| 168 | */ |
| 169 | public static void validateCardinalityNonNull(List<?> childContext, YangConstructType yangChildConstruct, |
| 170 | YangConstructType yangParentConstruct, String parentName) |
| 171 | throws ParserException { |
| 172 | |
| 173 | if (childContext.isEmpty()) { |
| 174 | ParserException parserException = new ParserException("YANG file error: Invalid cardinality of " |
| 175 | + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct) |
| 176 | + " \"" + parentName + "\"."); |
| 177 | throw parserException; |
| 178 | } |
| 179 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 180 | } |