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