Gaurav Agrawal | 480a47d | 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 |
| 33 | * positionStatement : POSITION_KEYWORD INTEGER STMTEND; |
| 34 | */ |
| 35 | |
| 36 | import org.onosproject.yangutils.datamodel.YangBit; |
| 37 | import org.onosproject.yangutils.datamodel.YangBits; |
| 38 | import org.onosproject.yangutils.parser.Parsable; |
| 39 | import static org.onosproject.yangutils.parser.ParsableDataType.POSITION_DATA; |
| 40 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 41 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 42 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 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; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 48 | |
| 49 | /** |
| 50 | * Implements listener based call back function corresponding to the "position" |
| 51 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 52 | */ |
| 53 | public final class PositionListener { |
| 54 | |
| 55 | // Exact message in case position is invalid. |
| 56 | private static String errMsg; |
| 57 | |
| 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 | * |
| 68 | * @param listener Listener's object. |
| 69 | * @param ctx context object of the grammar rule. |
| 70 | */ |
| 71 | public static void processPositionEntry(TreeWalkListener listener, |
| 72 | GeneratedYangParser.PositionStatementContext ctx) { |
| 73 | |
| 74 | // Check for stack to be non empty. |
| 75 | checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY); |
| 76 | |
| 77 | // Obtain the node of the stack. |
| 78 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 79 | switch (tmpNode.getParsableDataType()) { |
| 80 | case BIT_DATA: { |
| 81 | YangBit bitNode = (YangBit) tmpNode; |
| 82 | if (!isBitPositionValid(listener, ctx)) { |
| 83 | ParserException parserException = new ParserException(errMsg); |
| 84 | parserException.setLine(ctx.INTEGER().getSymbol().getLine()); |
| 85 | parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine()); |
| 86 | throw parserException; |
| 87 | } |
| 88 | bitNode.setPosition(Integer.valueOf(ctx.INTEGER().getText())); |
| 89 | break; |
| 90 | } |
| 91 | default: |
| 92 | throw new ParserException( |
| 93 | constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Validates BITS position value correctness and uniqueness. |
| 99 | * |
| 100 | * @param listener Listener's object. |
| 101 | * @param ctx context object of the grammar rule. |
| 102 | * @return validation result |
| 103 | */ |
| 104 | private static boolean isBitPositionValid(TreeWalkListener listener, |
| 105 | GeneratedYangParser.PositionStatementContext ctx) { |
| 106 | Parsable bitNode = listener.getParsedDataStack().pop(); |
| 107 | |
| 108 | // Check for stack to be non empty. |
| 109 | checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY); |
| 110 | |
| 111 | if (Integer.valueOf(ctx.INTEGER().getText()) < 0) { |
| 112 | errMsg = "Negative value of position is invalid"; |
| 113 | listener.getParsedDataStack().push(bitNode); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 118 | switch (tmpNode.getParsableDataType()) { |
| 119 | case BITS_DATA: { |
| 120 | YangBits yangBits = (YangBits) tmpNode; |
| 121 | for (YangBit curBit : yangBits.getBitSet()) { |
| 122 | if (Integer.valueOf(ctx.INTEGER().getText()) == curBit.getPosition()) { |
| 123 | errMsg = "Duplicate value of position is invalid"; |
| 124 | listener.getParsedDataStack().push(bitNode); |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | listener.getParsedDataStack().push(bitNode); |
| 129 | return true; |
| 130 | } |
| 131 | default: |
| 132 | listener.getParsedDataStack().push(bitNode); |
| 133 | throw new ParserException( |
| 134 | constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY)); |
| 135 | } |
| 136 | } |
| 137 | } |