blob: 8e1e2f753237c91b1543a36f156b852cdd9dcd62 [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
26import static org.onosproject.yangutils.parser.ParsableDataType.MIN_ELEMENT_DATA;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * min-elements-stmt = min-elements-keyword sep
38 * min-value-arg-str stmtend
39 * min-value-arg-str = < a string that matches the rule
40 * min-value-arg >
41 * min-value-arg = non-negative-integer-value
42 *
43 * ANTLR grammar rule
44 * minElementsStatement : MIN_ELEMENTS_KEYWORD INTEGER STMTEND;
45 */
46
47/**
48 * Implements listener based call back function corresponding to the "min-elements"
49 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
50 */
51public final class MinElementsListener {
52
53 /**
54 * Creates a new min-elements listener.
55 */
56 private MinElementsListener() {
57 }
58
59 /**
60 * It is called when parser receives an input matching the grammar
61 * rule (min-elements), performs validation and updates the data model
62 * tree.
63 *
64 * @param listener listener's object.
65 * @param ctx context object of the grammar rule.
66 */
67 public static void processMinElementsEntry(TreeWalkListener listener,
68 GeneratedYangParser.MinElementsStatementContext ctx) {
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, MIN_ELEMENT_DATA, ctx.INTEGER().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053072
73 Parsable tmpData = listener.getParsedDataStack().peek();
74 switch (tmpData.getParsableDataType()) {
75 case LEAF_LIST_DATA:
76 YangLeafList leafList = (YangLeafList) tmpData;
77 leafList.setMinElements(Integer.parseInt(ctx.INTEGER().getText()));
78 break;
79 case LIST_DATA:
80 YangList yangList = (YangList) tmpData;
81 yangList.setMinElements(Integer.parseInt(ctx.INTEGER().getText()));
82 break;
83 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053084 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MIN_ELEMENT_DATA,
85 ctx.INTEGER().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053086 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053087 }
88}