Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +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 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 20 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 22 | import org.onosproject.yangutils.datamodel.YangList; |
| 23 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidBooleanValue; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 38 | * |
| 39 | * ABNF grammar as per RFC6020 |
| 40 | * config-stmt = config-keyword sep |
| 41 | * config-arg-str stmtend |
| 42 | * config-arg-str = < a string that matches the rule |
| 43 | * config-arg > |
| 44 | * config-arg = true-keyword / false-keyword |
| 45 | * |
| 46 | * ANTLR grammar rule |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 47 | * configStatement : CONFIG_KEYWORD config STMTEND; |
| 48 | * config : string; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 52 | * Represents listener based call back function corresponding to the "config" |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 53 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 54 | */ |
| 55 | public final class ConfigListener { |
| 56 | |
| 57 | /** |
| 58 | * Creates a new config listener. |
| 59 | */ |
| 60 | private ConfigListener() { |
| 61 | } |
| 62 | |
| 63 | /** |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 64 | * It is called when parser receives an input matching the grammar rule |
| 65 | * (config), performs validation and updates the data model tree. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 66 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 67 | * @param listener listener's object |
| 68 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 69 | */ |
| 70 | public static void processConfigEntry(TreeWalkListener listener, |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 71 | GeneratedYangParser.ConfigStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 72 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 73 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 74 | checkStackIsNotEmpty(listener, MISSING_HOLDER, CONFIG_DATA, "", ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 75 | |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 76 | boolean isConfig = getValidBooleanValue(ctx.config().getText(), CONFIG_DATA, ctx); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 77 | |
| 78 | Parsable tmpData = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 79 | switch (tmpData.getYangConstructType()) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 80 | case LEAF_DATA: |
| 81 | YangLeaf leaf = (YangLeaf) tmpData; |
| 82 | leaf.setConfig(isConfig); |
| 83 | break; |
| 84 | case CONTAINER_DATA: |
| 85 | YangContainer container = (YangContainer) tmpData; |
| 86 | container.setConfig(isConfig); |
| 87 | break; |
| 88 | case LEAF_LIST_DATA: |
| 89 | YangLeafList leafList = (YangLeafList) tmpData; |
| 90 | leafList.setConfig(isConfig); |
| 91 | break; |
| 92 | case LIST_DATA: |
| 93 | YangList yangList = (YangList) tmpData; |
| 94 | yangList.setConfig(isConfig); |
| 95 | break; |
| 96 | case CHOICE_DATA: // TODO |
| 97 | break; |
| 98 | default: |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 99 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONFIG_DATA, "", ENTRY)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 100 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 101 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 102 | } |