Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +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 | 6250e6d | 2016-03-29 09:52:22 +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 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangRangeRestriction; |
| 21 | import org.onosproject.yangutils.datamodel.YangType; |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.Parsable; |
| 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 26 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED; |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.RestrictionResolver.isOfRangeRestrictedType; |
| 34 | import static org.onosproject.yangutils.utils.RestrictionResolver.processRangeRestriction; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA; |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 40 | * |
| 41 | * ABNF grammar as per RFC6020 |
| 42 | * range-stmt = range-keyword sep range-arg-str optsep |
| 43 | * (";" / |
| 44 | * "{" stmtsep |
| 45 | * ;; these stmts can appear in any order |
| 46 | * [error-message-stmt stmtsep] |
| 47 | * [error-app-tag-stmt stmtsep] |
| 48 | * [description-stmt stmtsep] |
| 49 | * [reference-stmt stmtsep] |
| 50 | * "}") |
| 51 | * |
| 52 | * ANTLR grammar rule |
| 53 | * rangeStatement : RANGE_KEYWORD range (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE); |
| 54 | */ |
| 55 | |
| 56 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 57 | * Represents listener based call back function corresponding to the "range" |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 58 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 59 | */ |
| 60 | public final class RangeRestrictionListener { |
| 61 | |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 62 | /** |
| 63 | * Creates a new range restriction listener. |
| 64 | */ |
| 65 | private RangeRestrictionListener() { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * It is called when parser receives an input matching the grammar |
| 70 | * rule (range), performs validation and updates the data model |
| 71 | * tree. |
| 72 | * |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 73 | * @param listener listener's object |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 74 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 75 | */ |
| 76 | public static void processRangeRestrictionEntry(TreeWalkListener listener, |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 77 | GeneratedYangParser.RangeStatementContext ctx) { |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 78 | |
| 79 | // Check for stack to be non empty. |
| 80 | checkStackIsNotEmpty(listener, MISSING_HOLDER, RANGE_DATA, ctx.range().getText(), ENTRY); |
| 81 | |
| 82 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 83 | if (tmpData.getYangConstructType() == TYPE_DATA) { |
| 84 | YangType type = (YangType) tmpData; |
| 85 | setRangeRestriction(type, ctx); |
| 86 | } else { |
| 87 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RANGE_DATA, |
| 88 | ctx.range().getText(), ENTRY)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets the range restriction to type. |
| 94 | * |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 95 | * @param type YANG type for which range restriction to be added |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 96 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 97 | */ |
| 98 | private static void setRangeRestriction(YangType type, |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 99 | GeneratedYangParser.RangeStatementContext ctx) { |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 100 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 101 | if (type.getDataType() == DERIVED) { |
| 102 | ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo()) |
| 103 | .setRangeRestrictionString(ctx.range().getText()); |
| 104 | ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo()) |
| 105 | .setLineNumber(ctx.getStart().getLine()); |
| 106 | ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo()) |
| 107 | .setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 108 | return; |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 109 | } |
| 110 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 111 | if (!(isOfRangeRestrictedType(type.getDataType()))) { |
| 112 | ParserException parserException = new ParserException("YANG file error: Range restriction can't be " + |
| 113 | "applied to a given type"); |
| 114 | parserException.setLine(ctx.getStart().getLine()); |
| 115 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 116 | throw parserException; |
| 117 | } |
| 118 | |
| 119 | YangRangeRestriction rangeRestriction = processRangeRestriction(null, ctx.getStart().getLine(), |
| 120 | ctx.getStart().getCharPositionInLine(), false, ctx.range().getText(), type.getDataType()); |
| 121 | |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 122 | if (rangeRestriction != null) { |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 123 | type.setDataTypeExtendedInfo(rangeRestriction); |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 124 | } |
Vidyashree Rama | 6250e6d | 2016-03-29 09:52:22 +0530 | [diff] [blame] | 125 | } |
| 126 | } |