blob: 46bc67aac03d7232305280156fe05bd3a42a32af [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
Vidyashree Rama0b920732016-03-29 09:52:22 +053019import java.util.regex.Pattern;
20
Vidyashree Rama0b920732016-03-29 09:52:22 +053021import org.onosproject.yangutils.datamodel.YangRangeInterval;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053022import org.onosproject.yangutils.datamodel.YangRangeRestriction;
23import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.datamodel.YangDerivedInfo;
Vidyashree Rama0b920732016-03-29 09:52:22 +053025import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053026import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Rama0b920732016-03-29 09:52:22 +053027import org.onosproject.yangutils.parser.Parsable;
28import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053031import org.onosproject.yangutils.utils.YangConstructType;
32import org.onosproject.yangutils.utils.builtindatatype.DataTypeException;
33import org.onosproject.yangutils.utils.builtindatatype.YangBuiltInDataTypeInfo;
Vidyashree Rama0b920732016-03-29 09:52:22 +053034
Vidyashree Rama0b920732016-03-29 09:52:22 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vidyashree Rama0b920732016-03-29 09:52:22 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
43import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
44import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA;
45import static org.onosproject.yangutils.utils.builtindatatype.BuiltInTypeObjectFactory.getDataObjectFromString;
Vidyashree Rama0b920732016-03-29 09:52:22 +053046
47/*
48 * Reference: RFC6020 and YANG ANTLR Grammar
49 *
50 * ABNF grammar as per RFC6020
51 * range-stmt = range-keyword sep range-arg-str optsep
52 * (";" /
53 * "{" stmtsep
54 * ;; these stmts can appear in any order
55 * [error-message-stmt stmtsep]
56 * [error-app-tag-stmt stmtsep]
57 * [description-stmt stmtsep]
58 * [reference-stmt stmtsep]
59 * "}")
60 *
61 * ANTLR grammar rule
62 * rangeStatement : RANGE_KEYWORD range (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
63 */
64
65/**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Represents listener based call back function corresponding to the "range"
Vidyashree Rama0b920732016-03-29 09:52:22 +053067 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
68 */
69public final class RangeRestrictionListener {
70
71 private static final String PIPE = "|";
72 private static final String RANGE_INTERVAL = "..";
73
74 /**
75 * Creates a new range restriction listener.
76 */
77 private RangeRestrictionListener() {
78 }
79
80 /**
81 * It is called when parser receives an input matching the grammar
82 * rule (range), performs validation and updates the data model
83 * tree.
84 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053085 * @param listener listener's object
86 * @param ctx context object of the grammar rule
Vidyashree Rama0b920732016-03-29 09:52:22 +053087 */
88 public static void processRangeRestrictionEntry(TreeWalkListener listener,
89 GeneratedYangParser.RangeStatementContext ctx) {
90
91 // Check for stack to be non empty.
92 checkStackIsNotEmpty(listener, MISSING_HOLDER, RANGE_DATA, ctx.range().getText(), ENTRY);
93
94 Parsable tmpData = listener.getParsedDataStack().peek();
95 if (tmpData.getYangConstructType() == TYPE_DATA) {
96 YangType type = (YangType) tmpData;
97 setRangeRestriction(type, ctx);
98 } else {
99 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RANGE_DATA,
100 ctx.range().getText(), ENTRY));
101 }
102 }
103
104 /**
105 * Sets the range restriction to type.
106 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530107 * @param type YANG type for which range restriction to be added
108 * @param ctx context object of the grammar rule
Vidyashree Rama0b920732016-03-29 09:52:22 +0530109 */
110 private static void setRangeRestriction(YangType type,
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530111 GeneratedYangParser.RangeStatementContext ctx) {
112
113 YangBuiltInDataTypeInfo<?> startValue, endValue;
114 YangRangeRestriction rangeRestriction = new YangRangeRestriction();
Vidyashree Rama0b920732016-03-29 09:52:22 +0530115
116 String rangeArgument = removeQuotesAndHandleConcat(ctx.range().getText());
117 String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
118
119 for (String rangePart : rangeArguments) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530120 String startInterval, endInterval;
121 YangRangeInterval rangeInterval = new YangRangeInterval();
Vidyashree Rama0b920732016-03-29 09:52:22 +0530122 String[] rangeBoundary = rangePart.trim().split(Pattern.quote(RANGE_INTERVAL));
123
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530124 if (rangeBoundary.length > 2) {
125 ParserException parserException = new ParserException("YANG file error : " +
126 YangConstructType.getYangConstructType(RANGE_DATA) + " " + rangeArgument +
127 " is not valid.");
128 parserException.setLine(ctx.getStart().getLine());
129 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
130 throw parserException;
Vidyashree Rama0b920732016-03-29 09:52:22 +0530131 }
132
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530133 if (rangeBoundary.length == 1) {
134 startInterval = rangeBoundary[0];
135 endInterval = rangeBoundary[0];
Vidyashree Rama0b920732016-03-29 09:52:22 +0530136 } else {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530137 startInterval = rangeBoundary[0];
138 endInterval = rangeBoundary[1];
Vidyashree Rama0b920732016-03-29 09:52:22 +0530139 }
140
141 try {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530142 startValue = getDataObjectFromString(startInterval, type.getDataType());
143 endValue = getDataObjectFromString(endInterval, type.getDataType());
144 } catch (DataTypeException e) {
145 ParserException parserException = new ParserException(e.getMessage());
146 parserException.setLine(ctx.getStart().getLine());
147 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
148 throw parserException;
149 }
150
151 rangeInterval.setStartValue(startValue);
152 rangeInterval.setEndValue(endValue);
153
154 try {
Vidyashree Rama0b920732016-03-29 09:52:22 +0530155 rangeRestriction.addRangeRestrictionInterval(rangeInterval);
156 } catch (DataModelException e) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530157 ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
158 UNHANDLED_PARSED_DATA, RANGE_DATA, rangeArgument, ENTRY, e.getMessage()));
159 parserException.setLine(ctx.getStart().getLine());
160 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
161 throw parserException;
Vidyashree Rama0b920732016-03-29 09:52:22 +0530162 }
163 }
164
165 if (rangeRestriction != null) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530166 if (type.getDataType() == YangDataTypes.DERIVED) {
167 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
168 .setExtendedInfo(rangeRestriction);
169 } else {
170 type.setDataTypeExtendedInfo(rangeRestriction);
171 }
Vidyashree Rama0b920732016-03-29 09:52:22 +0530172 }
173
174 }
175}