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