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; |
| 47 | import org.onosproject.yangutils.datamodel.YangType; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 48 | import org.onosproject.yangutils.datamodel.YangUnion; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 49 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 50 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 51 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 52 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 53 | |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 55 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 56 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 57 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 58 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 59 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 60 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.utils.YangConstructType.ENUMERATION_DATA; |
| 62 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame^] | 65 | * Represents listener based call back function corresponding to the |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 66 | * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 67 | * in RFC 6020. |
| 68 | */ |
| 69 | public final class EnumerationListener { |
| 70 | |
| 71 | /** |
| 72 | * Creates a new enumeration listener. |
| 73 | */ |
| 74 | private EnumerationListener() { |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * It is called when parser enters grammar rule (enumeration), it perform |
| 79 | * validations and updates the data model tree. |
| 80 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 81 | * @param listener listener's object |
| 82 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 83 | */ |
| 84 | public static void processEnumerationEntry(TreeWalkListener listener, |
| 85 | GeneratedYangParser.EnumSpecificationContext ctx) { |
| 86 | |
| 87 | // Check for stack to be non empty. |
| 88 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY); |
| 89 | |
| 90 | if (listener.getParsedDataStack().peek() instanceof YangType) { |
| 91 | YangEnumeration enumerationNode = new YangEnumeration(); |
| 92 | Parsable typeData = listener.getParsedDataStack().pop(); |
| 93 | |
| 94 | // Check for stack to be non empty. |
| 95 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY); |
| 96 | |
| 97 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 98 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 99 | switch (tmpData.getYangConstructType()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 100 | case LEAF_DATA: |
| 101 | enumerationNode.setEnumerationName(((YangLeaf) tmpData).getLeafName()); |
| 102 | break; |
| 103 | case LEAF_LIST_DATA: |
| 104 | enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName()); |
| 105 | break; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 106 | case UNION_DATA: |
| 107 | enumerationNode.setEnumerationName(((YangUnion) tmpData).getUnionName()); |
| 108 | break; |
| 109 | // TODO typedef, deviate. |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 110 | default: |
| 111 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 112 | ((YangType<?>) typeData).getDataTypeName(), ENTRY)); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 113 | } |
| 114 | listener.getParsedDataStack().push(typeData); |
| 115 | listener.getParsedDataStack().push(enumerationNode); |
| 116 | } else { |
| 117 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * It is called when parser exits from grammar rule (enumeration), it |
| 123 | * perform validations and update the data model tree. |
| 124 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 125 | * @param listener Listener's object |
| 126 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 127 | */ |
| 128 | public static void processEnumerationExit(TreeWalkListener listener, |
| 129 | GeneratedYangParser.EnumSpecificationContext ctx) { |
| 130 | |
| 131 | // Check for stack to be non empty. |
| 132 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT); |
| 133 | |
| 134 | Parsable tmpEnumerationNode = listener.getParsedDataStack().peek(); |
| 135 | if (tmpEnumerationNode instanceof YangEnumeration) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 136 | YangEnumeration enumerationNode = (YangEnumeration) tmpEnumerationNode; |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 137 | listener.getParsedDataStack().pop(); |
| 138 | |
| 139 | // Check for stack to be non empty. |
| 140 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT); |
| 141 | |
| 142 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 143 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 144 | case TYPE_DATA: { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 145 | YangType<YangEnumeration> typeNode = (YangType<YangEnumeration>) tmpNode; |
| 146 | typeNode.setDataTypeExtendedInfo(enumerationNode); |
Gaurav Agrawal | 4ce3acf | 2016-02-25 04:37:05 +0530 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | default: |
| 150 | throw new ParserException( |
| 151 | constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT)); |
| 152 | } |
| 153 | } else { |
| 154 | throw new ParserException( |
| 155 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT)); |
| 156 | } |
| 157 | } |
| 158 | } |