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; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 48 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.YangConstructType.AUGMENT_DATA; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 51 | import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA; |
| 52 | import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; |
| 53 | import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; |
| 54 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
| 55 | import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA; |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 59 | * |
| 60 | * ABNF grammar as per RFC6020 |
| 61 | * augment-stmt = augment-keyword sep augment-arg-str optsep |
| 62 | * "{" stmtsep |
| 63 | * ;; these stmts can appear in any order |
| 64 | * [when-stmt stmtsep] |
| 65 | * *(if-feature-stmt stmtsep) |
| 66 | * [status-stmt stmtsep] |
| 67 | * [description-stmt stmtsep] |
| 68 | * [reference-stmt stmtsep] |
| 69 | * 1*((data-def-stmt stmtsep) / |
| 70 | * (case-stmt stmtsep)) |
| 71 | * "}" |
| 72 | * |
| 73 | * ANTLR grammar rule |
| 74 | * augmentStatement : AUGMENT_KEYWORD augment LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | statusStatement |
| 75 | * | descriptionStatement | referenceStatement | dataDefStatement | caseStatement)* RIGHT_CURLY_BRACE; |
| 76 | */ |
| 77 | |
| 78 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 79 | * Represents listener based call back function corresponding to the "augment" |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 80 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 81 | */ |
| 82 | public final class AugmentListener { |
| 83 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 84 | private static final String AUGMENTED = "Augmented"; |
| 85 | |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 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, |
| 100 | GeneratedYangParser.AugmentStatementContext ctx) { |
| 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 |
| 106 | List<YangNodeIdentifier> targetNodes = getValidAbsoluteSchemaNodeId(ctx.augment().getText(), |
| 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 | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 118 | if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangUses) { |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 119 | |
| 120 | YangNode curNode = (YangNode) curData; |
| 121 | YangAugment yangAugment = getYangAugmentNode(JAVA_GENERATION); |
| 122 | yangAugment.setTargetNode(targetNodes); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 123 | yangAugment.setName(getValidNameForAugment(targetNodes)); |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 124 | try { |
| 125 | curNode.addChild(yangAugment); |
| 126 | } catch (DataModelException e) { |
| 127 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 128 | AUGMENT_DATA, ctx.augment().getText(), ENTRY, e.getMessage())); |
| 129 | } |
| 130 | listener.getParsedDataStack().push(yangAugment); |
| 131 | } else { |
| 132 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, AUGMENT_DATA, |
| 133 | ctx.augment().getText(), ENTRY)); |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * It is called when parser exits from grammar rule (augment), it perform |
| 140 | * validations and updates the data model tree. |
| 141 | * |
| 142 | * @param listener listener's object |
| 143 | * @param ctx context object of the grammar rule |
| 144 | */ |
| 145 | public static void processAugmentExit(TreeWalkListener listener, |
| 146 | GeneratedYangParser.AugmentStatementContext ctx) { |
| 147 | |
| 148 | //Check for stack to be non empty. |
| 149 | checkStackIsNotEmpty(listener, MISSING_HOLDER, AUGMENT_DATA, ctx.augment().getText(), EXIT); |
| 150 | |
| 151 | if (!(listener.getParsedDataStack().peek() instanceof YangAugment)) { |
| 152 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, AUGMENT_DATA, |
| 153 | ctx.augment().getText(), EXIT)); |
| 154 | } |
| 155 | listener.getParsedDataStack().pop(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Validates the cardinality of augment sub-statements as per grammar. |
| 160 | * |
| 161 | * @param ctx context object of the grammar rule |
| 162 | */ |
| 163 | private static void validateSubStatementsCardinality(GeneratedYangParser.AugmentStatementContext ctx) { |
| 164 | |
| 165 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 166 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 167 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 168 | validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 169 | validateMutuallyExclusiveChilds(ctx.dataDefStatement(), DATA_DEF_DATA, ctx.caseStatement(), |
| 170 | CASE_DATA, AUGMENT_DATA, ctx.augment().getText()); |
| 171 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 172 | |
| 173 | /** |
| 174 | * Returns a name identifier for augment. |
| 175 | * |
| 176 | * @param targetNode list of target nodes |
| 177 | * @return name identifier |
| 178 | */ |
| 179 | private static String getValidNameForAugment(List<YangNodeIdentifier> targetNodes) { |
| 180 | String name = ""; |
| 181 | YangNodeIdentifier nodeId = targetNodes.get(targetNodes.size() - 1); |
| 182 | |
| 183 | if (nodeId.getPrefix() != null) { |
| 184 | name = AUGMENTED + getCaptialCase(nodeId.getPrefix()) + getCaptialCase(nodeId.getName()); |
| 185 | } else { |
| 186 | //TODO: name = name + ((HasAugmentation)getParentNode()).getAugmentPrefix(nodeId); |
| 187 | } |
| 188 | return name; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Validates for the child nodes of augment node. |
| 193 | */ |
| 194 | private static void validateForChildNodes() { |
| 195 | //TODO: implement with linker. |
| 196 | return; |
| 197 | } |
Vidyashree Rama | e2bd9ba | 2016-03-29 14:37:02 +0530 | [diff] [blame] | 198 | } |