Vidyashree Rama | 13b4c55 | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 19 | import org.onosproject.yangutils.datamodel.YangMust; |
| 20 | import org.onosproject.yangutils.datamodel.YangMustHolder; |
| 21 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 25 | |
| 26 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MUST_DATA; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 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_CURRENT_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 35 | |
| 36 | /* |
| 37 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 38 | * |
| 39 | * ABNF grammar as per RFC6020 |
| 40 | * |
| 41 | * must-stmt = must-keyword sep string optsep |
| 42 | * (";" / |
| 43 | * "{" stmtsep |
| 44 | * ;; these stmts can appear in any order |
| 45 | * [error-message-stmt stmtsep] |
| 46 | * [error-app-tag-stmt stmtsep] |
| 47 | * [description-stmt stmtsep] |
| 48 | * [reference-stmt stmtsep] |
| 49 | * "}") |
| 50 | * |
| 51 | * ANTLR grammar rule |
| 52 | * mustStatement : MUST_KEYWORD string (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE); |
| 53 | */ |
| 54 | |
| 55 | /** |
| 56 | * Represents listener based call back function corresponding to the |
| 57 | * "must" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 58 | * in RFC 6020. |
| 59 | */ |
| 60 | public final class MustListener { |
| 61 | |
| 62 | /** |
| 63 | * Creates a new must listener. |
| 64 | */ |
| 65 | private MustListener() { |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Perform validations and updates the data model tree.It is called when parser |
| 70 | * receives an input matching the grammar rule (must). |
| 71 | * |
| 72 | * @param listener listener's object |
| 73 | * @param ctx context object of the grammar rule |
| 74 | */ |
| 75 | public static void processMustEntry(TreeWalkListener listener, |
| 76 | GeneratedYangParser.MustStatementContext ctx) { |
| 77 | |
| 78 | // Check for stack to be non empty. |
| 79 | checkStackIsNotEmpty(listener, MISSING_HOLDER, MUST_DATA, ctx.string().getText(), ENTRY); |
| 80 | String constraint = removeQuotesAndHandleConcat(ctx.string().getText()); |
| 81 | |
| 82 | // Obtain the node of the stack. |
| 83 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 84 | if (tmpNode instanceof YangMustHolder) { |
| 85 | |
| 86 | YangMust must = new YangMust(); |
| 87 | must.setConstraint(constraint); |
| 88 | |
| 89 | YangMustHolder mustHolder = (YangMustHolder) tmpNode; |
| 90 | mustHolder.addMust(must); |
| 91 | |
| 92 | listener.getParsedDataStack().push(must); |
| 93 | } else { |
| 94 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MUST_DATA, |
| 95 | ctx.string().getText(), ENTRY)); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Performs validation and updates the data model tree.It is called when parser |
| 102 | * exits from grammar rule (must). |
| 103 | * |
| 104 | * @param listener listener's object |
| 105 | * @param ctx context object of the grammar rule |
| 106 | */ |
| 107 | public static void processMustExit(TreeWalkListener listener, |
| 108 | GeneratedYangParser.MustStatementContext ctx) { |
| 109 | |
| 110 | // Check for stack to be non empty. |
| 111 | checkStackIsNotEmpty(listener, MISSING_HOLDER, MUST_DATA, ctx.string().getText(), EXIT); |
| 112 | |
| 113 | if (listener.getParsedDataStack().peek() instanceof YangMust) { |
| 114 | listener.getParsedDataStack().pop(); |
| 115 | } else { |
| 116 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MUST_DATA, |
| 117 | ctx.string().getText(), EXIT)); |
| 118 | } |
| 119 | } |
| 120 | } |