janani b | 0e4e8ae | 2016-07-13 21:06:41 +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.YangLeafRef; |
| 20 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 21 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 22 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 23 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 24 | |
| 25 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.validatePathArgument; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 28 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.PATH_DATA; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 32 | |
| 33 | /* |
| 34 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 35 | * |
| 36 | * ABNF grammar as per RFC6020 |
| 37 | * leafref-specification = |
| 38 | * ;; these stmts can appear in any order |
| 39 | * path-stmt stmtsep |
| 40 | * [require-instance-stmt stmtsep] |
| 41 | * |
| 42 | * path-stmt = path-keyword sep path-arg-str stmtend |
| 43 | * |
| 44 | * ANTLR grammar rule |
| 45 | * |
| 46 | * leafrefSpecification : (pathStatement (requireInstanceStatement)?) | ((requireInstanceStatement)? pathStatement); |
| 47 | * |
| 48 | * pathStatement : PATH_KEYWORD path STMTEND; |
| 49 | */ |
| 50 | |
| 51 | /** |
| 52 | * Represents listener based call back function corresponding to the |
| 53 | * "path" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 54 | * in RFC 6020. |
| 55 | */ |
| 56 | public final class PathListener { |
| 57 | |
| 58 | /** |
| 59 | * Creates a new path listener. |
| 60 | */ |
| 61 | private PathListener() { |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * It is called when parser receives an input matching the grammar rule |
| 66 | * (path), performs validation and updates the data model tree. |
| 67 | * |
| 68 | * @param listener listener's object |
| 69 | * @param ctx context object of the grammar rule |
| 70 | */ |
| 71 | public static void processPathEntry(TreeWalkListener listener, |
| 72 | GeneratedYangParser.PathStatementContext ctx) { |
| 73 | |
| 74 | // Check for stack to be non empty. |
| 75 | checkStackIsNotEmpty(listener, MISSING_HOLDER, PATH_DATA, ctx.path().getText(), ENTRY); |
| 76 | |
| 77 | Parsable curData = listener.getParsedDataStack().peek(); |
| 78 | |
| 79 | // Checks the holder of path as leafref, else throws error. |
| 80 | if (curData instanceof YangLeafRef) { |
| 81 | |
| 82 | // Splitting the path argument and updating it in the datamodel tree. |
| 83 | validatePathArgument(ctx.path().getText(), PATH_DATA, ctx, (YangLeafRef) curData); |
| 84 | } else { |
| 85 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, PATH_DATA, |
| 86 | ctx.path().getText(), ENTRY)); |
| 87 | } |
| 88 | } |
| 89 | } |