Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +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 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangModule; |
| 20 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 25 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.YangConstructType.CONTACT_DATA; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 35 | * |
| 36 | * ABNF grammar as per RFC6020 |
| 37 | * meta-stmts = ;; these stmts can appear in any order |
| 38 | * [organization-stmt stmtsep] |
| 39 | * [contact-stmt stmtsep] |
| 40 | * [description-stmt stmtsep] |
| 41 | * [reference-stmt stmtsep] |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 42 | * contact-stmt = contact-keyword sep string optsep stmtend |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 43 | * |
| 44 | * ANTLR grammar rule |
| 45 | * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt? |
| 46 | * | organization_stmt? contact_stmt? reference_stmt? description_stmt? |
| 47 | * | organization_stmt? description_stmt? contact_stmt? reference_stmt? |
| 48 | * | organization_stmt? description_stmt? reference_stmt? contact_stmt? |
| 49 | * | organization_stmt? reference_stmt? contact_stmt? description_stmt? |
| 50 | * | organization_stmt? reference_stmt? description_stmt? contact_stmt? |
| 51 | * | contact_stmt? organization_stmt? description_stmt? reference_stmt? |
| 52 | * | contact_stmt? organization_stmt? reference_stmt? description_stmt? |
| 53 | * | contact_stmt? reference_stmt? organization_stmt? description_stmt? |
| 54 | * | contact_stmt? reference_stmt? description_stmt? organization_stmt? |
| 55 | * | contact_stmt? description_stmt? reference_stmt? organization_stmt? |
| 56 | * | contact_stmt? description_stmt? organization_stmt? reference_stmt? |
| 57 | * | reference_stmt? contact_stmt? organization_stmt? description_stmt? |
| 58 | * | reference_stmt? contact_stmt? description_stmt? organization_stmt? |
| 59 | * | reference_stmt? organization_stmt? contact_stmt? description_stmt? |
| 60 | * | reference_stmt? organization_stmt? description_stmt? contact_stmt? |
| 61 | * | reference_stmt? description_stmt? organization_stmt? contact_stmt? |
| 62 | * | reference_stmt? description_stmt? contact_stmt? organization_stmt? |
| 63 | * | description_stmt? reference_stmt? contact_stmt? organization_stmt? |
| 64 | * | description_stmt? reference_stmt? organization_stmt? contact_stmt? |
| 65 | * | description_stmt? contact_stmt? reference_stmt? organization_stmt? |
| 66 | * | description_stmt? contact_stmt? organization_stmt? reference_stmt? |
| 67 | * | description_stmt? organization_stmt? contact_stmt? reference_stmt? |
| 68 | * | description_stmt? organization_stmt? reference_stmt? contact_stmt? |
| 69 | * ; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 70 | * contact_stmt : CONTACT_KEYWORD string STMTEND; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | /** |
| 74 | * Implements listener based call back function corresponding to the "contact" |
| 75 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 76 | */ |
| 77 | public final class ContactListener { |
| 78 | |
| 79 | /** |
| 80 | * Creates a new contact listener. |
| 81 | */ |
| 82 | private ContactListener() { |
| 83 | } |
| 84 | |
| 85 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 86 | * It is called when parser receives an input matching the grammar rule |
| 87 | * (contact), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 88 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 89 | * @param listener Listener's object |
| 90 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 91 | */ |
| 92 | public static void processContactEntry(TreeWalkListener listener, GeneratedYangParser.ContactStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 93 | |
| 94 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 95 | checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTACT_DATA, ctx.string().getText(), ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 96 | |
| 97 | // Obtain the node of the stack. |
| 98 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 99 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 100 | case MODULE_DATA: { |
| 101 | YangModule module = (YangModule) tmpNode; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 102 | module.setContact(ctx.string().getText()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | case SUB_MODULE_DATA: { |
| 106 | YangSubModule subModule = (YangSubModule) tmpNode; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 107 | subModule.setContact(ctx.string().getText()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | default: |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 111 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTACT_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 112 | ctx.string().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 113 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 114 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 115 | } |