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