Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +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 | 480a47d | 2016-02-26 02:47:39 +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 | * 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 | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 33 | * positionStatement : POSITION_KEYWORD position STMTEND; |
| 34 | * position : string; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | import org.onosproject.yangutils.datamodel.YangBit; |
| 38 | import org.onosproject.yangutils.datamodel.YangBits; |
| 39 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 40 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 41 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 42 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 43 | |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNonNegativeIntegerValue; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.YangConstructType.POSITION_DATA; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 53 | * Represents listener based call back function corresponding to the "position" |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 54 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 55 | */ |
| 56 | public final class PositionListener { |
| 57 | |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 58 | /** |
| 59 | * Creates a new position listener. |
| 60 | */ |
| 61 | private PositionListener() { |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * It is called when parser receives an input matching the grammar rule |
| 66 | * (position), perform validations and update the data model tree. |
| 67 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 68 | * @param listener Listener's object |
| 69 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 70 | */ |
| 71 | public static void processPositionEntry(TreeWalkListener listener, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 72 | GeneratedYangParser.PositionStatementContext ctx) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 73 | |
| 74 | // Check for stack to be non empty. |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 75 | checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.position().getText(), ENTRY); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 76 | |
| 77 | // Obtain the node of the stack. |
| 78 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 79 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 80 | case BIT_DATA: { |
| 81 | YangBit bitNode = (YangBit) tmpNode; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 82 | int positionValue = getValidBitPosition(listener, ctx); |
| 83 | bitNode.setPosition(positionValue); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | default: |
| 87 | throw new ParserException( |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 88 | constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.position().getText(), ENTRY)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Validates BITS position value correctness and uniqueness. |
| 94 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 95 | * @param listener Listener's object |
| 96 | * @param ctx context object of the grammar rule |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 97 | * @return position value |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 98 | */ |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 99 | private static int getValidBitPosition(TreeWalkListener listener, |
| 100 | GeneratedYangParser.PositionStatementContext ctx) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 101 | Parsable bitNode = listener.getParsedDataStack().pop(); |
| 102 | |
| 103 | // Check for stack to be non empty. |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 104 | checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.position().getText(), ENTRY); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 105 | |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 106 | int positionValue = getValidNonNegativeIntegerValue(ctx.position().getText(), POSITION_DATA, ctx); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 107 | |
| 108 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 109 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 110 | case BITS_DATA: { |
| 111 | YangBits yangBits = (YangBits) tmpNode; |
| 112 | for (YangBit curBit : yangBits.getBitSet()) { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 113 | if (positionValue == curBit.getPosition()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 114 | listener.getParsedDataStack().push(bitNode); |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 115 | ParserException parserException = new ParserException("YANG file error: Duplicate value of " + |
| 116 | "position is invalid."); |
| 117 | parserException.setLine(ctx.getStart().getLine()); |
| 118 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 119 | throw parserException; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | listener.getParsedDataStack().push(bitNode); |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 123 | return positionValue; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 124 | } |
| 125 | default: |
| 126 | listener.getParsedDataStack().push(bitNode); |
| 127 | throw new ParserException( |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 128 | constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.position().getText(), ENTRY)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |