Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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.listeners; |
| 18 | |
| 19 | /* |
| 20 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 21 | * |
| 22 | * ABNF grammar as per RFC6020 |
| 23 | * value-stmt = value-keyword sep integer-value stmtend |
| 24 | * |
| 25 | * ANTLR grammar rule |
| 26 | * valueStatement : VALUE_KEYWORD ((MINUS INTEGER) | INTEGER) STMTEND; |
| 27 | */ |
| 28 | |
| 29 | import org.onosproject.yangutils.datamodel.YangEnum; |
| 30 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 31 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.utils.YangConstructType.VALUE_DATA; |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 34 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 35 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 41 | |
| 42 | /** |
| 43 | * Implements listener based call back function corresponding to the "value" |
| 44 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 45 | */ |
| 46 | public final class ValueListener { |
| 47 | |
| 48 | /** |
| 49 | * Creates a new value listener. |
| 50 | */ |
| 51 | private ValueListener() { |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * It is called when parser receives an input matching the grammar rule |
| 56 | * (value), perform validations and update the data model tree. |
| 57 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 58 | * @param listener Listener's object |
| 59 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 60 | */ |
| 61 | public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) { |
| 62 | |
| 63 | // Check for stack to be non empty. |
| 64 | checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY); |
| 65 | |
| 66 | // Obtain the node of the stack. |
| 67 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 68 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 69 | case ENUM_DATA: { |
| 70 | YangEnum enumNode = (YangEnum) tmpNode; |
| 71 | if (!isEnumValueValid(listener, ctx)) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 72 | ParserException parserException = new ParserException("Duplicate Value Entry"); |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 73 | parserException.setLine(ctx.INTEGER().getSymbol().getLine()); |
| 74 | parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine()); |
| 75 | throw parserException; |
| 76 | } |
| 77 | enumNode.setValue(Integer.valueOf(ctx.INTEGER().getText())); |
| 78 | break; |
| 79 | } |
| 80 | default: |
| 81 | throw new ParserException( |
| 82 | constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Validates ENUM value uniqueness. |
| 88 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 89 | * @param listener Listener's object |
| 90 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 91 | * @return validation result |
| 92 | */ |
| 93 | private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) { |
| 94 | Parsable enumNode = listener.getParsedDataStack().pop(); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 95 | |
| 96 | // Check for stack to be non empty. |
| 97 | checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY); |
| 98 | |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 99 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 100 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 101 | case ENUMERATION_DATA: { |
| 102 | YangEnumeration yangEnumeration = (YangEnumeration) tmpNode; |
| 103 | for (YangEnum curEnum : yangEnumeration.getEnumSet()) { |
| 104 | if (Integer.valueOf(ctx.INTEGER().getText()) == curEnum.getValue()) { |
| 105 | listener.getParsedDataStack().push(enumNode); |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | listener.getParsedDataStack().push(enumNode); |
| 110 | return true; |
| 111 | } |
| 112 | default: |
| 113 | listener.getParsedDataStack().push(enumNode); |
| 114 | throw new ParserException( |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 115 | constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY)); |
Gaurav Agrawal | 9c512e0 | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |