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