blob: 84eefeaff33931dcf92d4c054dc1ba4a4595069e [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;
22import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053030
31/*
32 * Reference: RFC6020 and YANG ANTLR Grammar
33 *
34 * ABNF grammar as per RFC6020
35 * max-elements-stmt = max-elements-keyword sep
36 * max-value-arg-str stmtend
37 * max-value-arg-str = < a string that matches the rule
38 * max-value-arg >
39 *
40 * ANTLR grammar rule
41 * maxElementsStatement : MAX_ELEMENTS_KEYWORD maxValueArgument STMTEND;
42 */
43
44/**
45 * Implements listener based call back function corresponding to the "max-elements"
46 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
47 */
48public final class MaxElementsListener {
49
50 /**
51 * Creates a new max-elements listener.
52 */
53 private MaxElementsListener() {
54 }
55
56 /**
57 * It is called when parser receives an input matching the grammar
58 * rule (max-elements), performs validation and updates the data model
59 * tree.
60 *
61 * @param listener listener's object.
62 * @param ctx context object of the grammar rule.
63 */
64 public static void processMaxElementsEntry(TreeWalkListener listener,
65 GeneratedYangParser.MaxElementsStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053066 int maxElementsValue;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053067
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053068 // Check for stack to be non empty.
69 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
70 ParsableDataType.MAX_ELEMENT_DATA, "", ListenerErrorLocation.ENTRY);
71
72 if (ctx.maxValueArgument().UNBOUNDED_KEYWORD() != null) {
73 maxElementsValue = Integer.MAX_VALUE;
74 } else {
75 maxElementsValue = Integer.parseInt(ctx.maxValueArgument().INTEGER().getText());
76 }
77
78 Parsable tmpData = listener.getParsedDataStack().peek();
79 switch (tmpData.getParsableDataType()) {
80 case LEAF_LIST_DATA:
81 YangLeafList leafList = (YangLeafList) tmpData;
82 leafList.setMaxElelements(maxElementsValue);
83 break;
84 case LIST_DATA:
85 YangList yangList = (YangList) tmpData;
86 yangList.setMaxElelements(maxElementsValue);
87 break;
88 default:
89 throw new ParserException(ListenerErrorMessageConstruction
90 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
91 ParsableDataType.MAX_ELEMENT_DATA,
92 "", ListenerErrorLocation.ENTRY));
93 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053094 }
95}