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; |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
| 21 | import org.onosproject.yangutils.datamodel.YangPatternRestriction; |
| 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; |
| 29 | |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +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; |
| 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.YangConstructType.PATTERN_DATA; |
| 39 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 43 | * |
| 44 | * ABNF grammar as per RFC6020 |
| 45 | * pattern-stmt = pattern-keyword sep string 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 | * patternStatement : PATTERN_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE); |
| 57 | */ |
| 58 | |
| 59 | /** |
| 60 | * Represents listener based call back function corresponding to the "pattern" |
| 61 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 62 | */ |
| 63 | public final class PatternRestrictionListener { |
| 64 | |
| 65 | private static final String EMPTY_STRING = ""; |
| 66 | |
| 67 | /** |
| 68 | * Creates a new pattern restriction listener. |
| 69 | */ |
| 70 | private PatternRestrictionListener() { |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * It is called when parser receives an input matching the grammar |
| 75 | * rule (pattern), performs validation and updates the data model |
| 76 | * tree. |
| 77 | * |
| 78 | * @param listener listener's object |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 79 | * @param ctx context object of the grammar rule |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 80 | */ |
| 81 | public static void processPatternRestrictionEntry(TreeWalkListener listener, |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 82 | GeneratedYangParser.PatternStatementContext ctx) { |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 83 | |
| 84 | // Check for stack to be non empty. |
| 85 | checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), ENTRY); |
| 86 | |
| 87 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 88 | if (tmpData.getYangConstructType() == TYPE_DATA) { |
| 89 | YangType type = (YangType) tmpData; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 90 | setPatternRestriction(listener, type, ctx); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 91 | } else { |
| 92 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PATTERN_DATA, |
| 93 | ctx.string().getText(), ENTRY)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets the pattern restriction to type. |
| 99 | * |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 100 | * @param listener listener's object |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 101 | * @param type Yang type for which pattern restriction to be set |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 102 | * @param ctx context object of the grammar rule |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 103 | */ |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 104 | private static void setPatternRestriction(TreeWalkListener listener, YangType type, |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 105 | GeneratedYangParser.PatternStatementContext ctx) { |
| 106 | |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 107 | if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) { |
| 108 | |
| 109 | ParserException parserException = new ParserException("YANG file error : " + |
| 110 | YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() + |
| 111 | " can be used to restrict the built-in type string or types derived from string."); |
| 112 | parserException.setLine(ctx.getStart().getLine()); |
| 113 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 114 | throw parserException; |
| 115 | } |
| 116 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 117 | String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 118 | |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 119 | if (type.getDataType() == YangDataTypes.STRING) { |
| 120 | YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo(); |
| 121 | if (stringRestriction == null) { |
| 122 | stringRestriction = new YangStringRestriction(); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 123 | type.setDataTypeExtendedInfo(stringRestriction); |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 124 | stringRestriction.addPattern(patternArgument); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 125 | } else { |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 126 | stringRestriction.addPattern(patternArgument); |
| 127 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 128 | listener.getParsedDataStack().push(stringRestriction); |
Gaurav Agrawal | cc55d48 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 129 | } else { |
| 130 | YangPatternRestriction patternRestriction = (YangPatternRestriction) ((YangDerivedInfo<?>) type |
| 131 | .getDataTypeExtendedInfo()).getPatternRestriction(); |
| 132 | if (patternRestriction == null) { |
| 133 | patternRestriction = new YangPatternRestriction(); |
| 134 | ((YangDerivedInfo<?>) type.getDataTypeExtendedInfo()).setPatternRestriction(patternRestriction); |
| 135 | patternRestriction.addPattern(patternArgument); |
| 136 | } else { |
| 137 | ((YangDerivedInfo<?>) type.getDataTypeExtendedInfo()).setPatternRestriction(patternRestriction); |
| 138 | patternRestriction.addPattern(patternArgument); |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 139 | } |
| 140 | } |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 141 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Performs validation and updates the data model tree. |
| 145 | * It is called when parser exits from grammar rule (pattern). |
| 146 | * |
| 147 | * @param listener listener's object |
| 148 | * @param ctx context object of the grammar rule |
| 149 | */ |
| 150 | public static void processPatternRestrictionExit(TreeWalkListener listener, |
| 151 | GeneratedYangParser.PatternStatementContext ctx) { |
| 152 | |
| 153 | // Check for stack to be non empty. |
| 154 | checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), EXIT); |
| 155 | |
| 156 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 157 | if (tmpData instanceof YangStringRestriction) { |
| 158 | listener.getParsedDataStack().pop(); |
| 159 | } else if (tmpData instanceof YangType |
| 160 | && ((YangType) tmpData).getDataType() == DERIVED) { |
| 161 | // TODO : need to handle in linker |
| 162 | } else { |
| 163 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, PATTERN_DATA, |
| 164 | ctx.string().getText(), EXIT)); |
| 165 | } |
| 166 | } |
Vidyashree Rama | bc9611f | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 167 | } |