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