janani b | ebb143d | 2016-07-14 19:35:22 +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.YangList; |
| 20 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 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.UNIQUE_DATA; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
| 29 | .constructExtendedListenerErrorMessage; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction |
| 31 | .constructListenerErrorMessage; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 37 | |
| 38 | /* |
| 39 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 40 | * |
| 41 | * ABNF grammar as per RFC6020 |
| 42 | * unique-stmt = unique-keyword sep unique-arg-str stmtend |
| 43 | * |
| 44 | * ANTLR grammar rule |
| 45 | * uniqueStatement: UNIQUE_KEYWORD unique STMTEND; |
| 46 | * unique : string; |
| 47 | */ |
| 48 | |
| 49 | /** |
| 50 | * Represesnts listener based call back function corresponding to the "unique" rule |
| 51 | * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 52 | */ |
| 53 | public final class UniqueListener { |
| 54 | |
| 55 | /** |
| 56 | * Creates a new unique listener. |
| 57 | */ |
| 58 | private UniqueListener() { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * It is called when parser receives an input matching the grammar rule |
| 63 | * (unique), perform validations and updates the data model tree. |
| 64 | * |
| 65 | * @param listener listener's object |
| 66 | * @param ctx context object of the grammar rule |
| 67 | */ |
| 68 | public static void processUniqueEntry(TreeWalkListener listener, |
| 69 | GeneratedYangParser.UniqueStatementContext ctx) { |
| 70 | |
| 71 | // Check for stack to be non empty. |
| 72 | checkStackIsNotEmpty(listener, MISSING_HOLDER, UNIQUE_DATA, ctx.unique().getText(), ENTRY); |
| 73 | |
| 74 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 75 | if (listener.getParsedDataStack().peek() instanceof YangList) { |
| 76 | YangList yangList = (YangList) tmpData; |
| 77 | String tmpUniqueValue = removeQuotesAndHandleConcat(ctx.unique().getText()); |
| 78 | |
| 79 | if (tmpUniqueValue.contains(" ")) { |
| 80 | String[] uniqueValues = tmpUniqueValue.split(" "); |
| 81 | for (String uniqueValue : uniqueValues) { |
| 82 | try { |
| 83 | yangList.addUnique(uniqueValue); |
| 84 | } catch (DataModelException e) { |
| 85 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 86 | UNIQUE_DATA, |
| 87 | ctx.unique().getText(), ENTRY, e.getMessage())); |
| 88 | } |
| 89 | } |
| 90 | } else { |
| 91 | try { |
| 92 | yangList.addUnique(tmpUniqueValue); |
| 93 | } catch (DataModelException e) { |
| 94 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, UNIQUE_DATA, |
| 95 | ctx.unique().getText(), ENTRY, e.getMessage())); |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, UNIQUE_DATA, ctx.unique().getText(), |
| 100 | ENTRY)); |
| 101 | } |
| 102 | } |
| 103 | } |