Vidyashree Rama | 07c26bb | 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.YangAppExtendedName; |
| 20 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
| 21 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 25 | |
| 26 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.APP_EXTENDED_NAME_DATA; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidPrefix; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 34 | |
| 35 | /* |
| 36 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 37 | * |
| 38 | * ABNF grammar as per RFC6020 |
| 39 | * app-extended-stmt = prefix:app-extended-name-keyword string ";" |
| 40 | * |
| 41 | * ANTLR grammar rule |
| 42 | * appExtendedStatement : APP_EXTENDED extendedName STMTEND; |
| 43 | */ |
| 44 | |
| 45 | /** |
| 46 | * Represents listener based call back function corresponding to the "app-extended-name" |
| 47 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 48 | */ |
| 49 | public final class AppExtendedNameListener { |
| 50 | |
| 51 | /** |
| 52 | * Creates a new app-extended-name listener. |
| 53 | */ |
| 54 | private AppExtendedNameListener() { |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Performs validation and updates the data model tree. It is called when parser receives an |
| 59 | * input matching the grammar rule(app-extended-name). |
| 60 | * |
| 61 | * @param listener listener's object |
| 62 | * @param ctx context object of the grammar rule |
| 63 | */ |
| 64 | public static void processAppExtendedNameEntry(TreeWalkListener listener, |
| 65 | GeneratedYangParser.AppExtendedStatementContext ctx) { |
| 66 | |
| 67 | checkStackIsNotEmpty(listener, MISSING_HOLDER, APP_EXTENDED_NAME_DATA, ctx.extendedName().getText(), ENTRY); |
| 68 | |
| 69 | String prefix = getValidPrefix(ctx.APP_EXTENDED().getText(), APP_EXTENDED_NAME_DATA, ctx); |
| 70 | YangAppExtendedName extendedName = new YangAppExtendedName(); |
| 71 | extendedName.setPrefix(prefix); |
| 72 | extendedName.setYangAppExtendedName(removeQuotesAndHandleConcat(ctx.extendedName().getText())); |
| 73 | |
| 74 | Parsable curData = listener.getParsedDataStack().peek(); |
| 75 | if (curData instanceof YangCompilerAnnotation) { |
| 76 | YangCompilerAnnotation compilerAnnotation = ((YangCompilerAnnotation) curData); |
| 77 | compilerAnnotation.setYangAppExtendedName(extendedName); |
| 78 | } else { |
| 79 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, APP_EXTENDED_NAME_DATA, |
| 80 | ctx.extendedName().getText(), ENTRY)); |
| 81 | } |
| 82 | } |
| 83 | } |