blob: 7e7ed78dea9d8b8c2c0805ddab678ac19941233c [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;
rama-huawei6c728a92016-07-11 14:48:12 +053021import org.onosproject.yangutils.datamodel.YangMinElement;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
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 Rama59071f32016-02-20 19:27:56 +053026
Bharat saraswal96dfef02016-06-16 00:29:12 +053027import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MIN_ELEMENT_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswald9822e92016-04-05 15:13:44 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNonNegativeIntegerValue;
Vidyashree Rama59071f32016-02-20 19:27:56 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * min-elements-stmt = min-elements-keyword sep
40 * min-value-arg-str stmtend
41 * min-value-arg-str = < a string that matches the rule
42 * min-value-arg >
43 * min-value-arg = non-negative-integer-value
44 *
45 * ANTLR grammar rule
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053046 * minElementsStatement : MIN_ELEMENTS_KEYWORD minValue STMTEND;
47 * minValue : string;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053048 */
49
50/**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Represents listener based call back function corresponding to the "min-elements"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053052 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
53 */
54public final class MinElementsListener {
55
56 /**
57 * Creates a new min-elements listener.
58 */
59 private MinElementsListener() {
60 }
61
62 /**
63 * It is called when parser receives an input matching the grammar
64 * rule (min-elements), performs validation and updates the data model
65 * tree.
66 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053067 * @param listener listener's object
68 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069 */
70 public static void processMinElementsEntry(TreeWalkListener listener,
71 GeneratedYangParser.MinElementsStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053072
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053073 // Check for stack to be non empty.
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053074 checkStackIsNotEmpty(listener, MISSING_HOLDER, MIN_ELEMENT_DATA, ctx.minValue().getText(), ENTRY);
75
76 int minElementValue = getValidNonNegativeIntegerValue(ctx.minValue().getText(), MIN_ELEMENT_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053077
78 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053079 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053080 case LEAF_LIST_DATA:
81 YangLeafList leafList = (YangLeafList) tmpData;
rama-huawei6c728a92016-07-11 14:48:12 +053082 YangMinElement minLeafListElement = new YangMinElement();
83 minLeafListElement.setMinElement(minElementValue);
84 leafList.setMinElements(minLeafListElement);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053085 break;
86 case LIST_DATA:
87 YangList yangList = (YangList) tmpData;
rama-huawei6c728a92016-07-11 14:48:12 +053088 YangMinElement minElement = new YangMinElement();
89 minElement.setMinElement(minElementValue);
90 yangList.setMinElements(minElement);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053091 break;
92 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053093 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MIN_ELEMENT_DATA,
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053094 ctx.minValue().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053095 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053096 }
97}