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