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