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