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 | |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 64 | import java.util.Map; |
| 65 | |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 66 | import org.onosproject.yangutils.datamodel.YangBit; |
| 67 | import org.onosproject.yangutils.datamodel.YangBits; |
| 68 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 69 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 70 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 71 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 72 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 73 | |
| 74 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BIT_DATA; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 76 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 77 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 78 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 79 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 80 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT; |
| 81 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 82 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 83 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 84 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 85 | |
| 86 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | * Represents listener based call back function corresponding to the "bit" |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 88 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 89 | */ |
| 90 | public final class BitListener { |
| 91 | |
| 92 | /** |
| 93 | * Creates a new bit listener. |
| 94 | */ |
| 95 | private BitListener() { |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * It is called when parser enters grammar rule (bit), it perform |
| 100 | * validations and updates the data model tree. |
| 101 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 102 | * @param listener listener's object |
| 103 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 104 | */ |
| 105 | public static void processBitEntry(TreeWalkListener listener, |
| 106 | GeneratedYangParser.BitStatementContext ctx) { |
| 107 | |
| 108 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 109 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), ENTRY); |
| 110 | |
| 111 | String identifier = getValidIdentifier(ctx.identifier().getText(), BIT_DATA, ctx); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 112 | |
| 113 | YangBit bitNode = new YangBit(); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 114 | bitNode.setBitName(identifier); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 115 | listener.getParsedDataStack().push(bitNode); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * It is called when parser exits from grammar rule (bit), it perform |
| 120 | * validations and update the data model tree. |
| 121 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 122 | * @param listener Listener's object |
| 123 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 124 | */ |
| 125 | public static void processBitExit(TreeWalkListener listener, |
| 126 | GeneratedYangParser.BitStatementContext ctx) { |
| 127 | |
| 128 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 129 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 130 | |
| 131 | Parsable tmpBitNode = listener.getParsedDataStack().peek(); |
| 132 | if (tmpBitNode instanceof YangBit) { |
| 133 | listener.getParsedDataStack().pop(); |
| 134 | |
| 135 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 136 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 137 | |
| 138 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 139 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 140 | case BITS_DATA: { |
| 141 | YangBits yangBits = (YangBits) tmpNode; |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 142 | if (ctx.bitBodyStatement() == null || ctx.bitBodyStatement().positionStatement() == null) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 143 | int maxPosition = 0; |
| 144 | boolean isPositionPresent = false; |
| 145 | |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 146 | for (Map.Entry<Integer, YangBit> element : yangBits.getBitPositionMap().entrySet()) { |
| 147 | if (maxPosition <= element.getKey()) { |
| 148 | maxPosition = element.getKey(); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 149 | isPositionPresent = true; |
| 150 | } |
| 151 | } |
Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 152 | |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 153 | if (isPositionPresent) { |
| 154 | maxPosition++; |
| 155 | } |
| 156 | ((YangBit) tmpBitNode).setPosition(maxPosition); |
| 157 | } |
| 158 | try { |
| 159 | yangBits.addBitInfo((YangBit) tmpBitNode); |
| 160 | } catch (DataModelException e) { |
| 161 | ParserException parserException = new ParserException(constructExtendedListenerErrorMessage( |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 162 | INVALID_CONTENT, BIT_DATA, ctx.identifier().getText(), EXIT, e.getMessage())); |
| 163 | parserException.setLine(ctx.getStart().getLine()); |
| 164 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 165 | throw parserException; |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | default: |
| 170 | throw new ParserException( |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 171 | constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 172 | } |
| 173 | } else { |
| 174 | throw new ParserException( |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 175 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |