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.YangLeaf; |
| 20 | import org.onosproject.yangutils.parser.Parsable; |
| 21 | import org.onosproject.yangutils.parser.ParsableDataType; |
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 | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 32 | * |
| 33 | * ABNF grammar as per RFC6020 |
| 34 | * mandatory-stmt = mandatory-keyword sep |
| 35 | * mandatory-arg-str stmtend |
| 36 | * |
| 37 | * mandatory-arg-str = < a string that matches the rule |
| 38 | * mandatory-arg > |
| 39 | * |
| 40 | * mandatory-arg = true-keyword / false-keyword |
| 41 | * |
| 42 | * ANTLR grammar rule |
| 43 | * mandatoryStatement : MANDATORY_KEYWORD (TRUE_KEYWORD | FALSE_KEYWORD) STMTEND; |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * Implements listener based call back function corresponding to the "mandatory" |
| 48 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 49 | */ |
| 50 | public final class MandatoryListener { |
| 51 | |
| 52 | /** |
| 53 | * Creates a new mandatory listener. |
| 54 | */ |
| 55 | private MandatoryListener() { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * It is called when parser receives an input matching the grammar |
| 60 | * rule (mandatory), performs validation and updates the data model |
| 61 | * tree. |
| 62 | * |
| 63 | * @param listener listener's object. |
| 64 | * @param ctx context object of the grammar rule. |
| 65 | */ |
| 66 | public static void processMandatoryEntry(TreeWalkListener listener, |
| 67 | GeneratedYangParser.MandatoryStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 68 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 69 | // Check for stack to be non empty. |
| 70 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 71 | ParsableDataType.MANDATORY_DATA, "", ListenerErrorLocation.ENTRY); |
| 72 | |
| 73 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 74 | switch (tmpNode.getParsableDataType()) { |
| 75 | case LEAF_DATA: |
| 76 | YangLeaf leaf = (YangLeaf) tmpNode; |
| 77 | if (ctx.TRUE_KEYWORD() != null) { |
| 78 | leaf.setMandatory(true); |
| 79 | } else { |
| 80 | leaf.setMandatory(false); |
| 81 | } |
| 82 | break; |
| 83 | case CHOICE_DATA: // TODO |
| 84 | break; |
| 85 | default: |
| 86 | throw new ParserException(ListenerErrorMessageConstruction |
| 87 | .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, |
| 88 | ParsableDataType.MANDATORY_DATA, "", ListenerErrorLocation.ENTRY)); |
| 89 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 90 | } |
| 91 | } |