Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +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 | 480a47d | 2016-02-26 02:47:39 +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 | * bits-specification = 1*(bit-stmt stmtsep) |
| 34 | * |
| 35 | * ANTLR grammar rule |
| 36 | * |
| 37 | * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification |
| 38 | * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification |
| 39 | * | bitsSpecification | unionSpecification; |
| 40 | * |
| 41 | * bitsSpecification : bitStatement+; |
| 42 | */ |
| 43 | |
| 44 | import org.onosproject.yangutils.datamodel.YangBits; |
| 45 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 46 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 47 | import org.onosproject.yangutils.datamodel.YangType; |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 48 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 49 | import org.onosproject.yangutils.datamodel.YangUnion; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 50 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 51 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 52 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 53 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 54 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BITS_DATA; |
| 56 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 58 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 59 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 60 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 61 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 62 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 63 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 64 | |
| 65 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 66 | * Represents listener based call back function corresponding to the "bits" rule |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 67 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 68 | */ |
| 69 | public final class BitsListener { |
| 70 | |
| 71 | /** |
| 72 | * Creates a new bits listener. |
| 73 | */ |
| 74 | private BitsListener() { |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * It is called when parser enters grammar rule (bits), 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 | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 83 | */ |
| 84 | public static void processBitsEntry(TreeWalkListener listener, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 85 | GeneratedYangParser.BitsSpecificationContext ctx) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 86 | |
| 87 | // Check for stack to be non empty. |
| 88 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY); |
| 89 | |
| 90 | if (listener.getParsedDataStack().peek() instanceof YangType) { |
| 91 | YangBits bitsNode = new YangBits(); |
| 92 | Parsable typeData = listener.getParsedDataStack().pop(); |
| 93 | |
| 94 | // Check for stack to be non empty. |
| 95 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_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 | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 100 | case LEAF_DATA: |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 101 | bitsNode.setBitsName(((YangLeaf) tmpData).getName()); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 102 | break; |
| 103 | case LEAF_LIST_DATA: |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 104 | bitsNode.setBitsName(((YangLeafList) tmpData).getName()); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 105 | break; |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 106 | case TYPEDEF_DATA: |
| 107 | bitsNode.setBitsName(((YangTypeDef) tmpData).getName()); |
| 108 | break; |
| 109 | case UNION_DATA: |
| 110 | bitsNode.setBitsName(((YangUnion) tmpData).getName()); |
| 111 | break; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 112 | // TODO typedef, union, deviate. |
| 113 | default: |
| 114 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 115 | ((YangType<?>) typeData).getDataTypeName(), ENTRY)); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 116 | } |
| 117 | listener.getParsedDataStack().push(typeData); |
| 118 | listener.getParsedDataStack().push(bitsNode); |
| 119 | } else { |
| 120 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", ENTRY)); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * It is called when parser exits from grammar rule (bits), it perform |
| 126 | * validations and update the data model tree. |
| 127 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 128 | * @param listener Listener's object |
| 129 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 130 | */ |
| 131 | public static void processBitsExit(TreeWalkListener listener, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 132 | GeneratedYangParser.BitsSpecificationContext ctx) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 133 | |
| 134 | // Check for stack to be non empty. |
| 135 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT); |
| 136 | |
| 137 | Parsable tmpBitsNode = listener.getParsedDataStack().peek(); |
| 138 | if (tmpBitsNode instanceof YangBits) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 139 | YangBits bitsNode = (YangBits) tmpBitsNode; |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 140 | listener.getParsedDataStack().pop(); |
| 141 | |
| 142 | // Check for stack to be non empty. |
| 143 | checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT); |
| 144 | |
| 145 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 146 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 147 | case TYPE_DATA: { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 148 | YangType<YangBits> typeNode = (YangType<YangBits>) tmpNode; |
| 149 | typeNode.setDataTypeExtendedInfo(bitsNode); |
Gaurav Agrawal | 480a47d | 2016-02-26 02:47:39 +0530 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | default: |
| 153 | throw new ParserException( |
| 154 | constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", EXIT)); |
| 155 | } |
| 156 | } else { |
| 157 | throw new ParserException( |
| 158 | constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BITS_DATA, "", EXIT)); |
| 159 | } |
| 160 | } |
| 161 | } |