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