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 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 19 | import java.util.List; |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 20 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
| 23 | import org.onosproject.yangutils.datamodel.YangModule; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNode; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangSubModule; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 30 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 31 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 32 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.COMPILER_ANNOTATION_DATA; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId; |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidPrefix; |
| 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
| 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 47 | |
| 48 | /* |
| 49 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 50 | * |
| 51 | * ABNF grammar as per RFC6020 |
| 52 | * compiler-annotation-stmt = prefix:compiler-annotation-keyword string |
| 53 | * "{" |
| 54 | * [app-data-structure-stmt stmtsep] |
| 55 | * [app-extended-stmt stmtsep] |
| 56 | * "}" |
| 57 | * |
| 58 | * ANTLR grammar rule |
| 59 | * compilerAnnotationStatement : COMPILER_ANNOTATION string LEFT_CURLY_BRACE |
| 60 | * compilerAnnotationBodyStatement RIGHT_CURLY_BRACE; |
| 61 | * |
| 62 | * compilerAnnotationBodyStatement : appDataStructureStatement? appExtendedStatement? ; |
| 63 | */ |
| 64 | |
| 65 | /** |
| 66 | * Represents listener based call back function corresponding to the "compiler-annotation" |
| 67 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 68 | */ |
| 69 | public final class CompilerAnnotationListener { |
| 70 | |
| 71 | /** |
| 72 | * Creates a new compiler-annotation listener. |
| 73 | */ |
| 74 | private CompilerAnnotationListener() { |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Performs validation and updates the data model tree. It is called when parser receives an |
| 79 | * input matching the grammar rule(compiler-annotation). |
| 80 | * |
| 81 | * @param listener listener's object |
| 82 | * @param ctx context object of the grammar rule |
| 83 | */ |
| 84 | public static void processCompilerAnnotationEntry(TreeWalkListener listener, |
| 85 | GeneratedYangParser.CompilerAnnotationStatementContext ctx) { |
| 86 | // Check for stack to be non empty. |
| 87 | checkStackIsNotEmpty(listener, MISSING_HOLDER, COMPILER_ANNOTATION_DATA, ctx.string().getText(), ENTRY); |
| 88 | String prefix = getValidPrefix(ctx.COMPILER_ANNOTATION().getText(), COMPILER_ANNOTATION_DATA, ctx); |
| 89 | |
| 90 | YangCompilerAnnotation compilerAnnotation = new YangCompilerAnnotation(); |
| 91 | compilerAnnotation.setPrefix(prefix); |
| 92 | compilerAnnotation.setPath(removeQuotesAndHandleConcat(ctx.string().getText())); |
| 93 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 94 | // Validate augment argument string |
| 95 | List<YangAtomicPath> targetNodes = getValidAbsoluteSchemaNodeId(ctx.string().getText(), |
| 96 | COMPILER_ANNOTATION_DATA, ctx); |
| 97 | |
| 98 | compilerAnnotation.setAtomicPathList(targetNodes); |
| 99 | |
| 100 | int line = ctx.getStart().getLine(); |
| 101 | int charPositionInLine = ctx.getStart().getCharPositionInLine(); |
| 102 | |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 103 | Parsable curData = listener.getParsedDataStack().peek(); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 104 | if (!(curData instanceof YangModule || curData instanceof YangSubModule)) { |
| 105 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, COMPILER_ANNOTATION_DATA, |
| 106 | ctx.string().getText(), ENTRY)); |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 107 | } |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 108 | |
| 109 | // Add resolution information to the list |
| 110 | YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangCompilerAnnotation>( |
| 111 | compilerAnnotation, (YangNode) curData, line, charPositionInLine); |
| 112 | addToResolutionList(resolutionInfo, ctx); |
| 113 | |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 114 | listener.getParsedDataStack().push(compilerAnnotation); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Performs validation and updates the data model tree. It is called when parser |
| 119 | * exits from grammar rule (compiler-annotation). |
| 120 | * |
| 121 | * @param listener listener's object |
| 122 | * @param ctx context object of the grammar rule |
| 123 | */ |
| 124 | public static void processCompilerAnnotationExit(TreeWalkListener listener, |
| 125 | GeneratedYangParser.CompilerAnnotationStatementContext ctx) { |
| 126 | |
| 127 | checkStackIsNotEmpty(listener, MISSING_HOLDER, COMPILER_ANNOTATION_DATA, ctx.string().getText(), EXIT); |
| 128 | if (!(listener.getParsedDataStack().peek() instanceof YangCompilerAnnotation)) { |
| 129 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, COMPILER_ANNOTATION_DATA, |
| 130 | ctx.string().getText(), EXIT)); |
| 131 | } |
| 132 | listener.getParsedDataStack().pop(); |
| 133 | } |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Adds to resolution list. |
| 137 | * |
| 138 | * @param resolutionInfo resolution information. |
| 139 | * @param ctx context object of the grammar rule |
| 140 | */ |
| 141 | private static void addToResolutionList(YangResolutionInfoImpl<YangCompilerAnnotation> resolutionInfo, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 142 | GeneratedYangParser.CompilerAnnotationStatementContext ctx) { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 143 | |
| 144 | try { |
| 145 | addResolutionInfo(resolutionInfo); |
| 146 | } catch (DataModelException e) { |
| 147 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 148 | COMPILER_ANNOTATION_DATA, ctx.COMPILER_ANNOTATION().getText(), ENTRY, e.getMessage())); |
| 149 | } |
| 150 | } |
Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 151 | } |