blob: eec78ba2bde0595b5e10acaa46a0416a6eba9cd9 [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 Rama8a6b1282016-03-15 10:18:25 +053025import org.onosproject.yangutils.utils.YangConstructType;
Vidyashree Rama59071f32016-02-20 19:27:56 +053026
Vidyashree Rama59071f32016-02-20 19:27:56 +053027import 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;
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
Vidyashree Rama59071f32016-02-20 19:27:56 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053033import static org.onosproject.yangutils.utils.YangConstructType.MAX_ELEMENT_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ABNF grammar as per RFC6020
39 * max-elements-stmt = max-elements-keyword sep
40 * max-value-arg-str stmtend
41 * max-value-arg-str = < a string that matches the rule
42 * max-value-arg >
43 *
44 * ANTLR grammar rule
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053045 * maxElementsStatement : MAX_ELEMENTS_KEYWORD maxValue STMTEND;
46 * maxValue : 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
Vinod Kumar Sc4216002016-03-03 19:55:30 +053051 * "max-elements" rule defined in ANTLR grammar file for corresponding ABNF rule
52 * in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053053 */
54public final class MaxElementsListener {
55
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053056 private static final String POSITIVE_INTEGER_PATTERN = "[1-9][0-9]*";
57 private static final String UNBOUNDED_KEYWORD = "unbounded";
58
Vidyashree Rama92fc5562016-02-12 18:44:12 +053059 /**
60 * Creates a new max-elements listener.
61 */
62 private MaxElementsListener() {
63 }
64
65 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053066 * It is called when parser receives an input matching the grammar rule
67 * (max-elements), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053068 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053069 * @param listener listener's object
70 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053071 */
72 public static void processMaxElementsEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053073 GeneratedYangParser.MaxElementsStatementContext ctx) {
Vidyashree Rama92fc5562016-02-12 18:44:12 +053074
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053075 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053076 checkStackIsNotEmpty(listener, MISSING_HOLDER, MAX_ELEMENT_DATA, "", ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053077
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053078 int maxElementsValue = getValidMaxElementValue(ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053079
80 Parsable tmpData = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053081 switch (tmpData.getYangConstructType()) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053082 case LEAF_LIST_DATA:
83 YangLeafList leafList = (YangLeafList) tmpData;
84 leafList.setMaxElelements(maxElementsValue);
85 break;
86 case LIST_DATA:
87 YangList yangList = (YangList) tmpData;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053088 yangList.setMaxElements(maxElementsValue);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053089 break;
90 default:
Vidyashree Rama59071f32016-02-20 19:27:56 +053091 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, MAX_ELEMENT_DATA, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053092 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +053093 }
Vidyashree Rama8a6b1282016-03-15 10:18:25 +053094
95 /**
96 * Validates max element value and returns the value from context.
97 *
98 * @param ctx context object of the grammar rule
99 * @return max element's value
100 */
101 private static int getValidMaxElementValue(GeneratedYangParser.MaxElementsStatementContext ctx) {
102
103 int maxElementsValue;
104
105 String value = removeQuotesAndHandleConcat(ctx.maxValue().getText());
106 if (value.equals(UNBOUNDED_KEYWORD)) {
107 maxElementsValue = Integer.MAX_VALUE;
108 } else if (value.matches(POSITIVE_INTEGER_PATTERN)) {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530109 try {
110 maxElementsValue = Integer.parseInt(value);
111 } catch (NumberFormatException e) {
112 ParserException parserException = new ParserException("YANG file error : " +
113 YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " +
114 "valid.");
115 parserException.setLine(ctx.getStart().getLine());
116 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
117 throw parserException;
118 }
Vidyashree Rama8a6b1282016-03-15 10:18:25 +0530119 } else {
120 ParserException parserException = new ParserException("YANG file error : " +
121 YangConstructType.getYangConstructType(MAX_ELEMENT_DATA) + " value " + value + " is not " +
122 "valid.");
123 parserException.setLine(ctx.getStart().getLine());
124 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
125 throw parserException;
126 }
127
128 return maxElementsValue;
129 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530130}