Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +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 | 0520c7c | 2016-02-21 02:56:46 +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 | * typedef-stmt = typedef-keyword sep identifier-arg-str optsep |
| 23 | * "{" stmtsep |
| 24 | * ;; these stmts can appear in any order |
| 25 | * type-stmt stmtsep |
| 26 | * [units-stmt stmtsep] |
| 27 | * [default-stmt stmtsep] |
| 28 | * [status-stmt stmtsep] |
| 29 | * [description-stmt stmtsep] |
| 30 | * [reference-stmt stmtsep] |
| 31 | * "}" |
| 32 | * default-stmt = default-keyword sep string stmtend |
| 33 | |
| 34 | * |
| 35 | * ANTLR grammar rule |
| 36 | * typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE |
| 37 | * (typeStatement | unitsStatement | defaultStatement | statusStatement |
| 38 | * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE; |
| 39 | * defaultStatement : DEFAULT_KEYWORD string STMTEND; |
| 40 | */ |
| 41 | |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 42 | import org.onosproject.yangutils.datamodel.YangChoice; |
| 43 | import org.onosproject.yangutils.datamodel.YangLeaf; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 44 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
| 45 | import org.onosproject.yangutils.parser.Parsable; |
| 46 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 47 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 48 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 49 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 51 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.utils.YangConstructType.DEFAULT_DATA; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 56 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 57 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 58 | * Represents listener for default YANG statement. |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 59 | */ |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 60 | public final class DefaultListener { |
| 61 | |
| 62 | /** |
| 63 | * Creates a new default listener. |
| 64 | */ |
| 65 | private DefaultListener() { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * It is called when parser enters grammar rule (default), it perform |
| 70 | * validations and updates the data model tree. |
| 71 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 72 | * @param listener listener's object |
| 73 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 74 | */ |
| 75 | public static void processDefaultEntry(TreeWalkListener listener, |
| 76 | GeneratedYangParser.DefaultStatementContext ctx) { |
| 77 | |
| 78 | // Check for stack to be non empty. |
| 79 | checkStackIsNotEmpty(listener, MISSING_HOLDER, DEFAULT_DATA, ctx.string().getText(), ENTRY); |
| 80 | |
| 81 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 82 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 83 | case TYPEDEF_DATA: { |
| 84 | YangTypeDef typeDef = (YangTypeDef) tmpNode; |
| 85 | typeDef.setDefaultValueInString(ctx.string().getText()); |
| 86 | break; |
| 87 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 88 | case LEAF_DATA: { |
| 89 | YangLeaf leaf = (YangLeaf) tmpNode; |
| 90 | leaf.setDefaultValueInString(ctx.string().getText()); |
| 91 | break; |
| 92 | } |
| 93 | case CHOICE_DATA: { |
| 94 | YangChoice choice = (YangChoice) tmpNode; |
| 95 | choice.setDefaultValueInString(ctx.string().getText()); |
| 96 | break; |
| 97 | } |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 98 | default: |
| 99 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, |
| 100 | DEFAULT_DATA, ctx.string().getText(), ENTRY)); |
| 101 | } |
| 102 | } |
| 103 | } |