Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +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 | * position-stmt = position-keyword sep |
| 24 | * position-value-arg-str stmtend |
| 25 | * position-value-arg-str = < a string that matches the rule |
| 26 | * position-value-arg > |
| 27 | * position-value-arg = non-negative-integer-value |
| 28 | * non-negative-integer-value = "0" / positive-integer-value |
| 29 | * positive-integer-value = (non-zero-digit *DIGIT) |
| 30 | * zero-integer-value = 1*DIGIT |
| 31 | * |
| 32 | * ANTLR grammar rule |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 33 | * positionStatement : POSITION_KEYWORD string STMTEND; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | import org.onosproject.yangutils.datamodel.YangBit; |
| 37 | import org.onosproject.yangutils.datamodel.YangBits; |
| 38 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 40 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 41 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 42 | |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.YangConstructType.POSITION_DATA; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Implements listener based call back function corresponding to the "position" |
| 53 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 54 | */ |
| 55 | public final class PositionListener { |
| 56 | |
| 57 | // Exact message in case position is invalid. |
| 58 | private static String errMsg; |
| 59 | |
| 60 | /** |
| 61 | * Creates a new position listener. |
| 62 | */ |
| 63 | private PositionListener() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * It is called when parser receives an input matching the grammar rule |
| 68 | * (position), perform validations and update the data model tree. |
| 69 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 70 | * @param listener Listener's object |
| 71 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 72 | */ |
| 73 | public static void processPositionEntry(TreeWalkListener listener, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 74 | GeneratedYangParser.PositionStatementContext ctx) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 75 | |
| 76 | // Check for stack to be non empty. |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 77 | checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY); |
| 78 | |
| 79 | String position = removeQuotesAndHandleConcat(ctx.string().getText()); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 80 | |
| 81 | // Obtain the node of the stack. |
| 82 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 83 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 84 | case BIT_DATA: { |
| 85 | YangBit bitNode = (YangBit) tmpNode; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 86 | if (!isBitPositionValid(listener, ctx, position)) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 87 | ParserException parserException = new ParserException(errMsg); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 88 | parserException.setLine(ctx.getStart().getLine()); |
| 89 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 90 | throw parserException; |
| 91 | } |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 92 | bitNode.setPosition(Integer.valueOf(position)); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | default: |
| 96 | throw new ParserException( |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 97 | constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY)); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Validates BITS position value correctness and uniqueness. |
| 103 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 104 | * @param listener Listener's object |
| 105 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 106 | * @return validation result |
| 107 | */ |
| 108 | private static boolean isBitPositionValid(TreeWalkListener listener, |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 109 | GeneratedYangParser.PositionStatementContext ctx, String position) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 110 | Parsable bitNode = listener.getParsedDataStack().pop(); |
| 111 | |
| 112 | // Check for stack to be non empty. |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 113 | checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 114 | |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 115 | if (Integer.valueOf(position) < 0) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 116 | errMsg = "YANG file error: Negative value of position is invalid."; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 117 | listener.getParsedDataStack().push(bitNode); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 122 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 123 | case BITS_DATA: { |
| 124 | YangBits yangBits = (YangBits) tmpNode; |
| 125 | for (YangBit curBit : yangBits.getBitSet()) { |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 126 | if (Integer.valueOf(position) == curBit.getPosition()) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 127 | errMsg = "YANG file error: Duplicate value of position is invalid."; |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 128 | listener.getParsedDataStack().push(bitNode); |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | listener.getParsedDataStack().push(bitNode); |
| 133 | return true; |
| 134 | } |
| 135 | default: |
| 136 | listener.getParsedDataStack().push(bitNode); |
| 137 | throw new ParserException( |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame^] | 138 | constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.string().getText(), ENTRY)); |
Gaurav Agrawal | 0cfdeed | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |