Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 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 | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 19 | import java.util.Date; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.ResolvableType; |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangModule; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
janani b | cc9ac30 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangRevision; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 30 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MODULE_DATA; |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 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.ListenerErrorType.MISSING_HOLDER; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getCurrentDateForRevision; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangModuleNode; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 47 | * |
| 48 | * ABNF grammar as per RFC6020 |
| 49 | * module-stmt = optsep module-keyword sep identifier-arg-str |
| 50 | * optsep |
| 51 | * "{" stmtsep |
| 52 | * module-header-stmts |
| 53 | * linkage-stmts |
| 54 | * meta-stmts |
| 55 | * revision-stmts |
| 56 | * body-stmts |
| 57 | * "}" optsep |
| 58 | * |
| 59 | * ANTLR grammar rule |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 60 | * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 64 | * Represents listener based call back function corresponding to the "module" |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 65 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 66 | */ |
| 67 | public final class ModuleListener { |
| 68 | |
| 69 | /** |
| 70 | * Creates a new module listener. |
| 71 | */ |
| 72 | private ModuleListener() { |
| 73 | } |
| 74 | |
| 75 | /** |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 76 | * It is called when parser receives an input matching the grammar rule |
| 77 | * (module), perform validations and update the data model tree. |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 78 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 79 | * @param listener Listener's object |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 80 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 81 | */ |
| 82 | public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 83 | |
| 84 | // Check if stack is empty. |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 85 | checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY); |
| 86 | |
| 87 | String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 88 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | YangModule yangModule = getYangModuleNode(JAVA_GENERATION); |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 90 | yangModule.setName(identifier); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 91 | |
Vidyashree Rama | bcd7fba | 2016-03-09 20:41:44 +0530 | [diff] [blame] | 92 | if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 93 | yangModule.setVersion((byte) 1); |
| 94 | } |
| 95 | |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 96 | listener.getParsedDataStack().push(yangModule); |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * It is called when parser exits from grammar rule (module), it perform |
| 101 | * validations and update the data model tree. |
| 102 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 103 | * @param listener Listener's object |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 104 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 105 | */ |
| 106 | public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 107 | |
| 108 | // Check for stack to be non empty. |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 109 | checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 110 | |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 111 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 112 | if (!(tmpNode instanceof YangModule)) { |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 113 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA, |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 114 | ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 115 | } |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 116 | |
| 117 | if (((YangModule) tmpNode).getRevision() == null) { |
| 118 | Date currentDate = getCurrentDateForRevision(); |
| 119 | YangRevision currentRevision = new YangRevision(); |
| 120 | currentRevision.setRevDate(currentDate); |
| 121 | ((YangModule) tmpNode).setRevision(currentRevision); |
| 122 | } |
| 123 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 124 | try { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 125 | ((YangReferenceResolver) listener.getParsedDataStack() |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 126 | .peek()).resolveSelfFileLinking(ResolvableType.YANG_IF_FEATURE); |
| 127 | ((YangReferenceResolver) listener.getParsedDataStack() |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 128 | .peek()).resolveSelfFileLinking(ResolvableType.YANG_USES); |
| 129 | ((YangReferenceResolver) listener.getParsedDataStack() |
| 130 | .peek()).resolveSelfFileLinking(ResolvableType.YANG_DERIVED_DATA_TYPE); |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 131 | ((YangReferenceResolver) listener.getParsedDataStack() |
| 132 | .peek()).resolveSelfFileLinking(ResolvableType.YANG_LEAFREF); |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 133 | ((YangReferenceResolver) listener.getParsedDataStack() |
| 134 | .peek()).resolveSelfFileLinking(ResolvableType.YANG_BASE); |
| 135 | ((YangReferenceResolver) listener.getParsedDataStack() |
| 136 | .peek()).resolveSelfFileLinking(ResolvableType.YANG_IDENTITYREF); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 137 | } catch (DataModelException e) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 138 | LinkerException linkerException = new LinkerException(e.getMessage()); |
| 139 | linkerException.setLine(e.getLineNumber()); |
| 140 | linkerException.setCharPosition(e.getCharPositionInLine()); |
| 141 | throw linkerException; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 142 | } |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 143 | } |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 144 | } |