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; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 18 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 19 | import org.onosproject.yangutils.datamodel.YangNode; |
| 20 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 22 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 23 | import org.onosproject.yangutils.parser.ParsableDataType; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 27 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 29 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 30 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 31 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 35 | * |
| 36 | * ABNF grammar as per RFC6020 |
| 37 | * container-stmt = container-keyword sep identifier-arg-str optsep |
| 38 | * (";" / |
| 39 | * "{" stmtsep |
| 40 | * ;; these stmts can appear in any order |
| 41 | * [when-stmt stmtsep] |
| 42 | * *(if-feature-stmt stmtsep) |
| 43 | * *(must-stmt stmtsep) |
| 44 | * [presence-stmt stmtsep] |
| 45 | * [config-stmt stmtsep] |
| 46 | * [status-stmt stmtsep] |
| 47 | * [description-stmt stmtsep] |
| 48 | * [reference-stmt stmtsep] |
| 49 | * *((typedef-stmt / |
| 50 | * grouping-stmt) stmtsep) |
| 51 | * *(data-def-stmt stmtsep) |
| 52 | * "}") |
| 53 | * |
| 54 | * ANTLR grammar rule |
| 55 | * containerStatement : CONTAINER_KEYWORD IDENTIFIER |
| 56 | * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement | |
| 57 | * presenceStatement | configStatement | statusStatement | descriptionStatement | |
| 58 | * referenceStatement | typedefStatement | groupingStatement |
| 59 | * | dataDefStatement)* RIGHT_CURLY_BRACE); |
| 60 | */ |
| 61 | |
| 62 | /** |
| 63 | * Implements listener based call back function corresponding to the "container" |
| 64 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 65 | */ |
| 66 | public final class ContainerListener { |
| 67 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 68 | private static ParsableDataType yangConstruct; |
| 69 | |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 70 | /** |
| 71 | * Creates a new container listener. |
| 72 | */ |
| 73 | private ContainerListener() { |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * It is called when parser receives an input matching the grammar |
| 78 | * rule (container), performs validation and updates the data model |
| 79 | * tree. |
| 80 | * |
| 81 | * @param listener listener's object. |
| 82 | * @param ctx context object of the grammar rule. |
| 83 | */ |
| 84 | public static void processContainerEntry(TreeWalkListener listener, |
| 85 | GeneratedYangParser.ContainerStatementContext ctx) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 86 | |
| 87 | // Check for stack to be non empty. |
| 88 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 89 | ParsableDataType.CONTAINER_DATA, String.valueOf(ctx.IDENTIFIER().getText()), |
| 90 | ListenerErrorLocation.ENTRY); |
| 91 | |
| 92 | boolean result = validateSubStatementsCardinality(ctx); |
| 93 | if (!result) { |
| 94 | throw new ParserException(ListenerErrorMessageConstruction |
| 95 | .constructListenerErrorMessage(ListenerErrorType.INVALID_CARDINALITY, |
| 96 | yangConstruct, "", ListenerErrorLocation.ENTRY)); |
| 97 | } |
| 98 | |
| 99 | YangContainer container = new YangContainer(); |
| 100 | container.setName(ctx.IDENTIFIER().getText()); |
| 101 | |
| 102 | Parsable curData = listener.getParsedDataStack().peek(); |
| 103 | |
| 104 | if (curData instanceof YangNode) { |
| 105 | YangNode curNode = (YangNode) curData; |
| 106 | try { |
| 107 | curNode.addChild(container); |
| 108 | } catch (DataModelException e) { |
| 109 | throw new ParserException(ListenerErrorMessageConstruction |
| 110 | .constructExtendedListenerErrorMessage(ListenerErrorType.UNHANDLED_PARSED_DATA, |
| 111 | ParsableDataType.CONTAINER_DATA, |
| 112 | String.valueOf(ctx.IDENTIFIER().getText()), |
| 113 | ListenerErrorLocation.ENTRY, |
| 114 | e.getMessage())); |
| 115 | } |
| 116 | listener.getParsedDataStack().push(container); |
| 117 | } else { |
| 118 | throw new ParserException(ListenerErrorMessageConstruction |
| 119 | .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, |
| 120 | ParsableDataType.CONTAINER_DATA, |
| 121 | String.valueOf(ctx.IDENTIFIER().getText()), |
| 122 | ListenerErrorLocation.ENTRY)); |
| 123 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * It is called when parser exits from grammar rule (container), it perform |
| 128 | * validations and updates the data model tree. |
| 129 | * |
| 130 | * @param listener listener's object. |
| 131 | * @param ctx context object of the grammar rule. |
| 132 | */ |
| 133 | public static void processContainerExit(TreeWalkListener listener, |
| 134 | GeneratedYangParser.ContainerStatementContext ctx) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame^] | 135 | |
| 136 | // Check for stack to be non empty. |
| 137 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 138 | ParsableDataType.CONTAINER_DATA, String.valueOf(ctx.IDENTIFIER().getText()), |
| 139 | ListenerErrorLocation.EXIT); |
| 140 | |
| 141 | if (listener.getParsedDataStack().peek() instanceof YangContainer) { |
| 142 | listener.getParsedDataStack().pop(); |
| 143 | } else { |
| 144 | throw new ParserException(ListenerErrorMessageConstruction |
| 145 | .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, |
| 146 | ParsableDataType.CONTAINER_DATA, |
| 147 | String.valueOf(ctx.IDENTIFIER().getText()), |
| 148 | ListenerErrorLocation.EXIT)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Validates the cardinality of container sub-statements as per grammar. |
| 154 | * |
| 155 | * @param ctx context object of the grammar rule. |
| 156 | * @return true/false validation success or failure. |
| 157 | */ |
| 158 | public static boolean validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) { |
| 159 | |
| 160 | if ((!ctx.presenceStatement().isEmpty()) |
| 161 | && (ctx.presenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 162 | yangConstruct = ParsableDataType.PRESENCE_DATA; |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | if ((!ctx.configStatement().isEmpty()) |
| 167 | && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 168 | yangConstruct = ParsableDataType.CONFIG_DATA; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | if ((!ctx.descriptionStatement().isEmpty()) |
| 173 | && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 174 | yangConstruct = ParsableDataType.DESCRIPTION_DATA; |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if ((!ctx.referenceStatement().isEmpty()) |
| 179 | && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 180 | yangConstruct = ParsableDataType.REFERENCE_DATA; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if ((!ctx.statusStatement().isEmpty()) |
| 185 | && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 186 | yangConstruct = ParsableDataType.STATUS_DATA; |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return true; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 191 | } |
| 192 | } |