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