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