Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 20 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 22 | import org.onosproject.yangutils.datamodel.YangType; |
| 23 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 24 | import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 35 | |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 36 | /* |
| 37 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 38 | * |
| 39 | * ABNF grammar as per RFC6020 |
| 40 | * type-stmt = type-keyword sep identifier-ref-arg-str optsep |
| 41 | * (";" / |
| 42 | * "{" stmtsep |
| 43 | * type-body-stmts |
| 44 | * "}") |
| 45 | * |
| 46 | * ANTLR grammar rule |
| 47 | * typeStatement : TYPE_KEYWORD string (STMTEND | LEFT_CURLY_BRACE typeBodyStatements RIGHT_CURLY_BRACE); |
| 48 | */ |
| 49 | |
| 50 | /** |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 51 | * Implements listener based call back function corresponding to the "type" rule |
| 52 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 53 | */ |
| 54 | public final class TypeListener { |
| 55 | |
| 56 | /** |
| 57 | * Creates a new type listener. |
| 58 | */ |
| 59 | private TypeListener() { |
| 60 | } |
| 61 | |
| 62 | /** |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 63 | * It is called when parser receives an input matching the grammar rule |
| 64 | * (type), performs validation and updates the data model tree. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 65 | * |
| 66 | * @param listener listener's object. |
| 67 | * @param ctx context object of the grammar rule. |
| 68 | */ |
| 69 | public static void processTypeEntry(TreeWalkListener listener, |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 70 | GeneratedYangParser.TypeStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 71 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 72 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 73 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 74 | |
| 75 | YangType type = new YangType(); |
| 76 | YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText()); |
| 77 | type.setDataTypeName(ctx.string().getText()); |
| 78 | type.setDataType(yangDataTypes); |
| 79 | |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 80 | listener.getParsedDataStack().push(type); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * It is called when parser exits from grammar rule (type), it perform |
| 85 | * validations and update the data model tree. |
| 86 | * |
| 87 | * @param listener Listener's object. |
| 88 | * @param ctx context object of the grammar rule. |
| 89 | */ |
| 90 | public static void processTypeExit(TreeWalkListener listener, |
| 91 | GeneratedYangParser.TypeStatementContext ctx) { |
| 92 | |
| 93 | // Check for stack to be non empty. |
| 94 | checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); |
| 95 | |
| 96 | Parsable type = listener.getParsedDataStack().pop(); |
| 97 | if (!(type instanceof YangType)) { |
| 98 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
| 99 | ctx.string().getText(), EXIT)); |
| 100 | } |
| 101 | |
| 102 | // Check for stack to be non empty. |
| 103 | checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), EXIT); |
| 104 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 105 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 106 | switch (tmpData.getParsableDataType()) { |
| 107 | case LEAF_DATA: |
| 108 | YangLeaf leaf = (YangLeaf) tmpData; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 109 | leaf.setDataType((YangType) type); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 110 | break; |
| 111 | case LEAF_LIST_DATA: |
| 112 | YangLeafList leafList = (YangLeafList) tmpData; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 113 | leafList.setDataType((YangType) type); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 114 | break; |
| 115 | case TYPEDEF_DATA: //TODO |
| 116 | break; |
| 117 | default: |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 118 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 119 | ctx.string().getText(), EXIT)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 120 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 121 | } |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame^] | 122 | } |