blob: 9c2a4bd2d92b69e76444781f806849f508b0c583 [file] [log] [blame]
Vidyashree Rama0b920732016-03-29 09:52:22 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama0b920732016-03-29 09:52:22 +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
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053019import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053020import org.onosproject.yangutils.datamodel.YangRangeRestriction;
21import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama0b920732016-03-29 09:52:22 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053028import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
29import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypeUtils.isOfRangeRestrictedType;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053030import static org.onosproject.yangutils.datamodel.utils.RestrictionResolver.processRangeRestriction;
Bharat saraswal96dfef02016-06-16 00:29:12 +053031import static org.onosproject.yangutils.datamodel.utils.YangConstructType.RANGE_DATA;
32import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA;
Vidyashree Rama0b920732016-03-29 09:52:22 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Vidyashree Rama1db15562016-05-17 16:16:15 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Rama0b920732016-03-29 09:52:22 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Rama1db15562016-05-17 16:16:15 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama0b920732016-03-29 09:52:22 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama0b920732016-03-29 09:52:22 +053040
41/*
42 * Reference: RFC6020 and YANG ANTLR Grammar
43 *
44 * ABNF grammar as per RFC6020
45 * range-stmt = range-keyword sep range-arg-str optsep
46 * (";" /
47 * "{" stmtsep
48 * ;; these stmts can appear in any order
49 * [error-message-stmt stmtsep]
50 * [error-app-tag-stmt stmtsep]
51 * [description-stmt stmtsep]
52 * [reference-stmt stmtsep]
53 * "}")
54 *
55 * ANTLR grammar rule
56 * rangeStatement : RANGE_KEYWORD range (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
57 */
58
59/**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Represents listener based call back function corresponding to the "range"
Vidyashree Rama0b920732016-03-29 09:52:22 +053061 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
62 */
63public final class RangeRestrictionListener {
64
Vidyashree Rama0b920732016-03-29 09:52:22 +053065 /**
66 * Creates a new range restriction listener.
67 */
68 private RangeRestrictionListener() {
69 }
70
71 /**
72 * It is called when parser receives an input matching the grammar
73 * rule (range), performs validation and updates the data model
74 * tree.
75 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053076 * @param listener listener's object
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053077 * @param ctx context object of the grammar rule
Vidyashree Rama0b920732016-03-29 09:52:22 +053078 */
79 public static void processRangeRestrictionEntry(TreeWalkListener listener,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053080 GeneratedYangParser.RangeStatementContext ctx) {
Vidyashree Rama0b920732016-03-29 09:52:22 +053081
82 // Check for stack to be non empty.
83 checkStackIsNotEmpty(listener, MISSING_HOLDER, RANGE_DATA, ctx.range().getText(), ENTRY);
84
85 Parsable tmpData = listener.getParsedDataStack().peek();
86 if (tmpData.getYangConstructType() == TYPE_DATA) {
87 YangType type = (YangType) tmpData;
Vidyashree Rama1db15562016-05-17 16:16:15 +053088 setRangeRestriction(listener, type, ctx);
Vidyashree Rama0b920732016-03-29 09:52:22 +053089 } else {
90 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RANGE_DATA,
91 ctx.range().getText(), ENTRY));
92 }
93 }
94
95 /**
96 * Sets the range restriction to type.
97 *
Vidyashree Rama1db15562016-05-17 16:16:15 +053098 * @param listener listener's object
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053099 * @param type YANG type for which range restriction to be added
100 * @param ctx context object of the grammar rule
Vidyashree Rama0b920732016-03-29 09:52:22 +0530101 */
Vidyashree Rama1db15562016-05-17 16:16:15 +0530102 private static void setRangeRestriction(TreeWalkListener listener, YangType type,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530103 GeneratedYangParser.RangeStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530104
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530105 if (type.getDataType() == DERIVED) {
106 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
107 .setRangeRestrictionString(ctx.range().getText());
108 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
109 .setLineNumber(ctx.getStart().getLine());
110 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
111 .setCharPosition(ctx.getStart().getCharPositionInLine());
112 return;
Vidyashree Rama0b920732016-03-29 09:52:22 +0530113 }
114
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530115 if (!(isOfRangeRestrictedType(type.getDataType()))) {
116 ParserException parserException = new ParserException("YANG file error: Range restriction can't be " +
117 "applied to a given type");
118 parserException.setLine(ctx.getStart().getLine());
119 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
120 throw parserException;
121 }
122
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530123 YangRangeRestriction rangeRestriction = null;
124 try {
125 rangeRestriction = processRangeRestriction(null, ctx.getStart().getLine(),
126 ctx.getStart().getCharPositionInLine(), false, ctx.range().getText(), type.getDataType());
127 } catch (DataModelException e) {
128 ParserException parserException = new ParserException(e.getMessage());
129 parserException.setCharPosition(e.getCharPositionInLine());
130 parserException.setLine(e.getLineNumber());
131 throw parserException;
132 }
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530133
Vidyashree Rama0b920732016-03-29 09:52:22 +0530134 if (rangeRestriction != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530135 type.setDataTypeExtendedInfo(rangeRestriction);
Vidyashree Rama0b920732016-03-29 09:52:22 +0530136 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530137 listener.getParsedDataStack().push(rangeRestriction);
138 }
139
140 /**
141 * Performs validation and updates the data model tree.
142 * It is called when parser exits from grammar rule (range).
143 *
144 * @param listener listener's object
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530145 * @param ctx context object of the grammar rule
Vidyashree Rama1db15562016-05-17 16:16:15 +0530146 */
147 public static void processRangeRestrictionExit(TreeWalkListener listener,
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530148 GeneratedYangParser.RangeStatementContext ctx) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530149
150 // Check for stack to be non empty.
151 checkStackIsNotEmpty(listener, MISSING_HOLDER, RANGE_DATA, ctx.range().getText(), EXIT);
152
153 Parsable tmpData = listener.getParsedDataStack().peek();
154 if (tmpData instanceof YangRangeRestriction) {
155 listener.getParsedDataStack().pop();
156 } else if (tmpData instanceof YangType
157 && ((YangType) tmpData).getDataType() == DERIVED) {
158 // TODO : need to handle in linker
159 } else {
160 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, RANGE_DATA,
161 ctx.range().getText(), EXIT));
162 }
Vidyashree Rama0b920732016-03-29 09:52:22 +0530163 }
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530164}