Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +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 | |
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; |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame^] | 21 | import org.onosproject.yangutils.datamodel.YangMaxElement; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 23 | import org.onosproject.yangutils.datamodel.utils.YangConstructType; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 27 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MAX_ELEMENT_DATA; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 38 | * |
| 39 | * ABNF grammar as per RFC6020 |
| 40 | * max-elements-stmt = max-elements-keyword sep |
| 41 | * max-value-arg-str stmtend |
| 42 | * max-value-arg-str = < a string that matches the rule |
| 43 | * max-value-arg > |
| 44 | * |
| 45 | * ANTLR grammar rule |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 46 | * maxElementsStatement : MAX_ELEMENTS_KEYWORD maxValue STMTEND; |
| 47 | * maxValue : string; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Represents listener based call back function corresponding to the |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 52 | * "max-elements" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 53 | * in RFC 6020. |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 54 | */ |
| 55 | public final class MaxElementsListener { |
| 56 | |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 57 | private static final String POSITIVE_INTEGER_PATTERN = "[1-9][0-9]*"; |
| 58 | private static final String UNBOUNDED_KEYWORD = "unbounded"; |
| 59 | |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 60 | /** |
| 61 | * Creates a new max-elements listener. |
| 62 | */ |
| 63 | private MaxElementsListener() { |
| 64 | } |
| 65 | |
| 66 | /** |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 67 | * It is called when parser receives an input matching the grammar rule |
| 68 | * (max-elements), performs validation and updates the data model tree. |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 69 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 70 | * @param listener listener's object |
| 71 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 72 | */ |
| 73 | public static void processMaxElementsEntry(TreeWalkListener listener, |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 74 | GeneratedYangParser.MaxElementsStatementContext ctx) { |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 75 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 76 | // Check for stack to be non empty. |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 77 | checkStackIsNotEmpty(listener, MISSING_HOLDER, MAX_ELEMENT_DATA, "", ENTRY); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 78 | |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 79 | int maxElementsValue = getValidMaxElementValue(ctx); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 80 | |
| 81 | Parsable tmpData = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 82 | switch (tmpData.getYangConstructType()) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 83 | case LEAF_LIST_DATA: |
| 84 | YangLeafList leafList = (YangLeafList) tmpData; |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame^] | 85 | YangMaxElement maxLeafListElement = new YangMaxElement(); |
| 86 | maxLeafListElement.setMaxElement(maxElementsValue); |
| 87 | leafList.setMaxElements(maxLeafListElement); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 88 | break; |
| 89 | case LIST_DATA: |
| 90 | YangList yangList = (YangList) tmpData; |
rama-huawei | 6c728a9 | 2016-07-11 14:48:12 +0530 | [diff] [blame^] | 91 | YangMaxElement maxListElement = new YangMaxElement(); |
| 92 | maxListElement.setMaxElement(maxElementsValue); |
| 93 | yangList.setMaxElements(maxListElement); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 94 | break; |
| 95 | default: |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 96 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MAX_ELEMENT_DATA, "", ENTRY)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 97 | } |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 98 | } |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Validates max element value and returns the value from context. |
| 102 | * |
| 103 | * @param ctx context object of the grammar rule |
| 104 | * @return max element's value |
| 105 | */ |
| 106 | private static int getValidMaxElementValue(GeneratedYangParser.MaxElementsStatementContext ctx) { |
| 107 | |
| 108 | int maxElementsValue; |
| 109 | |
| 110 | String value = removeQuotesAndHandleConcat(ctx.maxValue().getText()); |
| 111 | if (value.equals(UNBOUNDED_KEYWORD)) { |
| 112 | maxElementsValue = Integer.MAX_VALUE; |
| 113 | } else if (value.matches(POSITIVE_INTEGER_PATTERN)) { |
Vidyashree Rama | 210c01d | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 114 | try { |
| 115 | maxElementsValue = Integer.parseInt(value); |
| 116 | } catch (NumberFormatException e) { |
| 117 | ParserException parserException = new ParserException("YANG file error : " + |
| 118 | YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " + |
| 119 | "valid."); |
| 120 | parserException.setLine(ctx.getStart().getLine()); |
| 121 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 122 | throw parserException; |
| 123 | } |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 124 | } else { |
| 125 | ParserException parserException = new ParserException("YANG file error : " + |
| 126 | YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " + |
| 127 | "valid."); |
| 128 | parserException.setLine(ctx.getStart().getLine()); |
| 129 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 130 | throw parserException; |
| 131 | } |
| 132 | |
| 133 | return maxElementsValue; |
| 134 | } |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 135 | } |