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 | * bit-stmt = bit-keyword sep identifier-arg-str optsep |
| 24 | * (";" / |
| 25 | * "{" stmtsep |
| 26 | * ;; these stmts can appear in any order |
| 27 | * [position-stmt stmtsep] |
| 28 | * [status-stmt stmtsep] |
| 29 | * [description-stmt stmtsep] |
| 30 | * [reference-stmt stmtsep] |
| 31 | * "}" |
| 32 | * "}") |
| 33 | * |
| 34 | * ANTLR grammar rule |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 35 | * bitStatement : BIT_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE bitBodyStatement RIGHT_CURLY_BRACE); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 36 | * |
| 37 | * bitBodyStatement : positionStatement? statusStatement? descriptionStatement? referenceStatement? |
| 38 | * | positionStatement? statusStatement? referenceStatement? descriptionStatement? |
| 39 | * | positionStatement? descriptionStatement? statusStatement? referenceStatement? |
| 40 | * | positionStatement? descriptionStatement? referenceStatement? statusStatement? |
| 41 | * | positionStatement? referenceStatement? statusStatement? descriptionStatement? |
| 42 | * | positionStatement? referenceStatement? descriptionStatement? statusStatement? |
| 43 | * | statusStatement? positionStatement? descriptionStatement? referenceStatement? |
| 44 | * | statusStatement? positionStatement? referenceStatement? descriptionStatement? |
| 45 | * | statusStatement? descriptionStatement? descriptionStatement? positionStatement? |
| 46 | * | statusStatement? descriptionStatement? positionStatement? descriptionStatement? |
| 47 | * | statusStatement? referenceStatement? positionStatement? descriptionStatement? |
| 48 | * | statusStatement? referenceStatement? descriptionStatement? positionStatement? |
| 49 | * | descriptionStatement? positionStatement? statusStatement? referenceStatement? |
| 50 | * | descriptionStatement? positionStatement? referenceStatement? statusStatement? |
| 51 | * | descriptionStatement? statusStatement? positionStatement? referenceStatement? |
| 52 | * | descriptionStatement? statusStatement? referenceStatement? positionStatement? |
| 53 | * | descriptionStatement? referenceStatement? positionStatement? statusStatement? |
| 54 | * | descriptionStatement? referenceStatement? statusStatement? positionStatement? |
| 55 | * | referenceStatement? positionStatement? descriptionStatement? statusStatement? |
| 56 | * | referenceStatement? positionStatement? statusStatement? descriptionStatement? |
| 57 | * | referenceStatement? statusStatement? descriptionStatement? positionStatement? |
| 58 | * | referenceStatement? statusStatement? positionStatement? descriptionStatement? |
| 59 | * | referenceStatement? descriptionStatement? positionStatement? statusStatement? |
| 60 | * | referenceStatement? descriptionStatement? statusStatement? positionStatement? |
| 61 | * ; |
| 62 | */ |
| 63 | |
| 64 | import org.onosproject.yangutils.datamodel.YangBit; |
| 65 | import org.onosproject.yangutils.datamodel.YangBits; |
| 66 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 67 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 68 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 69 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 70 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 71 | |
| 72 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
| 73 | import static org.onosproject.yangutils.utils.YangConstructType.BIT_DATA; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 75 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 76 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 77 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 78 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT; |
| 79 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 80 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 81 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 82 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 83 | |
| 84 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 85 | * Represents listener based call back function corresponding to the "bit" |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 86 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 87 | */ |
| 88 | public final class BitListener { |
| 89 | |
| 90 | /** |
| 91 | * Creates a new bit listener. |
| 92 | */ |
| 93 | private BitListener() { |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * It is called when parser enters grammar rule (bit), it perform |
| 98 | * validations and updates the data model tree. |
| 99 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 100 | * @param listener listener's object |
| 101 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 102 | */ |
| 103 | public static void processBitEntry(TreeWalkListener listener, |
| 104 | GeneratedYangParser.BitStatementContext ctx) { |
| 105 | |
| 106 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 107 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), ENTRY); |
| 108 | |
| 109 | String identifier = getValidIdentifier(ctx.identifier().getText(), BIT_DATA, ctx); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 110 | |
| 111 | YangBit bitNode = new YangBit(); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 112 | bitNode.setBitName(identifier); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 113 | listener.getParsedDataStack().push(bitNode); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * It is called when parser exits from grammar rule (bit), it perform |
| 118 | * validations and update the data model tree. |
| 119 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 120 | * @param listener Listener's object |
| 121 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 122 | */ |
| 123 | public static void processBitExit(TreeWalkListener listener, |
| 124 | GeneratedYangParser.BitStatementContext ctx) { |
| 125 | |
| 126 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 127 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 128 | |
| 129 | Parsable tmpBitNode = listener.getParsedDataStack().peek(); |
| 130 | if (tmpBitNode instanceof YangBit) { |
| 131 | listener.getParsedDataStack().pop(); |
| 132 | |
| 133 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 134 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 135 | |
| 136 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 137 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 138 | case BITS_DATA: { |
| 139 | YangBits yangBits = (YangBits) tmpNode; |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 140 | if (ctx.bitBodyStatement() == null || ctx.bitBodyStatement().positionStatement() == null) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 141 | int maxPosition = 0; |
| 142 | boolean isPositionPresent = false; |
| 143 | |
| 144 | for (YangBit curBit : yangBits.getBitSet()) { |
| 145 | if (maxPosition <= curBit.getPosition()) { |
| 146 | maxPosition = curBit.getPosition(); |
| 147 | isPositionPresent = true; |
| 148 | } |
| 149 | } |
| 150 | if (isPositionPresent) { |
| 151 | maxPosition++; |
| 152 | } |
| 153 | ((YangBit) tmpBitNode).setPosition(maxPosition); |
| 154 | } |
| 155 | try { |
| 156 | yangBits.addBitInfo((YangBit) tmpBitNode); |
| 157 | } catch (DataModelException e) { |
| 158 | ParserException parserException = new ParserException(constructExtendedListenerErrorMessage( |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 159 | INVALID_CONTENT, BIT_DATA, ctx.identifier().getText(), EXIT, e.getMessage())); |
| 160 | parserException.setLine(ctx.getStart().getLine()); |
| 161 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 162 | throw parserException; |
| 163 | } |
| 164 | break; |
| 165 | } |
| 166 | default: |
| 167 | throw new ParserException( |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 168 | constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 169 | } |
| 170 | } else { |
| 171 | throw new ParserException( |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 172 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |