Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +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 | /* |
| 20 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 21 | * |
| 22 | * ABNF grammar as per RFC6020 |
| 23 | * type-body-stmts = numerical-restrictions / |
| 24 | * decimal64-specification / |
| 25 | * string-restrictions / |
| 26 | * enum-specification / |
| 27 | * leafref-specification / |
| 28 | * identityref-specification / |
| 29 | * instance-identifier-specification / |
| 30 | * bits-specification / |
| 31 | * union-specification |
| 32 | * |
| 33 | * enum-specification = 1*(enum-stmt stmtsep) |
| 34 | * |
| 35 | * ANTLR grammar rule |
| 36 | * |
| 37 | * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification |
| 38 | * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification |
| 39 | * | bitsSpecification | unionSpecification; |
| 40 | * |
| 41 | * enumSpecification : enumStatement+; |
| 42 | */ |
| 43 | |
| 44 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 45 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 46 | import org.onosproject.yangutils.datamodel.YangLeafList; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 47 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 48 | import org.onosproject.yangutils.datamodel.YangType; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 49 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 50 | import org.onosproject.yangutils.datamodel.YangUnion; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 51 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 52 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 53 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 54 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 55 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 56 | import org.onosproject.yangutils.utils.YangConstructType; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 57 | |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 59 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangEnumerationNode; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 61 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 64 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 65 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 66 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.utils.YangConstructType.ENUMERATION_DATA; |
| 70 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 71 | |
| 72 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 73 | * Represents listener based call back function corresponding to the |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 74 | * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 75 | * in RFC 6020. |
| 76 | */ |
| 77 | public final class EnumerationListener { |
| 78 | |
| 79 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 80 | * Suffix to be used while creating enumeration class. |
| 81 | */ |
| 82 | private static final String ENUMERATION_CLASS_SUFFIX = "_enum"; |
| 83 | |
| 84 | /** |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 85 | * Creates a new enumeration listener. |
| 86 | */ |
| 87 | private EnumerationListener() { |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * It is called when parser enters grammar rule (enumeration), it perform |
| 92 | * validations and updates the data model tree. |
| 93 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 94 | * @param listener listener's object |
| 95 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 96 | */ |
| 97 | public static void processEnumerationEntry(TreeWalkListener listener, |
| 98 | GeneratedYangParser.EnumSpecificationContext ctx) { |
| 99 | |
| 100 | // Check for stack to be non empty. |
| 101 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY); |
| 102 | |
| 103 | if (listener.getParsedDataStack().peek() instanceof YangType) { |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 104 | YangEnumeration enumerationNode = getYangEnumerationNode(JAVA_GENERATION); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 105 | Parsable typeData = listener.getParsedDataStack().pop(); |
| 106 | |
| 107 | // Check for stack to be non empty. |
| 108 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY); |
| 109 | |
| 110 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 111 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 112 | switch (tmpData.getYangConstructType()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 113 | case LEAF_DATA: |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 114 | // Set the name of enumeration same as leaf. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 115 | enumerationNode.setName(((YangLeaf) tmpData).getName() + ENUMERATION_CLASS_SUFFIX); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 116 | // Pop the stack entry to obtain the parent YANG node. |
| 117 | Parsable leaf = listener.getParsedDataStack().pop(); |
| 118 | // Add the enumeration node to the parent holder of leaf. |
| 119 | addChildToParentNode(listener, enumerationNode); |
| 120 | // Push the popped entry back to the stack. |
| 121 | listener.getParsedDataStack().push(leaf); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 122 | break; |
| 123 | case LEAF_LIST_DATA: |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 124 | // Set the name of enumeration same as leaf list. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 125 | enumerationNode.setName(((YangLeafList) tmpData).getName() + ENUMERATION_CLASS_SUFFIX); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 126 | // Pop the stack entry to obtain the parent YANG node. |
| 127 | Parsable leafList = listener.getParsedDataStack().pop(); |
| 128 | // Add the enumeration node to the parent holder of leaf. |
| 129 | addChildToParentNode(listener, enumerationNode); |
| 130 | // Push the popped entry back to the stack. |
| 131 | listener.getParsedDataStack().push(leafList); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 132 | break; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 133 | case UNION_DATA: |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 134 | YangUnion yangUnion = (YangUnion) tmpData; |
| 135 | /* |
| 136 | * In case parent of enumeration is a union, name of the |
| 137 | * enumeration is parent union name suffixed with running |
| 138 | * integer number, this is done because under union there |
| 139 | * could be multiple child union types. |
| 140 | */ |
| 141 | enumerationNode.setName(yangUnion.getName() + yangUnion.getChildUnionNumber() |
| 142 | + ENUMERATION_CLASS_SUFFIX); |
| 143 | // Increment the running number. |
| 144 | yangUnion.setChildUnionNumber(yangUnion.getChildUnionNumber() + 1); |
| 145 | // Add union as a child to parent union. |
| 146 | addChildToParentNode(listener, enumerationNode); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 147 | break; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 148 | case TYPEDEF_DATA: |
| 149 | YangTypeDef typeDef = (YangTypeDef) tmpData; |
| 150 | // Set the name of enumeration same as typedef name. |
| 151 | enumerationNode.setName(typeDef.getName() + ENUMERATION_CLASS_SUFFIX); |
| 152 | // Add enumeration as a child to parent type def. |
| 153 | addChildToParentNode(listener, enumerationNode); |
| 154 | break; |
| 155 | // TODO deviate. |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 156 | default: |
| 157 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 158 | ((YangType<?>) typeData).getDataTypeName(), ENTRY)); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 159 | } |
| 160 | listener.getParsedDataStack().push(typeData); |
| 161 | listener.getParsedDataStack().push(enumerationNode); |
| 162 | } else { |
| 163 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * It is called when parser exits from grammar rule (enumeration), it |
| 169 | * perform validations and update the data model tree. |
| 170 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 171 | * @param listener Listener's object |
| 172 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 173 | */ |
| 174 | public static void processEnumerationExit(TreeWalkListener listener, |
| 175 | GeneratedYangParser.EnumSpecificationContext ctx) { |
| 176 | |
| 177 | // Check for stack to be non empty. |
| 178 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT); |
| 179 | |
| 180 | Parsable tmpEnumerationNode = listener.getParsedDataStack().peek(); |
| 181 | if (tmpEnumerationNode instanceof YangEnumeration) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 182 | YangEnumeration enumerationNode = (YangEnumeration) tmpEnumerationNode; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 183 | listener.getParsedDataStack().pop(); |
| 184 | |
| 185 | // Check for stack to be non empty. |
| 186 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT); |
| 187 | |
| 188 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 189 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 190 | case TYPE_DATA: { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 191 | YangType<YangEnumeration> typeNode = (YangType<YangEnumeration>) tmpNode; |
| 192 | typeNode.setDataTypeExtendedInfo(enumerationNode); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | default: |
| 196 | throw new ParserException( |
| 197 | constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT)); |
| 198 | } |
| 199 | } else { |
| 200 | throw new ParserException( |
| 201 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT)); |
| 202 | } |
| 203 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Adds the enumeration node to the parent holder. |
| 207 | * |
| 208 | * @param listener listener's object |
| 209 | * @param enumerationNode enumeration node which needs to be added to parent |
| 210 | */ |
| 211 | private static void addChildToParentNode(TreeWalkListener listener, YangEnumeration enumerationNode) { |
| 212 | if (!(listener.getParsedDataStack().peek() instanceof YangNode)) { |
| 213 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, |
| 214 | "", ENTRY)); |
| 215 | } else { |
| 216 | YangNode curNode = (YangNode) listener.getParsedDataStack().peek(); |
| 217 | try { |
| 218 | curNode.addChild(enumerationNode); |
| 219 | } catch (DataModelException e) { |
| 220 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 221 | YangConstructType.ENUMERATION_DATA, "", ENTRY, e.getMessage())); |
| 222 | } |
| 223 | } |
| 224 | } |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 225 | } |