Mahesh Poojary Huawei | 2cd4433 | 2016-07-14 12:38:17 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 19 | /* |
| 20 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 21 | * |
| 22 | * ABNF grammar as per RFC6020 |
| 23 | * decimal64-specification = fraction-digits-stmt |
| 24 | * |
| 25 | * fraction-digits-stmt = fraction-digits-keyword sep |
| 26 | * fraction-digits-arg-str stmtend |
| 27 | * |
| 28 | * fraction-digits-arg-str = < a string that matches the rule |
| 29 | * fraction-digits-arg > |
| 30 | * |
| 31 | * fraction-digits-arg = ("1" ["0" / "1" / "2" / "3" / "4" / |
| 32 | * "5" / "6" / "7" / "8"]) |
| 33 | * / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" |
| 34 | * |
| 35 | * ANTLR grammar rule |
| 36 | * decimal64Specification : FRACTION_DIGITS_KEYWORD fraction STMTEND; |
| 37 | * |
| 38 | * fraction : string; |
| 39 | */ |
| 40 | |
| 41 | import org.onosproject.yangutils.datamodel.YangDecimal64; |
| 42 | import org.onosproject.yangutils.datamodel.YangRangeRestriction; |
| 43 | import org.onosproject.yangutils.datamodel.YangType; |
| 44 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 45 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 46 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 47 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 48 | |
| 49 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.FRACTION_DIGITS_DATA; |
| 50 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 51 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 52 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 53 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 55 | |
| 56 | /** |
| 57 | * Represents listener based call back function corresponding to the "fraction-digits" |
| 58 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 59 | */ |
| 60 | public final class FractionDigitsListener { |
| 61 | |
| 62 | /** |
| 63 | * Creates a new bit listener. |
| 64 | */ |
| 65 | private FractionDigitsListener() { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * It is called when parser enters grammar rule (fraction-digits), it perform |
| 70 | * validations and updates the data model tree. |
| 71 | * |
| 72 | * @param listener listener's object |
| 73 | * @param ctx context object of the grammar rule |
| 74 | */ |
| 75 | public static void processFractionDigitsEntry(TreeWalkListener listener, |
| 76 | GeneratedYangParser.FractionDigitStatementContext ctx) { |
| 77 | |
| 78 | // Check for stack to be non empty. |
| 79 | checkStackIsNotEmpty(listener, MISSING_HOLDER, FRACTION_DIGITS_DATA, ctx.fraction().getText(), ENTRY); |
| 80 | |
| 81 | int value = getValidFractionDigits(ctx); |
| 82 | |
| 83 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 84 | if (tmpNode instanceof YangType) { |
| 85 | YangType<YangDecimal64<YangRangeRestriction>> typeNode = |
| 86 | (YangType<YangDecimal64<YangRangeRestriction>>) tmpNode; |
| 87 | YangDecimal64 decimal64Node = typeNode.getDataTypeExtendedInfo(); |
| 88 | decimal64Node.setFractionDigit(value); |
| 89 | } else { |
| 90 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, FRACTION_DIGITS_DATA, |
| 91 | ctx.fraction().getText(), ENTRY)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Validate fraction digits. |
| 97 | * |
| 98 | * @param ctx context object of the grammar rule |
| 99 | * @return validated fraction-digits |
| 100 | */ |
| 101 | public static int getValidFractionDigits(GeneratedYangParser.FractionDigitStatementContext ctx) { |
| 102 | String value = ctx.fraction().getText().trim(); |
| 103 | ParserException parserException; |
| 104 | |
| 105 | int fractionDigits = Integer.parseInt(value); |
| 106 | if ((fractionDigits >= YangDecimal64.MIN_FRACTION_DIGITS_VALUE) && |
| 107 | (fractionDigits <= YangDecimal64.MAX_FRACTION_DIGITS_VALUE)) { |
| 108 | return fractionDigits; |
| 109 | } else { |
| 110 | parserException = |
| 111 | new ParserException("YANG file error : fraction-digits value should be between 1 and 18."); |
| 112 | parserException.setLine(ctx.getStart().getLine()); |
| 113 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 114 | throw parserException; |
| 115 | } |
| 116 | } |
| 117 | } |