blob: b6e0cf6954cfce476707d416e3cbc0f0179f09f1 [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;
Vidyashree Rama0b920732016-03-29 09:52:22 +053022import org.onosproject.yangutils.parser.Parsable;
23import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24import org.onosproject.yangutils.parser.exceptions.ParserException;
25import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053027import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
Vidyashree Rama0b920732016-03-29 09:52:22 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Vidyashree Rama0b920732016-03-29 09:52:22 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053033import static org.onosproject.yangutils.utils.RestrictionResolver.isOfRangeRestrictedType;
34import static org.onosproject.yangutils.utils.RestrictionResolver.processRangeRestriction;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053035import static org.onosproject.yangutils.utils.YangConstructType.RANGE_DATA;
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053036import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Vidyashree Rama0b920732016-03-29 09:52:22 +053037
38/*
39 * Reference: RFC6020 and YANG ANTLR Grammar
40 *
41 * ABNF grammar as per RFC6020
42 * range-stmt = range-keyword sep range-arg-str optsep
43 * (";" /
44 * "{" stmtsep
45 * ;; these stmts can appear in any order
46 * [error-message-stmt stmtsep]
47 * [error-app-tag-stmt stmtsep]
48 * [description-stmt stmtsep]
49 * [reference-stmt stmtsep]
50 * "}")
51 *
52 * ANTLR grammar rule
53 * rangeStatement : RANGE_KEYWORD range (STMTEND | LEFT_CURLY_BRACE commonStatements RIGHT_CURLY_BRACE);
54 */
55
56/**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Represents listener based call back function corresponding to the "range"
Vidyashree Rama0b920732016-03-29 09:52:22 +053058 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
59 */
60public final class RangeRestrictionListener {
61
Vidyashree Rama0b920732016-03-29 09:52:22 +053062 /**
63 * Creates a new range restriction listener.
64 */
65 private RangeRestrictionListener() {
66 }
67
68 /**
69 * It is called when parser receives an input matching the grammar
70 * rule (range), performs validation and updates the data model
71 * tree.
72 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053073 * @param listener listener's object
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053074 * @param ctx context object of the grammar rule
Vidyashree Rama0b920732016-03-29 09:52:22 +053075 */
76 public static void processRangeRestrictionEntry(TreeWalkListener listener,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053077 GeneratedYangParser.RangeStatementContext ctx) {
Vidyashree Rama0b920732016-03-29 09:52:22 +053078
79 // Check for stack to be non empty.
80 checkStackIsNotEmpty(listener, MISSING_HOLDER, RANGE_DATA, ctx.range().getText(), ENTRY);
81
82 Parsable tmpData = listener.getParsedDataStack().peek();
83 if (tmpData.getYangConstructType() == TYPE_DATA) {
84 YangType type = (YangType) tmpData;
85 setRangeRestriction(type, ctx);
86 } else {
87 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, RANGE_DATA,
88 ctx.range().getText(), ENTRY));
89 }
90 }
91
92 /**
93 * Sets the range restriction to type.
94 *
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053095 * @param type YANG type for which range restriction to be added
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053096 * @param ctx context object of the grammar rule
Vidyashree Rama0b920732016-03-29 09:52:22 +053097 */
98 private static void setRangeRestriction(YangType type,
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053099 GeneratedYangParser.RangeStatementContext ctx) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530100
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530101 if (type.getDataType() == DERIVED) {
102 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
103 .setRangeRestrictionString(ctx.range().getText());
104 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
105 .setLineNumber(ctx.getStart().getLine());
106 ((YangDerivedInfo<YangRangeRestriction>) type.getDataTypeExtendedInfo())
107 .setCharPosition(ctx.getStart().getCharPositionInLine());
108 return;
Vidyashree Rama0b920732016-03-29 09:52:22 +0530109 }
110
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530111 if (!(isOfRangeRestrictedType(type.getDataType()))) {
112 ParserException parserException = new ParserException("YANG file error: Range restriction can't be " +
113 "applied to a given type");
114 parserException.setLine(ctx.getStart().getLine());
115 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
116 throw parserException;
117 }
118
119 YangRangeRestriction rangeRestriction = processRangeRestriction(null, ctx.getStart().getLine(),
120 ctx.getStart().getCharPositionInLine(), false, ctx.range().getText(), type.getDataType());
121
Vidyashree Rama0b920732016-03-29 09:52:22 +0530122 if (rangeRestriction != null) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +0530123 type.setDataTypeExtendedInfo(rangeRestriction);
Vidyashree Rama0b920732016-03-29 09:52:22 +0530124 }
Vidyashree Rama0b920732016-03-29 09:52:22 +0530125 }
126}