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