Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 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 | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.utils.YangConstructType; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 26 | |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +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; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 37 | * |
| 38 | * ABNF grammar as per RFC6020 |
| 39 | * status-stmt = status-keyword sep status-arg-str stmtend |
| 40 | * status-arg-str = < a string that matches the rule |
| 41 | * status-arg > |
| 42 | * status-arg = current-keyword / |
| 43 | * obsolete-keyword / |
| 44 | * deprecated-keyword |
| 45 | * |
| 46 | * ANTLR grammar rule |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 47 | * statusStatement : STATUS_KEYWORD status STMTEND; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Represents listener based call back function corresponding to the "status" |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 52 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 53 | */ |
| 54 | public final class StatusListener { |
| 55 | |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 56 | private static final String CURRENT_KEYWORD = "current"; |
| 57 | private static final String DEPRECATED_KEYWORD = "deprecated"; |
| 58 | private static final String OBSOLETE_KEYWORD = "obsolete"; |
| 59 | |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 60 | /** |
| 61 | * Creates a new status listener. |
| 62 | */ |
| 63 | private StatusListener() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * It is called when parser receives an input matching the grammar |
| 68 | * rule (status), performs validation and updates the data model |
| 69 | * tree. |
| 70 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 71 | * @param listener listener's object |
| 72 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 73 | */ |
| 74 | public static void processStatusEntry(TreeWalkListener listener, |
| 75 | GeneratedYangParser.StatusStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 76 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 77 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 78 | checkStackIsNotEmpty(listener, MISSING_HOLDER, STATUS_DATA, "", ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 79 | |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 80 | YangStatusType status = getValidStatus(ctx); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 81 | |
| 82 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 83 | if (tmpData instanceof YangStatus) { |
| 84 | YangStatus yangStatus = (YangStatus) tmpData; |
| 85 | yangStatus.setStatus(status); |
| 86 | } else { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 87 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, STATUS_DATA, "", ENTRY)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 88 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 89 | } |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Validates status value and returns the value from context. |
| 93 | * |
| 94 | * @param ctx context object of the grammar rule |
| 95 | * @return status current/deprecated/obsolete |
| 96 | */ |
| 97 | private static YangStatusType getValidStatus(GeneratedYangParser.StatusStatementContext ctx) { |
| 98 | |
| 99 | YangStatusType status; |
| 100 | |
| 101 | String value = removeQuotesAndHandleConcat(ctx.status().getText()); |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 102 | switch (value) { |
| 103 | case CURRENT_KEYWORD: { |
| 104 | status = YangStatusType.CURRENT; |
| 105 | break; |
| 106 | } |
| 107 | case DEPRECATED_KEYWORD: { |
| 108 | status = YangStatusType.DEPRECATED; |
| 109 | break; |
| 110 | } |
| 111 | case OBSOLETE_KEYWORD: { |
| 112 | status = YangStatusType.OBSOLETE; |
| 113 | break; |
| 114 | } |
| 115 | default: { |
| 116 | ParserException parserException = new ParserException("YANG file error : " + |
| 117 | YangConstructType.getYangConstructType(STATUS_DATA) + " " + ctx.status().getText() + |
| 118 | " is not valid."); |
| 119 | parserException.setLine(ctx.getStart().getLine()); |
| 120 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 121 | throw parserException; |
| 122 | } |
Vidyashree Rama | e68bb19 | 2016-03-15 10:18:25 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return status; |
| 126 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 127 | } |