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 | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.RestrictionResolver.processLengthRestriction; |
| 37 | import static org.onosproject.yangutils.utils.YangConstructType.LENGTH_DATA; |
| 38 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 42 | * |
| 43 | * ABNF grammar as per RFC6020 |
| 44 | * length-stmt = length-keyword sep length-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 | * |
| 55 | * ANTLR grammar rule |
| 56 | * lengthStatement : LENGTH_KEYWORD length |
| 57 | * (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE); |
| 58 | */ |
| 59 | |
| 60 | /** |
| 61 | * Represents listener based call back function corresponding to the "length" |
| 62 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 63 | */ |
| 64 | public final class LengthRestrictionListener { |
| 65 | |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 66 | /** |
| 67 | * Creates a new length restriction listener. |
| 68 | */ |
| 69 | private LengthRestrictionListener() { |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * It is called when parser receives an input matching the grammar |
| 74 | * rule (length), performs validation and updates the data model |
| 75 | * tree. |
| 76 | * |
| 77 | * @param listener listener's object |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 78 | * @param ctx context object of the grammar rule |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 79 | */ |
| 80 | public static void processLengthRestrictionEntry(TreeWalkListener listener, |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 81 | GeneratedYangParser.LengthStatementContext ctx) { |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 82 | |
| 83 | // Check for stack to be non empty. |
| 84 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LENGTH_DATA, ctx.length().getText(), ENTRY); |
| 85 | |
| 86 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 87 | if (tmpData.getYangConstructType() == TYPE_DATA) { |
| 88 | YangType type = (YangType) tmpData; |
| 89 | setLengthRestriction(type, ctx); |
| 90 | } else { |
| 91 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LENGTH_DATA, |
| 92 | ctx.length().getText(), ENTRY)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the length restriction to type. |
| 98 | * |
| 99 | * @param type Yang type for which length restriction to be set |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 100 | * @param ctx context object of the grammar rule |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 101 | */ |
| 102 | private static void setLengthRestriction(YangType type, |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 103 | GeneratedYangParser.LengthStatementContext 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 | .setLengthRestrictionString(ctx.length().getText()); |
| 108 | ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo()) |
| 109 | .setLineNumber(ctx.getStart().getLine()); |
| 110 | ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo()) |
| 111 | .setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 112 | return; |
| 113 | } |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 114 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 115 | if (type.getDataType() != YangDataTypes.STRING) { |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 116 | ParserException parserException = new ParserException("YANG file error : " + |
| 117 | YangConstructType.getYangConstructType(LENGTH_DATA) + " name " + ctx.length().getText() + |
| 118 | " can be used to restrict the built-in type string or types derived from string."); |
| 119 | parserException.setLine(ctx.getStart().getLine()); |
| 120 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 121 | throw parserException; |
| 122 | } |
| 123 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 124 | YangRangeRestriction lengthRestriction = processLengthRestriction(null, ctx.getStart().getLine(), |
| 125 | ctx.getStart().getCharPositionInLine(), false, ctx.length().getText()); |
| 126 | |
| 127 | YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo(); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 128 | |
| 129 | if (stringRestriction == null) { |
| 130 | stringRestriction = new YangStringRestriction(); |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 131 | type.setDataTypeExtendedInfo(stringRestriction); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | stringRestriction.setLengthRestriction(lengthRestriction); |
| 135 | } |
| 136 | } |