Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +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 | |
| 19 | import org.onosproject.yangutils.datamodel.YangNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangRpc; |
| 21 | import org.onosproject.yangutils.datamodel.YangModule; |
| 22 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 24 | import org.onosproject.yangutils.parser.Parsable; |
| 25 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 26 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 27 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 28 | |
| 29 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 30 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangRpcNode; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
| 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds; |
| 41 | import static org.onosproject.yangutils.utils.YangConstructType.RPC_DATA; |
| 42 | import static org.onosproject.yangutils.utils.YangConstructType.INPUT_DATA; |
| 43 | import static org.onosproject.yangutils.utils.YangConstructType.OUTPUT_DATA; |
| 44 | import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA; |
| 45 | import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA; |
| 46 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
| 47 | import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; |
| 48 | import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; |
| 49 | |
| 50 | /* |
| 51 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 52 | * |
| 53 | * ABNF grammar as per RFC6020 |
| 54 | * rpc-stmt = rpc-keyword sep identifier-arg-str optsep |
| 55 | * (";" / |
| 56 | * "{" stmtsep |
| 57 | * ;; these stmts can appear in any order |
| 58 | * *(if-feature-stmt stmtsep) |
| 59 | * [status-stmt stmtsep] |
| 60 | * [description-stmt stmtsep] |
| 61 | * [reference-stmt stmtsep] |
| 62 | * *((typedef-stmt / |
| 63 | * grouping-stmt) stmtsep) |
| 64 | * [input-stmt stmtsep] |
| 65 | * [output-stmt stmtsep] |
| 66 | * "}") |
| 67 | * |
| 68 | * ANTLR grammar rule |
| 69 | * rpcStatement : RPC_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE (ifFeatureStatement | statusStatement |
| 70 | * | descriptionStatement | referenceStatement | typedefStatement | groupingStatement | inputStatement |
| 71 | * | outputStatement)* RIGHT_CURLY_BRACE); |
| 72 | */ |
| 73 | |
| 74 | /** |
| 75 | * Implements listener based call back function corresponding to the "rpc" |
| 76 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 77 | */ |
| 78 | public final class RpcListener { |
| 79 | |
| 80 | /** |
| 81 | * Creates a new rpc listener. |
| 82 | */ |
| 83 | private RpcListener() { |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * It is called when parser receives an input matching the grammar rule |
| 88 | * (rpc), performs validation and updates the data model tree. |
| 89 | * |
| 90 | * @param listener listener's object |
| 91 | * @param ctx context object of the grammar rule |
| 92 | */ |
| 93 | public static void processRpcEntry(TreeWalkListener listener, |
| 94 | GeneratedYangParser.RpcStatementContext ctx) { |
| 95 | |
| 96 | // Check for stack to be non empty. |
| 97 | checkStackIsNotEmpty(listener, MISSING_HOLDER, RPC_DATA, ctx.identifier().getText(), ENTRY); |
| 98 | |
| 99 | String identifier = getValidIdentifier(ctx.identifier().getText(), RPC_DATA, ctx); |
| 100 | |
| 101 | // Validate sub statement cardinality. |
| 102 | validateSubStatementsCardinality(ctx); |
| 103 | |
| 104 | // Check for identifier collision |
| 105 | int line = ctx.getStart().getLine(); |
| 106 | int charPositionInLine = ctx.getStart().getCharPositionInLine(); |
| 107 | detectCollidingChildUtil(listener, line, charPositionInLine, identifier, RPC_DATA); |
| 108 | |
| 109 | Parsable curData = listener.getParsedDataStack().peek(); |
| 110 | if (curData instanceof YangModule || curData instanceof YangSubModule) { |
| 111 | |
| 112 | YangNode curNode = (YangNode) curData; |
| 113 | YangRpc yangRpc = getYangRpcNode(JAVA_GENERATION); |
| 114 | yangRpc.setName(identifier); |
| 115 | try { |
| 116 | curNode.addChild(yangRpc); |
| 117 | } catch (DataModelException e) { |
| 118 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 119 | RPC_DATA, ctx.identifier().getText(), ENTRY, e.getMessage())); |
| 120 | } |
| 121 | listener.getParsedDataStack().push(yangRpc); |
| 122 | } else { |
| 123 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RPC_DATA, |
| 124 | ctx.identifier().getText(), ENTRY)); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * It is called when parser exits from grammar rule (rpc), it perform |
| 130 | * validations and updates the data model tree. |
| 131 | * |
| 132 | * @param listener listener's object |
| 133 | * @param ctx context object of the grammar rule |
| 134 | */ |
| 135 | public static void processRpcExit(TreeWalkListener listener, |
| 136 | GeneratedYangParser.RpcStatementContext ctx) { |
| 137 | |
| 138 | //Check for stack to be non empty. |
| 139 | checkStackIsNotEmpty(listener, MISSING_HOLDER, RPC_DATA, ctx.identifier().getText(), EXIT); |
| 140 | |
| 141 | if (!(listener.getParsedDataStack().peek() instanceof YangRpc)) { |
| 142 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, RPC_DATA, |
| 143 | ctx.identifier().getText(), EXIT)); |
| 144 | } |
| 145 | listener.getParsedDataStack().pop(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Validates the cardinality of rpc sub-statements as per grammar. |
| 150 | * |
| 151 | * @param ctx context object of the grammar rule |
| 152 | */ |
| 153 | private static void validateSubStatementsCardinality(GeneratedYangParser.RpcStatementContext ctx) { |
| 154 | |
| 155 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, RPC_DATA, ctx.identifier().getText()); |
| 156 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, RPC_DATA, ctx.identifier().getText()); |
| 157 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, RPC_DATA, ctx.identifier().getText()); |
| 158 | validateCardinalityMaxOne(ctx.inputStatement(), INPUT_DATA, RPC_DATA, ctx.identifier().getText()); |
| 159 | validateCardinalityMaxOne(ctx.outputStatement(), OUTPUT_DATA, RPC_DATA, ctx.identifier().getText()); |
| 160 | validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA, |
| 161 | RPC_DATA, ctx.identifier().getText()); |
| 162 | } |
| 163 | |
| 164 | } |