Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +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 | package org.onosproject.yangutils.parser.impl.listeners; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.YangIdentity; |
| 19 | import org.onosproject.yangutils.datamodel.YangModule; |
| 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 23 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 27 | |
| 28 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 29 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangIdentityNode; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 40 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IDENTITY_DATA; |
| 41 | |
| 42 | /** |
| 43 | * Reference: RFC6020 and YANG ANTLR Grammar. |
| 44 | * |
| 45 | * ABNF grammar as per RFC6020 |
| 46 | * identity-stmt = identity-keyword sep identifier-arg-str optsep |
| 47 | * (";" / |
| 48 | * "{" stmtsep |
| 49 | * ;; these stmts can appear in any order |
| 50 | * [base-stmt stmtsep] |
| 51 | * [status-stmt stmtsep] |
| 52 | * [description-stmt stmtsep] |
| 53 | * [reference-stmt stmtsep] |
| 54 | * "}") |
| 55 | */ |
| 56 | |
| 57 | /** |
| 58 | * Represents listener based call back function corresponding to the "identity" |
| 59 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 60 | */ |
| 61 | public final class IdentityListener { |
| 62 | |
| 63 | //Creates a identity listener. |
| 64 | private IdentityListener() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Performs validations and update the data model tree when parser receives an input |
| 69 | * matching the grammar rule (identity). |
| 70 | * |
| 71 | * @param listener Listener's object |
| 72 | * @param ctx context object of the grammar rule |
| 73 | */ |
| 74 | public static void processIdentityEntry(TreeWalkListener listener, |
| 75 | GeneratedYangParser.IdentityStatementContext ctx) { |
| 76 | |
| 77 | // Check for stack to be non empty. |
| 78 | checkStackIsNotEmpty(listener, MISSING_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), ENTRY); |
| 79 | |
| 80 | String identifier = getValidIdentifier(ctx.identifier().getText(), IDENTITY_DATA, ctx); |
| 81 | |
| 82 | YangIdentity identity = getYangIdentityNode(JAVA_GENERATION); |
| 83 | identity.setName(identifier); |
| 84 | |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame^] | 85 | identity.setLineNumber(ctx.getStart().getLine()); |
| 86 | identity.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 87 | identity.setFileName(listener.getFileName()); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 88 | Parsable curData = listener.getParsedDataStack().peek(); |
| 89 | if (curData instanceof YangModule || curData instanceof YangSubModule) { |
| 90 | YangNode curNode = (YangNode) curData; |
| 91 | try { |
| 92 | curNode.addChild(identity); |
| 93 | } catch (DataModelException e) { |
| 94 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 95 | IDENTITY_DATA, ctx.identifier().getText(), ENTRY, e.getMessage())); |
| 96 | } |
| 97 | // Push identity node to the stack. |
| 98 | listener.getParsedDataStack().push(identity); |
| 99 | } else { |
| 100 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA, |
| 101 | ctx.identifier().getText(), ENTRY)); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Performs validations and update the data model tree when parser exits from grammar |
| 108 | * rule (identity). |
| 109 | * |
| 110 | * @param listener Listener's object |
| 111 | * @param ctx context object of the grammar rule |
| 112 | */ |
| 113 | public static void processIdentityExit(TreeWalkListener listener, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 114 | GeneratedYangParser.IdentityStatementContext ctx) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 115 | |
| 116 | // Check for stack to be non empty. |
| 117 | checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), EXIT); |
| 118 | |
| 119 | Parsable parsableType = listener.getParsedDataStack().pop(); |
| 120 | if (!(parsableType instanceof YangIdentity)) { |
| 121 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA, |
| 122 | ctx.identifier().getText(), EXIT)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |