Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 25bf4d0 | 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 | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 20 | |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 21 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangAugment; |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangModule; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | cc1cdab | 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 | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 29 | import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl; |
Vidyashree Rama | 25bf4d0 | 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 | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 34 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | 96dfef0 | 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 | 25bf4d0 | 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 | b1170bd | 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 | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Vidyashree Rama | 25bf4d0 | 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 | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 53 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 55 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
| 56 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangAugmentNode; |
Vidyashree Rama | 25bf4d0 | 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 | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 81 | * Represents listener based call back function corresponding to the "augment" |
Vidyashree Rama | 25bf4d0 | 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 |
| 97 | * @param ctx context object of the grammar rule |
| 98 | */ |
| 99 | public static void processAugmentEntry(TreeWalkListener listener, |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 100 | GeneratedYangParser.AugmentStatementContext ctx) { |
Vidyashree Rama | 25bf4d0 | 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 | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 106 | List<YangAtomicPath> targetNodes = getValidAbsoluteSchemaNodeId(ctx.augment().getText(), |
Vidyashree Rama | 25bf4d0 | 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(); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 118 | if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangUses) { |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 119 | YangNode curNode = (YangNode) curData; |
| 120 | YangAugment yangAugment = getYangAugmentNode(JAVA_GENERATION); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 121 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 122 | //validateTargetNodePath(targetNodes, curNode, ctx); |
| 123 | // TODO: handle in linker. |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 124 | |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 125 | yangAugment.setTargetNode(targetNodes); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 126 | yangAugment.setName(removeQuotesAndHandleConcat(ctx.augment().getText())); |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 127 | |
Vidyashree Rama | 25bf4d0 | 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); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 135 | |
| 136 | // Add resolution information to the list |
| 137 | YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangAugment>(yangAugment, |
| 138 | curNode, line, |
| 139 | charPositionInLine); |
| 140 | addToResolutionList(resolutionInfo, ctx); |
| 141 | |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 142 | } else { |
| 143 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, AUGMENT_DATA, |
| 144 | ctx.augment().getText(), ENTRY)); |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * It is called when parser exits from grammar rule (augment), it perform |
| 151 | * validations and updates the data model tree. |
| 152 | * |
| 153 | * @param listener listener's object |
| 154 | * @param ctx context object of the grammar rule |
| 155 | */ |
| 156 | public static void processAugmentExit(TreeWalkListener listener, |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 157 | GeneratedYangParser.AugmentStatementContext ctx) { |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 158 | |
| 159 | //Check for stack to be non empty. |
| 160 | checkStackIsNotEmpty(listener, MISSING_HOLDER, AUGMENT_DATA, ctx.augment().getText(), EXIT); |
| 161 | |
| 162 | if (!(listener.getParsedDataStack().peek() instanceof YangAugment)) { |
| 163 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, AUGMENT_DATA, |
| 164 | ctx.augment().getText(), EXIT)); |
| 165 | } |
| 166 | listener.getParsedDataStack().pop(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Validates the cardinality of augment sub-statements as per grammar. |
| 171 | * |
| 172 | * @param ctx context object of the grammar rule |
| 173 | */ |
| 174 | private static void validateSubStatementsCardinality(GeneratedYangParser.AugmentStatementContext ctx) { |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 175 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 176 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 177 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 178 | validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 179 | validateMutuallyExclusiveChilds(ctx.dataDefStatement(), DATA_DEF_DATA, ctx.caseStatement(), |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 180 | CASE_DATA, AUGMENT_DATA, ctx.augment().getText()); |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 181 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 182 | |
| 183 | /** |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 184 | * Add to resolution list. |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 185 | * |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 186 | * @param resolutionInfo resolution information. |
| 187 | * @param ctx context object of the grammar rule |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 188 | */ |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 189 | private static void addToResolutionList(YangResolutionInfoImpl<YangAugment> resolutionInfo, |
| 190 | GeneratedYangParser.AugmentStatementContext ctx) { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 191 | |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame^] | 192 | try { |
| 193 | addResolutionInfo(resolutionInfo); |
| 194 | } catch (DataModelException e) { |
| 195 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 196 | AUGMENT_DATA, ctx.augment().getText(), EXIT, e.getMessage())); |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 197 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 198 | } |
Vidyashree Rama | 25bf4d0 | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 199 | } |