blob: c03710666ca9b1d74c079b5eb24f7c7aae52d844 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama92fc5562016-02-12 18:44:12 +05303 *
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;
Bharat saraswald9822e92016-04-05 15:13:44 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNonNegativeIntegerValue;
Vidyashree Rama59071f32016-02-20 19:27:56 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053032import static org.onosproject.yangutils.utils.YangConstructType.MIN_ELEMENT_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053033
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * min-elements-stmt = min-elements-keyword sep
39 * min-value-arg-str stmtend
40 * min-value-arg-str = < a string that matches the rule
41 * min-value-arg >
42 * min-value-arg = non-negative-integer-value
43 *
44 * ANTLR grammar rule
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053045 * minElementsStatement : MIN_ELEMENTS_KEYWORD minValue STMTEND;
46 * minValue : string;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053047 */
48
49/**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Represents listener based call back function corresponding to the "min-elements"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053051 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
52 */
53public final class MinElementsListener {
54
55 /**
56 * Creates a new min-elements listener.
57 */
58 private MinElementsListener() {
59 }
60
61 /**
62 * It is called when parser receives an input matching the grammar
63 * rule (min-elements), performs validation and updates the data model
64 * tree.
65 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053066 * @param listener listener's object
67 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053068 */
69 public static void processMinElementsEntry(TreeWalkListener listener,
70 GeneratedYangParser.MinElementsStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053071
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053072 // Check for stack to be non empty.
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053073 checkStackIsNotEmpty(listener, MISSING_HOLDER, MIN_ELEMENT_DATA, ctx.minValue().getText(), ENTRY);
74
75 int minElementValue = getValidNonNegativeIntegerValue(ctx.minValue().getText(), MIN_ELEMENT_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053076
77 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053078 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053079 case LEAF_LIST_DATA:
80 YangLeafList leafList = (YangLeafList) tmpData;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053081 leafList.setMinElements(minElementValue);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053082 break;
83 case LIST_DATA:
84 YangList yangList = (YangList) tmpData;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053085 yangList.setMinElements(minElementValue);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053086 break;
87 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053088 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MIN_ELEMENT_DATA,
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053089 ctx.minValue().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053090 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053091 }
92}