Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 20 | import org.onosproject.yangutils.datamodel.YangList; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
| 22 | import org.onosproject.yangutils.parser.ParsableDataType; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 29 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 33 | * |
| 34 | * ABNF grammar as per RFC6020 |
| 35 | * max-elements-stmt = max-elements-keyword sep |
| 36 | * max-value-arg-str stmtend |
| 37 | * max-value-arg-str = < a string that matches the rule |
| 38 | * max-value-arg > |
| 39 | * |
| 40 | * ANTLR grammar rule |
| 41 | * maxElementsStatement : MAX_ELEMENTS_KEYWORD maxValueArgument STMTEND; |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * Implements listener based call back function corresponding to the "max-elements" |
| 46 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 47 | */ |
| 48 | public final class MaxElementsListener { |
| 49 | |
| 50 | /** |
| 51 | * Creates a new max-elements listener. |
| 52 | */ |
| 53 | private MaxElementsListener() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * It is called when parser receives an input matching the grammar |
| 58 | * rule (max-elements), performs validation and updates the data model |
| 59 | * tree. |
| 60 | * |
| 61 | * @param listener listener's object. |
| 62 | * @param ctx context object of the grammar rule. |
| 63 | */ |
| 64 | public static void processMaxElementsEntry(TreeWalkListener listener, |
| 65 | GeneratedYangParser.MaxElementsStatementContext ctx) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 66 | int maxElementsValue; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 67 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 68 | // Check for stack to be non empty. |
| 69 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 70 | ParsableDataType.MAX_ELEMENT_DATA, "", ListenerErrorLocation.ENTRY); |
| 71 | |
| 72 | if (ctx.maxValueArgument().UNBOUNDED_KEYWORD() != null) { |
| 73 | maxElementsValue = Integer.MAX_VALUE; |
| 74 | } else { |
| 75 | maxElementsValue = Integer.parseInt(ctx.maxValueArgument().INTEGER().getText()); |
| 76 | } |
| 77 | |
| 78 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 79 | switch (tmpData.getParsableDataType()) { |
| 80 | case LEAF_LIST_DATA: |
| 81 | YangLeafList leafList = (YangLeafList) tmpData; |
| 82 | leafList.setMaxElelements(maxElementsValue); |
| 83 | break; |
| 84 | case LIST_DATA: |
| 85 | YangList yangList = (YangList) tmpData; |
| 86 | yangList.setMaxElelements(maxElementsValue); |
| 87 | break; |
| 88 | default: |
| 89 | throw new ParserException(ListenerErrorMessageConstruction |
| 90 | .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, |
| 91 | ParsableDataType.MAX_ELEMENT_DATA, |
| 92 | "", ListenerErrorLocation.ENTRY)); |
| 93 | } |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 94 | } |
| 95 | } |