Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 19 | import org.onosproject.yangutils.datamodel.YangAppDataStructure; |
| 20 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
| 21 | import org.onosproject.yangutils.datamodel.YangDataStructure; |
| 22 | import org.onosproject.yangutils.datamodel.utils.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 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.datamodel.YangDataStructure.getDataStructureType; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.APP_DATA_STRUCTURE; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidPrefix; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 37 | |
| 38 | /* |
| 39 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 40 | * |
| 41 | * ABNF grammar as per RFC6020 |
| 42 | * app-data-structure-stmt = prefix:app-data-structure-keyword string |
| 43 | * (";" / |
| 44 | * "{" |
| 45 | * [data-structure-key-stmt stmtsep] |
| 46 | * "}") |
| 47 | * |
| 48 | * ANTLR grammar rule |
| 49 | * appDataStructureStatement : APP_DATA_STRUCTURE appDataStructure (STMTEND | (LEFT_CURLY_BRACE |
| 50 | * dataStructureKeyStatement? RIGHT_CURLY_BRACE)); |
| 51 | */ |
| 52 | |
| 53 | /** |
| 54 | * Represents listener based call back function corresponding to the "app-data-structure" |
| 55 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 56 | */ |
| 57 | public final class AppDataStructureListener { |
| 58 | |
| 59 | /** |
| 60 | * Creates a new app-data-structure listener. |
| 61 | */ |
| 62 | private AppDataStructureListener() { |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Performs validation and updates the data model tree. It is called when parser receives an |
| 67 | * input matching the grammar rule(app-data-structure). |
| 68 | * |
| 69 | * @param listener listener's object |
| 70 | * @param ctx context object of the grammar rule |
| 71 | */ |
| 72 | public static void processAppDataStructureEntry(TreeWalkListener listener, |
| 73 | GeneratedYangParser.AppDataStructureStatementContext ctx) { |
| 74 | |
| 75 | checkStackIsNotEmpty(listener, MISSING_HOLDER, APP_DATA_STRUCTURE, "", ENTRY); |
| 76 | |
| 77 | String prefix = getValidPrefix(ctx.APP_DATA_STRUCTURE().getText(), APP_DATA_STRUCTURE, ctx); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 78 | YangDataStructure dataStructure = getDataStructureType(ctx.appDataStructure().getText()); |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 79 | |
| 80 | YangAppDataStructure appDataStructure = new YangAppDataStructure(); |
| 81 | appDataStructure.setPrefix(prefix); |
| 82 | appDataStructure.setDataStructure(dataStructure); |
| 83 | |
| 84 | Parsable curData = listener.getParsedDataStack().peek(); |
| 85 | if (curData instanceof YangCompilerAnnotation) { |
| 86 | YangCompilerAnnotation compilerAnnotation = ((YangCompilerAnnotation) curData); |
| 87 | compilerAnnotation.setYangAppDataStructure(appDataStructure); |
| 88 | listener.getParsedDataStack().push(appDataStructure); |
| 89 | } else { |
| 90 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, APP_DATA_STRUCTURE, |
| 91 | "", ENTRY)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Performs validation and updates the data model tree. It is called when parser |
| 97 | * exits from grammar rule (app-data-structure). |
| 98 | * |
| 99 | * @param listener listener's object |
| 100 | * @param ctx context object of the grammar rule |
| 101 | */ |
| 102 | public static void processAppDataStructureExit(TreeWalkListener listener, |
| 103 | GeneratedYangParser.AppDataStructureStatementContext ctx) { |
| 104 | |
| 105 | checkStackIsNotEmpty(listener, MISSING_HOLDER, APP_DATA_STRUCTURE, "", EXIT); |
| 106 | if (!(listener.getParsedDataStack().peek() instanceof YangAppDataStructure)) { |
| 107 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, APP_DATA_STRUCTURE, |
| 108 | "", EXIT)); |
| 109 | } |
| 110 | listener.getParsedDataStack().pop(); |
| 111 | } |
| 112 | } |