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.YangStatus; |
| 20 | import org.onosproject.yangutils.datamodel.YangStatusType; |
| 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 | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame^] | 25 | |
| 26 | import static org.onosproject.yangutils.parser.ParsableDataType.STATUS_DATA; |
| 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; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 35 | * |
| 36 | * ABNF grammar as per RFC6020 |
| 37 | * status-stmt = status-keyword sep status-arg-str stmtend |
| 38 | * status-arg-str = < a string that matches the rule |
| 39 | * status-arg > |
| 40 | * status-arg = current-keyword / |
| 41 | * obsolete-keyword / |
| 42 | * deprecated-keyword |
| 43 | * |
| 44 | * ANTLR grammar rule |
| 45 | * statusStatement : STATUS_KEYWORD (CURRENT_KEYWORD | OBSOLETE_KEYWORD | DEPRECATED_KEYWORD) STMTEND; |
| 46 | */ |
| 47 | |
| 48 | /** |
| 49 | * Implements listener based call back function corresponding to the "status" |
| 50 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 51 | */ |
| 52 | public final class StatusListener { |
| 53 | |
| 54 | /** |
| 55 | * Creates a new status listener. |
| 56 | */ |
| 57 | private StatusListener() { |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * It is called when parser receives an input matching the grammar |
| 62 | * rule (status), performs validation and updates the data model |
| 63 | * tree. |
| 64 | * |
| 65 | * @param listener listener's object. |
| 66 | * @param ctx context object of the grammar rule. |
| 67 | */ |
| 68 | public static void processStatusEntry(TreeWalkListener listener, |
| 69 | GeneratedYangParser.StatusStatementContext ctx) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 70 | YangStatusType status; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 71 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 72 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame^] | 73 | checkStackIsNotEmpty(listener, MISSING_HOLDER, STATUS_DATA, "", ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 74 | |
| 75 | if (ctx.CURRENT_KEYWORD() != null) { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame^] | 76 | status = YangStatusType.CURRENT; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 77 | } else if (ctx.DEPRECATED_KEYWORD() != null) { |
| 78 | status = YangStatusType.DEPRECATED; |
| 79 | } else { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame^] | 80 | status = YangStatusType.OBSOLETE; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 84 | if (tmpData instanceof YangStatus) { |
| 85 | YangStatus yangStatus = (YangStatus) tmpData; |
| 86 | yangStatus.setStatus(status); |
| 87 | } else { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame^] | 88 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, STATUS_DATA, "", ENTRY)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 89 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 90 | } |
| 91 | } |