blob: da8a66633508502864dc070abce4c03059976c58 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
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
17package org.onosproject.yangutils.parser.impl.listeners;
18
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangLeafList;
20import org.onosproject.yangutils.datamodel.YangList;
21import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama59071f32016-02-20 19:27:56 +053025
Vidyashree Rama59071f32016-02-20 19:27:56 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053031import static org.onosproject.yangutils.utils.YangConstructType.MAX_ELEMENT_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * max-elements-stmt = max-elements-keyword sep
38 * max-value-arg-str stmtend
39 * max-value-arg-str = < a string that matches the rule
40 * max-value-arg >
41 *
42 * ANTLR grammar rule
43 * maxElementsStatement : MAX_ELEMENTS_KEYWORD maxValueArgument STMTEND;
44 */
45
46/**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053047 * Implements listener based call back function corresponding to the
48 * "max-elements" rule defined in ANTLR grammar file for corresponding ABNF rule
49 * in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053050 */
51public final class MaxElementsListener {
52
53 /**
54 * Creates a new max-elements listener.
55 */
56 private MaxElementsListener() {
57 }
58
59 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053060 * It is called when parser receives an input matching the grammar rule
61 * (max-elements), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053062 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053063 * @param listener listener's object
64 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053065 */
66 public static void processMaxElementsEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053067 GeneratedYangParser.MaxElementsStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053068 int maxElementsValue;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053070 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053071 checkStackIsNotEmpty(listener, MISSING_HOLDER, MAX_ELEMENT_DATA, "", ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053072
73 if (ctx.maxValueArgument().UNBOUNDED_KEYWORD() != null) {
74 maxElementsValue = Integer.MAX_VALUE;
75 } else {
76 maxElementsValue = Integer.parseInt(ctx.maxValueArgument().INTEGER().getText());
77 }
78
79 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053080 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053081 case LEAF_LIST_DATA:
82 YangLeafList leafList = (YangLeafList) tmpData;
83 leafList.setMaxElelements(maxElementsValue);
84 break;
85 case LIST_DATA:
86 YangList yangList = (YangList) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053087 yangList.setMaxElements(maxElementsValue);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053088 break;
89 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053090 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MAX_ELEMENT_DATA, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053091 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053092 }
93}