blob: 87a9cdb5cd05fdb068a7b6baccb3df00dec01943 [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 * min-elements-stmt = min-elements-keyword sep
36 * min-value-arg-str stmtend
37 * min-value-arg-str = < a string that matches the rule
38 * min-value-arg >
39 * min-value-arg = non-negative-integer-value
40 *
41 * ANTLR grammar rule
42 * minElementsStatement : MIN_ELEMENTS_KEYWORD INTEGER STMTEND;
43 */
44
45/**
46 * Implements listener based call back function corresponding to the "min-elements"
47 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
48 */
49public final class MinElementsListener {
50
51 /**
52 * Creates a new min-elements listener.
53 */
54 private MinElementsListener() {
55 }
56
57 /**
58 * It is called when parser receives an input matching the grammar
59 * rule (min-elements), performs validation and updates the data model
60 * tree.
61 *
62 * @param listener listener's object.
63 * @param ctx context object of the grammar rule.
64 */
65 public static void processMinElementsEntry(TreeWalkListener listener,
66 GeneratedYangParser.MinElementsStatementContext ctx) {
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.MIN_ELEMENT_DATA, String.valueOf(ctx.INTEGER().getText()),
71 ListenerErrorLocation.ENTRY);
72
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:
84 throw new ParserException(ListenerErrorMessageConstruction
85 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
86 ParsableDataType.MIN_ELEMENT_DATA,
87 String.valueOf(ctx.INTEGER().getText()),
88 ListenerErrorLocation.ENTRY));
89 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053090 }
91}