Vidyashree Rama | 72719fa | 2016-07-15 14:06:56 +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.YangExtension; |
| 20 | import org.onosproject.yangutils.datamodel.YangModule; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 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 | |
| 27 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.EXTENSION_DATA; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 36 | |
| 37 | /* |
| 38 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 39 | * |
| 40 | * ABNF grammar as per RFC6020 |
| 41 | * extension-stmt = extension-keyword sep identifier-arg-str optsep |
| 42 | * (";" / |
| 43 | * "{" stmtsep |
| 44 | * ;; these stmts can appear in any order |
| 45 | * [argument-stmt stmtsep] |
| 46 | * [status-stmt stmtsep] |
| 47 | * [description-stmt stmtsep] |
| 48 | * [reference-stmt stmtsep] |
| 49 | * "}") |
| 50 | * |
| 51 | * ANTLR grammar rule |
| 52 | * extensionStatement : EXTENSION_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE extensionBody RIGHT_CURLY_BRACE); |
| 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * Represents listener based call back function corresponding to the "extension" |
| 57 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 58 | */ |
| 59 | public final class ExtensionListener { |
| 60 | |
| 61 | /** |
| 62 | * Creates a new extension listener. |
| 63 | */ |
| 64 | private ExtensionListener() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Performs validation and updates the data model tree. It is called when parser |
| 69 | * receives an input matching the grammar rule (extension). |
| 70 | * |
| 71 | * @param listener listener's object |
| 72 | * @param ctx context object of the grammar rule |
| 73 | */ |
| 74 | public static void processExtensionEntry(TreeWalkListener listener, |
| 75 | GeneratedYangParser.ExtensionStatementContext ctx) { |
| 76 | |
| 77 | // Check for stack to be non empty. |
| 78 | checkStackIsNotEmpty(listener, MISSING_HOLDER, EXTENSION_DATA, ctx.identifier().getText(), ENTRY); |
| 79 | |
| 80 | String identifier = getValidIdentifier(ctx.identifier().getText(), EXTENSION_DATA, ctx); |
| 81 | |
| 82 | YangExtension extension = new YangExtension(); |
| 83 | extension.setName(identifier); |
| 84 | |
| 85 | Parsable curData = listener.getParsedDataStack().peek(); |
| 86 | switch (curData.getYangConstructType()) { |
| 87 | case MODULE_DATA: |
| 88 | YangModule module = ((YangModule) curData); |
| 89 | module.addExtension(extension); |
| 90 | break; |
| 91 | case SUB_MODULE_DATA: |
| 92 | YangSubModule subModule = ((YangSubModule) curData); |
| 93 | subModule.addExtension(extension); |
| 94 | break; |
| 95 | default: |
| 96 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, EXTENSION_DATA, |
| 97 | ctx.identifier().getText(), ENTRY)); |
| 98 | } |
| 99 | listener.getParsedDataStack().push(extension); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Performs validation and updates the data model tree. It is called when parser exits |
| 104 | * from grammar rule(extension). |
| 105 | * |
| 106 | * @param listener listener's object |
| 107 | * @param ctx context object of the grammar rule |
| 108 | */ |
| 109 | public static void processExtensionExit(TreeWalkListener listener, |
| 110 | GeneratedYangParser.ExtensionStatementContext ctx) { |
| 111 | |
| 112 | // Check for stack to be non empty. |
| 113 | checkStackIsNotEmpty(listener, MISSING_HOLDER, EXTENSION_DATA, ctx.identifier().getText(), EXIT); |
| 114 | |
| 115 | if (!(listener.getParsedDataStack().peek() instanceof YangExtension)) { |
| 116 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, EXTENSION_DATA, |
| 117 | ctx.identifier().getText(), EXIT)); |
| 118 | } |
| 119 | listener.getParsedDataStack().pop(); |
| 120 | } |
| 121 | } |