blob: 90acba6741bb9c8e91943d027a9f1509a822daf2 [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.YangMaxElement;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama59071f32016-02-20 19:27:56 +053027
Bharat saraswal96dfef02016-06-16 00:29:12 +053028import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MAX_ELEMENT_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
Vidyashree Rama59071f32016-02-20 19:27:56 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053035
36/*
37 * Reference: RFC6020 and YANG ANTLR Grammar
38 *
39 * ABNF grammar as per RFC6020
40 * max-elements-stmt = max-elements-keyword sep
41 * max-value-arg-str stmtend
42 * max-value-arg-str = < a string that matches the rule
43 * max-value-arg >
44 *
45 * ANTLR grammar rule
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053046 * maxElementsStatement : MAX_ELEMENTS_KEYWORD maxValue STMTEND;
47 * maxValue : 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
Vinod Kumar Sc4216002016-03-03 19:55:30 +053052 * "max-elements" rule defined in ANTLR grammar file for corresponding ABNF rule
53 * in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053054 */
55public final class MaxElementsListener {
56
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053057 private static final String POSITIVE_INTEGER_PATTERN = "[1-9][0-9]*";
58 private static final String UNBOUNDED_KEYWORD = "unbounded";
59
Vidyashree Rama92fc5562016-02-12 18:44:12 +053060 /**
61 * Creates a new max-elements listener.
62 */
63 private MaxElementsListener() {
64 }
65
66 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053067 * It is called when parser receives an input matching the grammar rule
68 * (max-elements), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053069 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053070 * @param listener listener's object
71 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053072 */
73 public static void processMaxElementsEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053074 GeneratedYangParser.MaxElementsStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053075
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053076 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053077 checkStackIsNotEmpty(listener, MISSING_HOLDER, MAX_ELEMENT_DATA, "", ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053078
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053079 int maxElementsValue = getValidMaxElementValue(ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053080
81 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053082 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053083 case LEAF_LIST_DATA:
84 YangLeafList leafList = (YangLeafList) tmpData;
rama-huawei6c728a92016-07-11 14:48:12 +053085 YangMaxElement maxLeafListElement = new YangMaxElement();
86 maxLeafListElement.setMaxElement(maxElementsValue);
87 leafList.setMaxElements(maxLeafListElement);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053088 break;
89 case LIST_DATA:
90 YangList yangList = (YangList) tmpData;
rama-huawei6c728a92016-07-11 14:48:12 +053091 YangMaxElement maxListElement = new YangMaxElement();
92 maxListElement.setMaxElement(maxElementsValue);
93 yangList.setMaxElements(maxListElement);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053094 break;
95 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053096 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MAX_ELEMENT_DATA, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053097 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053098 }
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053099
100 /**
101 * Validates max element value and returns the value from context.
102 *
103 * @param ctx context object of the grammar rule
104 * @return max element's value
105 */
106 private static int getValidMaxElementValue(GeneratedYangParser.MaxElementsStatementContext ctx) {
107
108 int maxElementsValue;
109
110 String value = removeQuotesAndHandleConcat(ctx.maxValue().getText());
111 if (value.equals(UNBOUNDED_KEYWORD)) {
112 maxElementsValue = Integer.MAX_VALUE;
113 } else if (value.matches(POSITIVE_INTEGER_PATTERN)) {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530114 try {
115 maxElementsValue = Integer.parseInt(value);
116 } catch (NumberFormatException e) {
117 ParserException parserException = new ParserException("YANG file error : " +
118 YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " +
119 "valid.");
120 parserException.setLine(ctx.getStart().getLine());
121 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
122 throw parserException;
123 }
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530124 } else {
125 ParserException parserException = new ParserException("YANG file error : " +
126 YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " +
127 "valid.");
128 parserException.setLine(ctx.getStart().getLine());
129 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
130 throw parserException;
131 }
132
133 return maxElementsValue;
134 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530135}