Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +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.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; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 34 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 35 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.VALUE_DATA; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIntegerValue; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 43 | |
| 44 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 45 | * Represents listener based call back function corresponding to the "value" |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 46 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 47 | */ |
| 48 | public final class ValueListener { |
| 49 | |
| 50 | /** |
| 51 | * Creates a new value listener. |
| 52 | */ |
| 53 | private ValueListener() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * It is called when parser receives an input matching the grammar rule |
| 58 | * (value), perform validations and update the data model tree. |
| 59 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 60 | * @param listener Listener's object |
| 61 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 62 | */ |
| 63 | public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) { |
| 64 | |
| 65 | // Check for stack to be non empty. |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 66 | checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY); |
| 67 | |
| 68 | // Validate value |
| 69 | int value = getValidIntegerValue(ctx.value().getText(), VALUE_DATA, ctx); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 70 | |
| 71 | // Obtain the node of the stack. |
| 72 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 73 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 74 | case ENUM_DATA: { |
| 75 | YangEnum enumNode = (YangEnum) tmpNode; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 76 | if (!isEnumValueValid(listener, ctx, value)) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 77 | ParserException parserException = new ParserException("Duplicate Value Entry"); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 78 | parserException.setLine(ctx.getStart().getLine()); |
| 79 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 80 | throw parserException; |
| 81 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 82 | enumNode.setValue(value); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 83 | break; |
| 84 | } |
| 85 | default: |
| 86 | throw new ParserException( |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 87 | constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY)); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Validates ENUM value uniqueness. |
| 93 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 94 | * @param listener Listener's object |
| 95 | * @param ctx context object of the grammar rule |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 96 | * @param value enum value |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 97 | * @return validation result |
| 98 | */ |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 99 | private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx, |
| 100 | int value) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 101 | Parsable enumNode = listener.getParsedDataStack().pop(); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 102 | |
| 103 | // Check for stack to be non empty. |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 104 | checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 105 | |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 106 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 107 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 108 | case ENUMERATION_DATA: { |
| 109 | YangEnumeration yangEnumeration = (YangEnumeration) tmpNode; |
| 110 | for (YangEnum curEnum : yangEnumeration.getEnumSet()) { |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 111 | if (value == curEnum.getValue()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 112 | listener.getParsedDataStack().push(enumNode); |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | listener.getParsedDataStack().push(enumNode); |
| 117 | return true; |
| 118 | } |
| 119 | default: |
| 120 | listener.getParsedDataStack().push(enumNode); |
| 121 | throw new ParserException( |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 122 | constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY)); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |