Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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.YangList; |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 25 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.YangConstructType.KEY_DATA; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +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.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
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 | * key-stmt = key-keyword sep key-arg-str stmtend |
| 41 | * |
| 42 | * ANTLR grammar rule |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 43 | * keyStatement : KEY_KEYWORD key STMTEND; |
| 44 | * key : string; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * Implements listener based call back function corresponding to the "key" |
| 49 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 50 | */ |
| 51 | public final class KeyListener { |
| 52 | |
| 53 | /** |
| 54 | * Creates a new key listener. |
| 55 | */ |
| 56 | private KeyListener() { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * It is called when parser receives an input matching the grammar |
| 61 | * rule (key), perform validations and updates the data model |
| 62 | * tree. |
| 63 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 64 | * @param listener listener's object |
| 65 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 66 | */ |
| 67 | public static void processKeyEntry(TreeWalkListener listener, |
| 68 | GeneratedYangParser.KeyStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 69 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 70 | // Check for stack to be non empty. |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 71 | checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.key().getText(), ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 72 | |
| 73 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 74 | if (listener.getParsedDataStack().peek() instanceof YangList) { |
| 75 | YangList yangList = (YangList) tmpData; |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 76 | String tmpKeyValue = removeQuotesAndHandleConcat(ctx.key().getText()); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 77 | if (tmpKeyValue.contains(" ")) { |
| 78 | String[] keyValues = tmpKeyValue.split(" "); |
| 79 | for (String keyValue : keyValues) { |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 80 | try { |
| 81 | yangList.addKey(keyValue); |
| 82 | } catch (DataModelException e) { |
| 83 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA, |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 84 | ctx.key().getText(), ENTRY, e.getMessage())); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 85 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 86 | } |
| 87 | } else { |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 88 | try { |
| 89 | yangList.addKey(tmpKeyValue); |
| 90 | } catch (DataModelException e) { |
| 91 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, KEY_DATA, |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 92 | ctx.key().getText(), ENTRY, e.getMessage())); |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 93 | } |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 94 | } |
| 95 | } else { |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 96 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.key().getText(), |
Vidyashree Rama | f67c4ba | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 97 | ENTRY)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 98 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 99 | } |
| 100 | } |